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

radon校正

Radon(拉东)算法是一种通过定方向投影叠加,找到最大投影值时角度,从而确定图像倾斜角度的算法。具体过程如图所示 

                    图像倾斜校正 Radon 变换原理及函数_第1张图片

拉东变换

若函数F表示一个未知的密度,对F做radon变换,相当于得到F投影后的讯号,举例来说:F相当于人体组织,断层扫描的输出讯号相当于经过radon变换的F。 因此,可以用radon反变换从投影后的密度函数,重建原始的密度函数,它也是重建断层扫描的数学理论基础,另一个被广为人知名词的是三维重建  
radon变换后的讯号称作“正弦图”,因为一个偏离中心的点的radon变换是一个正弦曲线。所以对一些小点的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;
[R,xp] = radon(binaryImage,theta);
imagesc(theta,xp, R); colormap(jet);
xlabel('theta (degrees)');ylabel('x''');
title('theta方向对应的Radon变换R随着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;    
 [R,xp]=radon( bwbone,theta);
 [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');
Img=imread([pn fn]);
imshow(Img);title('Original image');
I = rgb2gray(Img);
I=improve_hist(I);
bw=edge(I,'canny');
theta=1:179;
[R,xp]=radon(bw,theta);
[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)
将图像A(图像的数据矩阵)绕图像的中心点旋转angle度, 正数表示逆时针旋转, 负数表示顺时针旋转。返回旋转后的图像矩阵。
B = imrotate(A,angle,method)
使用method参数可以改变插值算法,method参数可以为下面这三个值:
'nearest':最邻近线性插值(Nearest-neighbor interpolation)
'bilinear':  双线性插值(Bilinear interpolation)
'bicubic':  双三次插值(或叫做双立方插值)(Bicubic interpolation)
B = imrotate(A,angle,method,bbox)
bbox参数用于指定输出图像属性:
'crop': 通过对旋转后的图像B进行裁剪, 保持旋转后输出图像B的尺寸和输入图像A的尺寸一样。
'loose': 使输出图像足够大, 以保证 源图像旋转后超出 图像尺寸范围的像素值没有丢失。 一般这种格式产生的图像的尺寸都要大于源图像的尺寸。

你可能感兴趣的:(IT技术)