Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何拉直图像中倾斜的方形形状?_Matlab_Image Processing_Image Rotation - Fatal编程技术网

Matlab 如何拉直图像中倾斜的方形形状?

Matlab 如何拉直图像中倾斜的方形形状?,matlab,image-processing,image-rotation,Matlab,Image Processing,Image Rotation,如何在图像中拉直倾斜的方形形状? 我不知道它倾斜的角度,代码必须计算它,然后自动旋转它 例如,我有以下图像: 应将其旋转以获得以下输出图像: 单向: I = imread('img.jpg'); I = rgb2gray(I); Ibw = I<threshold; %find the good threshold se = strel('square',sizesquare); %find the good size for the strel function. Ibw = imdi

如何在图像中拉直倾斜的方形形状? 我不知道它倾斜的角度,代码必须计算它,然后自动旋转它

例如,我有以下图像:

应将其旋转以获得以下输出图像:

单向:

I = imread('img.jpg');
I = rgb2gray(I);
Ibw = I<threshold; %find the good threshold
se = strel('square',sizesquare); %find the good size for the strel function.
Ibw = imdilate(Ibw,se); %fill the hole 
imshow(Ibw);

stat = regionprops(Ibw,'Extrema'); %extrema detection of the image.
point = stat.Extrema;
hold on
for i = 2:2:length(stat.Extrema)
    x = point(i,1);
    y = point(i,2);
    plot(x,y,'o');
    text(x,y,num2str(i),'color','w')
end
%construct the triangle that will help us to determine the shift angle.
P2 = [point(8,1),point(2,2)];
P1 = [point(8,1),point(8,2)];
P0 = [point(2,1),point(2,2)];

ang = atan2(abs(det([P2-P0;P1-P0])),dot(P2-P0,P1-P0))*180/pi

close all
imshow(imrotate(I,-ang))
I=imread('img.jpg');
I=RGB2灰色(I);

Ibw=I一种仅使用顶角和底角的简单方法。请注意,此方法依赖于最上角和最下角:

i = imread('sq.jpg');
i_bw = im2bw(i)==0;
% Modify the strel as required
se = strel('square', 10);
i_ed = imopen(i_bw, se);

limits = sum(i_ed, 2);

top_y = find(limits>0, 1);
bottom_y = find(limits>0, 1, 'last');
top_x = round(mean(find(i_ed(top_y, :)>0)));
bottom_x = round(mean(find(i_ed(bottom_y, :)>0)));    

slope = -1 * (top_y - bottom_y)/(top_x - bottom_x);
rot_angle = 2 * pi * atan(slope);

i2 = imrotate(i, -rot_angle);
imshow(i2)
之前

之后

您可以使用ìmrotate`旋转图像,并结合边缘检测算法,您将能够找到合适的角度图像上始终有正方形吗?或者你还有其他形状需要旋转?如果你尝试过一些东西,你可以与我们分享到目前为止你得到的东西。我总是在图像上固定大小的正方形。@PeymanPanjehbashi,但不与我们一起谢谢你我的朋友的回答,它奏效了。:)谢谢你的回答,我的朋友。:)