How can I identify broken pieces of rice using my webcam?
1 回表示 (過去 30 日間)
古いコメントを表示
I have been using this code to use my webcam to track objects in real time(the code tracks red objects only):
% Capture the video frames using the videoinput function % You have to replace the resolution & your installed adaptor name. vid = videoinput('winvideo', 1,'YUY2_352x288');
% Set the properties of the video object set(vid, 'FramesPerTrigger', Inf); set(vid, 'ReturnedColorspace', 'rgb') vid.FrameGrabInterval = 5;
%start the video aquisition here start(vid)
% Set a loop that stop after 100 frames of aquisition while(vid.FramesAcquired<=200)
% Get the snapshot of the current frame
data = getsnapshot(vid);
% Now to track red objects in real time
% we have to subtract the red component
% from the grayscale image to extract the red components in the image.
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
%Use a median filter to filter out noise
diff_im = medfilt2(diff_im, [3 3]);
% Convert the resulting grayscale image into a binary image.
diff_im = im2bw(diff_im,0.18);
% Remove all those pixels less than 300px
diff_im = bwareaopen(diff_im,60);
% Label all the connected components in the image.
bw = bwlabel(diff_im, 8);
% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats = regionprops(bw, 'BoundingBox', 'Centroid');
% Display the image
imshow(diff_im)
hold on
%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).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');
end
hold off
end
% Both the loops end here.
% Stop the video aquisition. stop(vid);
% Flush all the image data stored in the memory buffer. flushdata(vid);
% Clear all variables clear all
I've got it from a blog, but I need to be able to identify the broken rices instead the red objects. Tried to change some lines (imsubtract) but can't make it to be what I need.
2 件のコメント
Florian Morsch
2018 年 5 月 11 日
What do you mean by "broken rices"? Do you have rice and want to identify broken ones? How do you define broken?
回答 (1 件)
Florian Morsch
2018 年 5 月 11 日
You can try to find all grains first, then check for the pixels in each found object. If you have a set camera distance you will know how many pixel (give or take a few) one grain will have. So now you count the pixels of every found object and if its way lower then your average then it might be a broken one. If you have a moving camera you need a reference object.
This will work best if the grains are seperated and not touching each other. Otherwise two grains might be counted as one.
In this link you will find a step-by-step list what you could do: https://www.slideshare.net/SriramEmarose/bsaic-machine-vision-applications-using-mat-lab
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!