基於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:
這裏寫圖片描述

方法二:

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:
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章