使用网络摄像头检测白板matlab上的黑色方块

时间:2014-11-10 20:51:47

标签: image algorithm matlab image-processing

我是一名刚刚学习matlab的学生,我正在使用R2014a版的学生。

我需要使用网络摄像头来检测随机共置的3个方格(每个2厘米x 2厘米)。我必须有3个方格中的每一个的位置和质心,我必须有位置和方向(与图像的x轴的相对角度),我应该能够不断地检测它们。 网络摄像头被设置到一个固定的位置,我需要知道从每个方块到它在板上的相对位置的距离。

我到目前为止的代码如下:(目前我无法正确检测它们,我无法知道距离)

vid=videoinput('winvideo', 1)
preview(vid)
constantlydoing =1
while constantlydoing>=1
%Sacar fotografía del tablero (obtain a photo of the board)
image=getsnapshot(vid);
imshow(image)


%Conversión BW
level = graythresh(image);
imageBW = im2bw(image,level);
imageBW= not (imageBW);
imshow(imageBW);
L = bwlabel(imageBW,8);
imshow(L);
pause(1);
s=regionprops(L, 'centroid');
centroids = cat(1, s.Centroid);
j=regionprops(L, 'Perimeter');
k=regionprops(L, 'orientation');
orientations = cat(1, k.Orientation);


%determinamos perímetro (perimeter detect)
perimetro = cat(1, j.Perimeter);
[m,n]= size(perimetro);
pos=find(perimetro(:,1)>=100);
centroids=[centroids(pos(1),1) centroids(pos(1),2);...
       centroids(pos(2),1) centroids(pos(2),2);...
       centroids(pos(3),1) centroids(pos(3),2)];
%orientacion (orientation)

orientations=[orientations(pos(1),1);...
       orientations(pos(2),1) ;...
       orientations(pos(3),1)]

%Localización de los cubos (localization of squares)
cubo1=centroids(1,1:2);
cubo2=centroids(2,1:2);
cubo3=centroids(3,1:2);      
hold on; 
plot(centroids(:,1),centroids(:,2),'b*');
hold off;
propied=regionprops(L,'BoundingBox');
hold on

% Plot Bounding Box
for n=1:size(propied,1)
rectangle('Position',propied(n).BoundingBox ...
,'EdgeColor','g','LineWidth',2); ...
end
hold off
pause(1);
end

0 个答案:

没有答案
相关问题