图像校正

Question:

一张图像中有一张A4纸,通过图像处理的方法将其校正。

输入图像:                                      输出图像:


Answer:


function hough_transform
img=imread('input_¸±±¾.jpg');
figure
imshow(img)
axis on
title(['ԭͼÏñ']);

%canny ±ßÔµ¼ì²â
img=rgb2gray(img);
thresh=[0.01,0.4];
sigma=2;
temp = edge(double(img),'canny',thresh,sigma);
figure
imshow(temp,[]);
axis on
title(['canny ±ßÔµ¼ì²â']);

%»ô·ò±ä»»
[H, theta, rho]= hough(temp,'RhoResolution', 0.7);
peak=houghpeaks(H,4);
hold on
lines=houghlines(temp,theta,rho,peak);

figure 
imshow(temp,[])
axis on
title(['»ô·ò±ä»»½á¹û']),hold on
for k=1:length(lines)
    xy=[lines(k).point1;lines(k).point2]; 
    plot(xy(:,1),xy(:,2),'LineWidth',4,'Color','red');
end 
%{
disp (lines(1).point1);
disp (lines(1).point2);
disp (lines(2).point1);
disp (lines(2).point2);
disp (lines(3).point1);
disp (lines(3).point2);
disp (lines(4).point1);
disp (lines(4).point2);
%}
%͸Êӱ任
ori_vertex = [lines(2).point1;lines(4).point2;lines(2).point2;lines(1).point2]; %ԭͼÏñ¾ØÐÎËĸö¶¥µã
dest_vertex = [1 1;112 1;1 159;112 159];  %Ä¿±êͼÏñ¾ØÐÎËĸö¶¥µã
pers_form = cp2tform(ori_vertex,dest_vertex,'projective');
result = imtransform(img,pers_form,'XData',[1 112],'YData',[1 159]);
figure 
imshow(result)
axis on
title(['×îÖÕ½á¹ûͼÏñ']);
end

Algorithm description:

(1)先用canny算子作边缘检测,检测直线;(我将原图像缩小了)
thresh=[0.01,0.4];
sigma=2;
temp = edge(double(img),'canny',thresh,sigma);

(2)执行霍夫变换取得矩形四条边;
[H, theta, rho]= hough(temp,'RhoResolution', 0.7);
peak=houghpeaks(H,4);
hold on
lines=houghlines(temp,theta,rho,peak);

(3)利用原图矩形四个顶点执行透视变换转换到结果图像;
ori_vertex = [lines(2).point1;lines(4).point2;lines(2).point2;lines(1).point2]; 
%原图像矩形四个顶点
dest_vertex = [1 1;112 1;1 159;112 159];  %目标图像矩形四个顶点
pers_form = cp2tform(ori_vertex,dest_vertex,'projective');
result = imtransform(img,pers_form,'XData',[1 112],'YData',[1 159]);

猜你喜欢

转载自blog.csdn.net/qq_34200964/article/details/79528453
今日推荐