# 图像倾斜校正 Radon 变换原理及函数

matlab实现

clear all
clc
close all

[inputfilename,dirname] = uigetfile('*.*');
inputfilename = [dirname, inputfilename];
im = imread(inputfilename); % For example: 'input.jpg'

grayImage = rgb2gray(im);
%%%%%

%%%%% Edge detection and edge linking....
binaryImage = edge(grayImage,'canny'); % 'Canny' edge detector
binaryImage = bwmorph(binaryImage,'thicken'); % A morphological operation for edge linking
%%%%%

%%%%% Radon transform projections along 180 degrees, from -90 to +89....
% R: Radon transform of the intensity image 'grayImage' for -90:89 degrees.
% In fact, each column of R shows the image profile along corresponding angle.
% xp: a vector containing the radial coordinates corresponding to each row of 'R'.
% Negative angles correspond to clockwise directions, while positive angles
% correspond to counterclockwise directions around the center point (up-left corner).
% R1: A 1x180 vector in which, each element is equal the maximum value of Radon transform along each angle.
% This value reflects the maximum number of pixels along each direction.
% r_max: A 1x180 vector, which includes corresponding radii of 'R1'.
theta = -90:89;
imagesc(theta,xp, R); colormap(jet);
xlabel('theta (degrees)');ylabel('x''');
colorbar
%%%%%

[R1,r_max] = max(R);
theta_max = 90;
while(theta_max > 50 || theta_max<-50)
[R2,theta_max] = max(R1); % R2: Maximum Radon transform value over all angles.
% theta_max: Corresponding angle
R1(theta_max) = 0; % Remove element 'R2' from vector 'R1', so that other maximum values can be found.
theta_max = theta_max - 91;
end

correctedImage = imrotate(im,-theta_max); % Rotation correction
correctedImage(correctedImage == 0) = 255; % Converts black resgions to white regions

subplot(1,2,1), subimage(im)
subplot(1,2,2), subimage(correctedImage)

function [bw,qingxiejiao]=radontran(bwbone,bw)%radon倾斜校正

theta=1:90;
[I0,J]=find(R>=max(max(R)));%找倾斜角
qingxiejiao=90-J;
bw=imrotate(bw,qingxiejiao,'bilinear','crop');
clc;
clear all;
close all;
[fn pn fi]=uigetfile('*.*','choose a picture');
imshow(Img);title('Original image');
I = rgb2gray(Img);
I=improve_hist(I);
bw=edge(I,'canny');
theta=1:179;
[I0,J]=find(R>=max(R(:)));%J记录了倾斜角
qingxiejiao=90-J;
I1=imrotate(Img,qingxiejiao,'bilinear','crop');
subplot(1,2,1),imshow(Img);title('Original image');
subplot(1,2,2),imshow(I1);title('correct image');

B = imrotate(A,angle)

B = imrotate(A,angle,method)

'nearest'：最邻近线性插值（Nearest-neighbor interpolation）
'bilinear'： 双线性插值（Bilinear interpolation）
'bicubic'： 双三次插值（或叫做双立方插值）（Bicubic interpolation）
B = imrotate(A,angle,method,bbox)
bbox参数用于指定输出图像属性：
'crop'： 通过对旋转后的图像B进行裁剪， 保持旋转后输出图像B的尺寸和输入图像A的尺寸一样。
'loose'： 使输出图像足够大， 以保证源图像旋转后超出图像尺寸范围的像素值没有丢失。 一般这种格式产生的图像的尺寸都要大于源图像的尺寸。