Detection of red color RGB

17 views (last 30 days)
saeed ahmed
saeed ahmed on 20 Mar 2019
Commented: Image Analyst on 21 Mar 2019
i want a solution for my problem snice i am doing object detection using RGB and i cant get the orientation of the object . here is the code , also i have a problem with different light intesity that camera can detect object when light is normal but if it get lets dark camera cant detect
vid=videoinput('winvideo',1,'YUY2_640x480');
%Set the frames&RGB
set(vid,'FramesPerTrigger',inf);
set(vid,'ReturnedColorspace','rgb')
%Set timer of screenshoting of images per millisecond
vid.FrameGrabInterval=0.5;
%Acquiring video
start(vid);
%Set number of Frames in video
while(vid.FramesAcquired<=10000)
%To store extracted frames
data=getsnapshot(vid);
%extracting the Red color from grayscale image
diff_im=imsubtract(data(:,:,1),rgb2gray(data));
%Filtering the noise
diff_im=medfilt2(diff_im,[3,3]);
%Converting grayscale image into binary image
diff_im=im2bw(diff_im,0.18);
%remove all pixels less than 300 pixel
diff_im=bwareaopen(diff_im,300);
%Draw rectangular boxes around the red object detected & label image
bw=bwlabel(diff_im,8);
stats=regionprops(bw,'BoundingBox','Centroid');
%Show image
imshow(data);
hold on
%create a loop for the regtangular box
for(object=1:length(stats))
%saving data of centroid and boundary in BB & BC
bb=stats(object).BoundingBox;
bc=stats(object).Centroid;
%Draw the rectangle with data BB & BC
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
%Plot the rectangle output
plot(bc(1),bc(2),'-m+')
%Output X&Y coordinates
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
end
hold off
end
stop(vid);
%Flush the memory
flushdata(vid);
clear all;

Accepted Answer

Image Analyst
Image Analyst on 21 Mar 2019
Use the Color Thresholder app (on the Apps tab of tool ribbon) to generate a function that will get a mask for the red blobs. Then you can get the orientation of the Old Spice package by using the Orientation property returned by regionprops().
As an example, see attached demo I just posted a few minutes ago for another poster.
See my Image Segmentation Tutorial: click here
  3 Comments
saeed ahmed
saeed ahmed on 21 Mar 2019
plus i am using a live video not a images
Image Analyst
Image Analyst on 21 Mar 2019
No problem. Just do color segmentation on each frame as you grab it.

Sign in to comment.

More Answers (1)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!