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

时间:2016-09-15 08:49:43

标签: matlab image-processing image-rotation

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

例如,我有以下图片:

input_image

应该旋转

以提供以下输出图像:

output_image

2 个答案:

答案 0 :(得分:2)

一种方式:

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))

第1步

Step1

第2步

Step2

第3步

Step3

答案 1 :(得分:1)

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

<plugin>
  <groupId>org.scalatest</groupId>
  <artifactId>scalatest-maven-plugin</artifactId>
  <version>1.0</version>
  <configuration>
    <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
    <junitxml>.</junitxml>
    <filereports>WDF TestSuite.txt</filereports>
  </configuration>
  <executions>
    <execution>
      <id>test</id>
      <goals>
        <goal>test</goal>
      </goals>
    </execution>
  </executions>
</plugin>

<强> BEFORE BEFORE_SQ

<强> AFTER AFTER_SQ