2014-11-08 174 views
1

我正在写一个程序来检测MATLAB中的基本形状。 当我检测到形状时,我评估它的方向,然后旋转形状使其方向为零,然后我可以评估它的投影并指定它是什么。在图像中旋转三角形 - MATLAB

问题是MATLAB函数:regionprops()不正确评估三角形的方向。

I = zeros(256,256); 
pos_triangle = [64 64 128 192 128 128]; 
Is = insertShape(I, 'FilledPolygon', pos_triangle); 
imshow(Is) 

original = Is; 

originalBW = im2bw(original); 
figure; imshow(originalBW); 
S = regionprops(originalBW,'All'); 

bwr=imrotate(originalBW,S.Orientation); 
S2 = regionprops(bwr,'Centroid','Orientation'); 

figure;imshow(bwr); 

我使用imrotate fnc旋转图像,我没有旋转问题,imrotate工作正常。问题在于计算方向[使用'regionprps()'fnc]的图像!例如:我想从这个位置转动三角形

http://postimg.org/image/4un4sc7pn/

Orentation值:-28.9621 所以我将其旋转28.9621度到其现在的位置改变这一

http://postimg.org/image/x68opdrm3/

但输出是这样的:

http://postimg.org/image/yf15or8y3/

使用他们的方位(或图像的其他可能的属性)

的其他例子:由Up左第二三角形改变位置,以向上左第一三角形

+0

您是否试图确定三角形的中心并使用2D旋转矩阵? – CroCo 2014-11-08 21:21:56

+0

你的例子的输出是什么?你为什么认为这是错的? – beaker 2014-11-09 20:07:01

+0

CroCo&breaker,我编辑问题,希望它变得更清楚。 – 2014-11-10 09:27:44

回答

2

下面是一个方式工作出来。注意:

1)我没有计算机视觉系统工具箱,所以我不能使用insertShape;相反,我使用fill函数在图像中创建三角形,然后使用getframe获取实际图像。我想这与使用insertShape相同。

2)我用'FilledArea'属性regionprops来检测三角形状。如果只有一个形状,这很容易,但是如果有很多形状的话,你需要修改一些代码。

3)我执行的旋转等于-1 *由regionprops给出的方向使其回到0.您当然可以改变它。旋转后的图像更大以解释旋转。

下面是代码:

clear 
clc 
close all 

I = zeros(256,256); 
pos_triangle = [64 64 128 192 128 128]; 

xt = [64 128 128]; 
yt = [64 192 128]; 

%// Since I don't have the Computer Vision System Toolbox I use 'fill'. 
%//Is = insertShape(I, 'FilledPolygon', pos_triangle); 
imshow(I) 
hold on 

fill(xt,yt,'w') 
hold off 

原三角形:

enter image description here

%// Create image using getframe. 
hFrame = getframe(gca); 
original = hFrame.cdata; 

originalBW = im2bw(original(:,:,1)); 

%// Remove border of the image. 
originalBW = imclearborder(originalBW); 

figure; 

imshow(originalBW); 

S = regionprops(originalBW,'FilledArea','Orientation'); 

%// Find region filled with the most pixels: that's the shape. 
[a,b] =max([S.FilledArea]); 

%// Get corresponding orientation 
Orientation = S(b).Orientation; 

%// Rotate by the inverse of the orientation; I'm not sur that's what you 
%// want but it looks OK. 
bwr=imrotate(originalBW,-1*Orientation); 
S2 = regionprops(bwr,'Centroid','Orientation'); 

figure;imshow(bwr); 

旋转后的三角形,它看起来像它的取向为0:

enter image description here

希望有帮助!

+0

亲爱的Benoit_11,thanX回答。我检查你的解决方案的另一个三角形{如:http://media-cache-ec0.pinimg.com/236x/bd/fb/91/bdfb9174a2ffa7bda5de160e4c1f0622.jpg}和同样的问题仍然存在。 – 2014-11-08 20:25:19

0

我不是图像处理方面的专家,但要实现旋转并不难。你唯一需要知道的图像中心。在Matlab中,左上角的图像中心。要围绕中心旋转对象,您需要将每个点乘以旋转矩阵。让我们围绕图像中心旋转三角形。

clear 
clc 
close all 

I = zeros(256,256); 
pos_triangle = [64 64 128 192 128 128]; 

xt = [64 128 128]; 
yt = [64 192 128]; 

a = deg2rad(20); 
R = [cos(a) -sin(a); 
    sin(a) cos(a)]; 

p1 = R*[64; 64] 
p2 = R*[128; 192] 
p3 = R*[128; 128] 

% the rotated points 
x1 = [p1(1) p2(1) p3(1)]; 
y1 = [p1(2) p2(2) p3(2)]; 

imshow(I) 
hold on 

fill(x1,y1,'r') 
fill(xt,yt,'w') 
hold off 

结果现在

enter image description here

如果你想自己周围旋转它,那么你需要的图像的中心转换为三角形的中心。这不是一个完整的解决方案,但我希望它有帮助。