How to remove time lag and multiple centroid in this code?

1 回表示 (過去 30 日間)
Rohit Dixit
Rohit Dixit 2014 年 9 月 26 日
コメント済み: Image Analyst 2014 年 9 月 27 日
The cursor does not follows red color properly and it detects white and various other color also. currently i am doing virtual mouse using color detection. this is code please help.
clear all
close all
clc
import java.awt.Robot
m=Robot;
v=videoinput('winvideo',1,'YUY2_640x480');
set(v,'ReturnedColorSpace','YCbCr');
v.FramesPerTrigger=inf;
v.FrameGrabInterval=1;
pause(1)
while (v.FramesAcquired<=35)
i=getsnapshot(v);
% i=imresize(i,2);
% imtool(im_resized)
first_comp=i(:,:,1);
% imtool(first_comp)
rgb_2_gray=rgb2gray(i);
% imtool(rgb_2_gray)
d=first_comp-rgb_2_gray;
% imtool(d)
b=im2bw(d,.1);
bw=bwareaopen(b,500);
imshow(bw);
hold on;
% end;
% % bw=bwlabel(bw,8);
% % imtool(bw)
p=regionprops(bw,'centroid');
for z=1:length(p)
a=p(z).Centroid;
plot(a(1),a(2),'r*');
m.mouseMove(a(1),a(2));
end;
end;
  1 件のコメント
Matt J
Matt J 2014 年 9 月 26 日
For your reference, your code is now much more readable because I applied this formatting button to it,

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2014 年 9 月 26 日
It's just not a very robust color segmentation algorithm. You might be better off going to hsv color space. See my File Exchange. However one of the methods I use there does find red objects in RGB color space. http://www.mathworks.com/matlabcentral/fileexchange/26420-simplecolordetection
  2 件のコメント
Rohit Dixit
Rohit Dixit 2014 年 9 月 27 日
This link was very helpful to understand quickly now will be able to detect green and blue color and perform clicking accordingly.
Image Analyst
Image Analyst 2014 年 9 月 27 日
I just looked at it. It's not any more robust than yours was - in fact it's pretty much the same algorithm. Well, whatever, if it works in your specific situation, then fine.

サインインしてコメントする。

その他の回答 (1 件)

Rohit Dixit
Rohit Dixit 2014 年 9 月 27 日
Thank you sir good to know. I will improve the robustness in code.

Community Treasure Hunt

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

Start Hunting!

Translated by