基于canny边缘检测以及基于radon变换的矩形旋转矫正

方法一:

clear all; clc; close all;

Img = imread('test.jpg');
figure;
subplot(2, 2, 1); imshow(Img); title('original image');
I = rgb2gray(Img);
subplot(2, 2, 2); imshow(I); title('gray image');
bw = im2bw(I, graythresh(I));
bw = edge(bw, 'canny');
subplot(2, 2, 3); imshow(bw); title('canny image');

[r, c] = find(bw);
[rmin, indr] = min(r);
[cmin, indc] = min(c);
p1 = [rmin, c(indr)];
p2 = [r(indc) cmin];
hold on;
plot([p1(2) p2(2)], [p1(1) p2(1)], 'r-o');
k = (p2(1)-p1(1))/(p2(2)-p1(2));
theta = atan(k)/pi*180;
I1(:, :, 1) = imrotate(Img(:, :, 1), theta, 'bilinear');
I1(:, :, 2) = imrotate(Img(:, :, 2), theta, 'bilinear');
I1(:, :, 3) = imrotate(Img(:, :, 3), theta, 'bilinear');
subplot(2, 2, 4); imshow(I1); title('correct image');

result:
基于canny边缘检测以及基于radon变换的矩形旋转矫正_第1张图片

方法二:

clear,clc,close all;
I=imread('3.bmp');
imshow(I);title('Original image');
gray=rgb2gray(I);figure,imshow(gray);title('Grayscale image');
bw=edge(gray,'canny');
theta=1:180;
[R,xp]=radon(bw,theta);
[I0,J]=find(R>=max(max(R)));%J记录了倾斜角
qingxiejiao=90-J
I1=imrotate(I,qingxiejiao,'bilinear','crop');
figure,imshow(I1);title('correct image');

result:
基于canny边缘检测以及基于radon变换的矩形旋转矫正_第2张图片

你可能感兴趣的:(Matlab,DIP)