How to detect red objects from a video and also find its centroid. I have written code for it but i am getting error
Show older comments
sampling_factor = 1;
resizing_params = [500 500];
%%// Input video
xyloObj = VideoReader('myVideo.avi');
%%// Setup other parameters
nFrames = floor(xyloObj.NumberOfFrame/sampling_factor); %%// xyloObj.NumberOfFrames;
vidHeight = resizing_params(1); %// xyloObj.Height;
vidWidth = resizing_params(1); %// xyloObj.Width;
%// Preallocate movie structure.
mov(1:nFrames) = struct('cdata', zeros(vidHeight, vidWidth, 3, 'uint8'),'colormap',[]);
%// Read one frame at a time.
for k = 1 :nFrames
IMG = read(xyloObj, (k-1)*sampling_factor+1);
%// IMG = some_operation(IMG);
mov(k).cdata = imresize(IMG,[vidHeight vidWidth]);
end
%// Size a figure based on the video's width and height.
hf = figure;
set(hf, 'position', [150 150 vidWidth vidHeight])
%// Play back the movie once at the video's frame rate.
%movie(hf, mov, 1, xyloObj.FrameRate);
while i=1:nFrames
im_in = frame2im(i);
diff_im = imsubtract(im_in(:,:,1), rgb2gray(im_in));
diff_im = medfilt2(diff_im, [3 3]);
diff_im = im2bw(diff_im,0.18);
diff_im = bwareaopen(diff_im,300);
bw = bwlabel(diff_im, 8);
stats = regionprops(bw, 'BoundingBox', 'Centroid');
imshow(im_in)
hold on
%This is a loop to bound the red objects in a rectangular box.
for im_in = 1:length(stats)
bb = stats(im_in).BoundingBox;
bc = stats(im_in).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
im_outs=reshape(bw,size(im_in));
im_out = double(im_outs);
end
hold off
end
% Both the loops end here.
frm = im2frame(im_outs);
aviobj = addframe(aviobj,frm);
aviobj = close(aviobj);
return;
1 Comment
sandeep
on 6 Nov 2014
on running your code it says 'Undefined function or method 'VideoReader' for input arguments of type 'char'.
Accepted Answer
More Answers (1)
Bruno Pop-Stefanov
on 6 Oct 2014
2 votes
I would recommend to do some color thresholding to isolate the red objects. Image Processing Toolbox has a useful Color Thresholder app that you can use to easily generate code to do color thresholding. For example, you could extract one frame from the video and open it in Color Thresholder, set the parameters the way you want in order to isolate the red objects using an appropriate color space, and then generate M code that you can use on all frames of the video.
You can read more about Color Thresholder here:
Once you have a black and white mask representing the red objects, it's easy to find the centroids of the objects using the regionprops function as you are already doing in the code.
As for the error that you are getting: can you copy and paste the entire error message?
I can't run your code because I don't have the appropriate data to run it. Having the exact error message could help us figure out what is going wrong.
1 Comment
Image Analyst
on 6 Oct 2014
Thresholding is essentially what he's doing with this line:
diff_im = im2bw(diff_im,0.18);
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!