object detection using RGB and image
5 views (last 30 days)
Show older comments
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(1.3*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','Orientation');
%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;
be=stats(object).Orientation;
%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)))));
b=text(bc(1)-50,bc(2)+45, strcat('angel:', num2str(round(be))));
end
hold off
end
stop(vid);
%Flush the memory
flushdata(vid);
clear all;
I use this code to detect the red color of any object and i want to detect specific object from images using imread command but i am not perfect in using matlab to read the image then compare it with the live video to check if they are similar then start creating rectangle around the object so can anyone help me to modify the code
0 Comments
Answers (2)
Image Analyst
on 1 Apr 2019
I have code to track a colored sharpie marker. I'm attaching it. Adapt as needed.
5 Comments
saeed ahmed
on 2 Apr 2019
9 Comments
Image Analyst
on 4 Apr 2019
Yes, just use either
- getsnapshot() to read from a live video stream, or
- read() to read from a video file, or
- imread() to read from a still image file.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!