【问题标题】:Rotating triangle in image - MATLAB图像中的旋转三角形 - MATLAB
【发布时间】:2014-11-10 17:35:56
【问题描述】:

我正在编写一个用于在 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/

方向值:-28.9621 所以我将它旋转了 28.9621 度以改变它的位置

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

但输出是这样的:

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

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

另一个例子:将位置从左上第二三角形更改为左上第一三角形

【问题讨论】:

  • 您是否尝试过确定三角形的中心并使用二维旋转矩阵?
  • 您的示例的输出是什么?为什么你认为它是错误的?
  • CroCo & Breaker,我编辑了这个问题,希望它变得更清楚。

标签: matlab image-processing shape matlab-cvst


【解决方案1】:

这是一种解决方法。请注意:

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

2) 我使用regionprops 的'FilledArea' 属性来检测三角形。当只有一个形状时这很容易,但如果有很多形状,您将不得不修改一些代码。

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

原来的三角形:

%// 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。:

希望对您有所帮助!

【讨论】:

【解决方案2】:

我不是图像处理方面的专家,但实现旋转并不难。您唯一需要知道图像的中心。在 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 

结果是现在

如果您想将其围绕自身旋转,则需要将图像的中心平移到三角形的中心。这不是一个完整的解决方案,但我希望它有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多