object detection using RGB and image

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

Respuestas (2)

Image Analyst
Image Analyst el 1 de Abr. de 2019

1 voto

I have code to track a colored sharpie marker. I'm attaching it. Adapt as needed.

5 comentarios

saeed ahmed
saeed ahmed el 1 de Abr. de 2019
Editada: Image Analyst el 2 de Abr. de 2019
But my task is to track a Coca-Cola can so I need to use image processing to measure its features. Then to detect it, for example if any other type of can, like Pepsi, is in front of camera it will skip and no action will be taken.
Image Analyst
Image Analyst el 2 de Abr. de 2019
Sounds like fun. Good luck.
If you need any help, see my File Exchange for more demos. Or ask a question here.
saeed ahmed
saeed ahmed el 2 de Abr. de 2019
Ok i will check it
saeed ahmed
saeed ahmed el 2 de Abr. de 2019
i didnt find yet my target yet do you have any idea how to make my task ?
Image Analyst
Image Analyst el 2 de Abr. de 2019
Maybe you can upload a short video with your target moving in it.

Iniciar sesión para comentar.

saeed ahmed
saeed ahmed el 2 de Abr. de 2019

1 voto

More explanation please

9 comentarios

Image Analyst
Image Analyst el 3 de Abr. de 2019
Editada: Image Analyst el 3 de Abr. de 2019
I'm sure my code is well explained. I put way, WAY more comments into my programs than anyone else.
More data (your video) please.
saeed ahmed
saeed ahmed el 3 de Abr. de 2019
ok but now i have extract features of my object and i want to start a video and compare the seen object by webcam to my input image that i extracted its feature , can i do that ??
Image Analyst
Image Analyst el 3 de Abr. de 2019
Yes, did you see the "deep learning in 9 lines of code" video that Mathworks has posted here? Search for Deep Learning in the main web site (not Answers) and I'm sure you will find it. It does that.
saeed ahmed
saeed ahmed el 3 de Abr. de 2019
ok i will check it and if i cant reach it i will back for you , Thank you much for your time
saeed ahmed
saeed ahmed el 3 de Abr. de 2019
now i give up searching about this comparsion because i didnt reach anything , but i will try adjust my code i want to detect the coordnates of my object place on a circle table shall i divide the circle into parts to detect the right position of the object place on this circle
Image Analyst
Image Analyst el 3 de Abr. de 2019
That should be pretty easy. Upload the code to process one frame, plus a frame (image) that it can work with, if you still have trouble.
You can see My Image Segmentation Tutorial if you need help.
saeed ahmed
saeed ahmed el 4 de Abr. de 2019
%This Function is used to detect the objects from the lower plateform
%And also identifies its location and orientation measured from the right
%axis
clear all;
close all;
%connecting with the camera
vid=webcam;
%you can change the snapshot() with imread if you don't have a camera and want to %test your code
a=snapshot(vid);
%a=imread('testNow.jpg');
sum=0;
s1=floor(size(a(:,:,2),1)/2);
s2=floor(size(a(:,:,2),2)/2);
M=[s1 s1 s1 s1 s1];
K=[s2 s2 s2 s2 s2];
%Subtracting the green matrix from the rgb image
imggreen=imsubtract(1.2*a(:,:,2),rgb2gray(a));
%median filter to remove the noise if existed
imggreen=medfilt2(imggreen,[3,3]);
%transforming te image into binary
imggreen=im2bw(imggreen,0.18);
%removing areas below 300 pixels
imggreen=bwareaopen(imggreen,300);
figure,imshow(imggreen)
Spacing_cols=floor(size(imggreen,2)/6);
Spacing_rows=floor(size(imggreen,1)/6);
for(i=1:5)
sum = sum + Spacing_cols;
N = find(imggreen(:,sum));
if(size(N,1)~=0)
M(i)=N(1); % to get the numbers of rows of 1
end
end
sum=0;
for(j=1:5)
sum = sum + Spacing_rows;
L = find(imggreen(sum,:));
if(size(L,2)~=0)
K(j)=L(1); % to get the numbers of Col.s of 1
if(j==3)
v = L(size(L,2))-L(1)+2.5
end
end
end
M_Avg=floor((M(1)+M(2)+M(3)+M(4)+M(5))/5);
K_Avg=floor((K(1)+K(2)+K(3)+K(4)+K(5))/5);
Coordinates=zeros(size(imggreen,1),size(imggreen,2));
Coordinates(M_Avg,:)=1;
Coordinates(:,K_Avg)=1;
[x,y,d,RotAngle]=ObjectDetection(a,M_Avg,K_Avg,v);
figure,imshow(Coordinates | d)
hold on
text(K_Avg,10,['+ve Y-axis'],'BackgroundColor',[0 1 1]);
text(0,M_Avg,['+ve X-axis'],'BackgroundColor',[0 1 1]);
ObjectDetection
%This function is called inside the Detection.m file to locate the centroid
%and the orientation
function[x,y,A,angle]=ObjectDetection(rgbImage,x_zero,y_zero,scale)
prod_image=rgbImage;
%thresholding the image channel by channel to get the best result than...
%...thresholding the image at one time.
red=prod_image(:,:,1);
green=prod_image(:,:,2);
blue=prod_image(:,:,3);
I1=im2bw(red,0.6);
I2=im2bw(green,0.6);
I3=im2bw(blue,0.6);
%Anding the channels again
m=I1&I2&I3;
figure,imshow(m)
%Apply Median Filter to remove the noise.
A=medfilt2(m);
A=imerode(A,strel('square',30));
%remove all areas below 500 Pixels.
A=bwareaopen(A,500);
% Filling the holes in the white areas
%A=imfill(A,'holes');
figure,imshow(A)
labelled=bwlabel(A);
props=regionprops(labelled,'BoundingBox','Centroid','Orientation');
% box=props(1).BoundingBox;
% rectangle('position',box,'EdgeColor','r','LineWidth',2)
figure,imshow(prod_image)
hold on
center=props(1).Centroid;
% Plotting the Centroid
plot(center(1),center(2),'-m+')
x = (center(1) - y_zero)/(scale*10/12);
y = (center(2) - x_zero)/(scale*10/12);
angle=props(1).Orientation
text(round(center(1))-50,round(center(2))+20,['The Cenroid','(',...
num2str(round(x)),', ',num2str(round(y)),')'],'BackgroundColor',[0 1 1]);
text(round(center(1))-50,round(center(2))+45,['Rotation Angle from right horizontal',' ',num2str(angle)],'BackgroundColor',[0 1 1]);
end
can i convert this code into a live video ??
Image Analyst
Image Analyst el 4 de Abr. de 2019
Yes, just use either
  1. getsnapshot() to read from a live video stream, or
  2. read() to read from a video file, or
  3. imread() to read from a still image file.
saeed ahmed
saeed ahmed el 4 de Abr. de 2019
when i use getsnapshot it gives me error could you please add me the important command to convert it into live video stream

Iniciar sesión para comentar.

Preguntada:

el 1 de Abr. de 2019

Comentada:

el 4 de Abr. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by