SoftwarePractice.org: Home | Courseware | Wiki | Archive

Anglex.m Code

From SoftwarePractice.org

% This function rotates the image if the bar code is 
% theta degres angle, 
% returns the rotates image(array) and and flag to be use to 
% to inform user weather the original image rotates
% Use [rotateI,flag]= anglex(I)
function [I2,flag] = anglex(I)
% clear all;close all;
% I = imread('example18_90.jpg');
% input image and convert to type double for rotation
%if image is type jpg, convert to a 2dimensional array for use
I = double(im2bw(I));
% figure(1), imshow(I);
%Perform edge detection with canny filter
BW = edge(I,'canny');
% figure(2), imshow(BW);
%compute Radon transform of edge image
%determines projection angle of image
theta = 0:179;
[R,xp] = radon(BW,theta);
% figure(3),imagesc(theta, xp, R); colormap(hot)
%find angle thetap & offset xpp of image.
%the found angle and offsets correspond to the maximum value in the Radon
%transform & the strongest line evident in the image
[r,c] = find(R == max(R(:)));
thetap = theta(c(1)) ;           
xpp = xp(r(1));
flag=0;
if (thetap ~= 0)
   flag=1;
   %rotate image at angle found from Radon transform
   if (thetap==90 && xpp ~=-90)
       I2=I;
   elseif(xpp==0 && thetap>60)
       I2=I;
   elseif (xpp>=0 && thetap >=45)
       I2 = imrotate(I,-thetap,'bilinear', 'crop');
   elseif (thetap>90 && xpp< 0)
       I2=imrotate(I,thetap-90,'bilinear');
   elseif (xpp < 0 && thetap >= 45)
       I2=imrotate(I,90-thetap,'bilinear', 'crop');
   elseif ((xpp <= -1 && xpp >= -50) && thetap >45)
       I2 = imrotate(I,thetap-90,'bilinear');
   elseif (xpp >= 0 && thetap < 45)
       I2 = imrotate(I, -thetap, 'bilinear');
   elseif (xpp < 0 && thetap < 45) 
       I2 = imrotate(I,-thetap,'bilinear');
   end
elseif (thetap==0 && xpp <=-45 && xpp>=-60)
   flag=1;
   I2 = imrotate(I, 90, 'bilinear');
else
   flag=0;
   I2=I;
end
%  figure(4), imshow(I2);



To return back to the angular rotation page: Angular Rotation

Personal tools