Azzera filtri
Azzera filtri

How to align the whole image according to a line ?

14 visualizzazioni (ultimi 30 giorni)
Vic
Vic il 21 Gen 2013
I have this code below which detects lines on my image but I can't align the whole image( scanned image) to make the lines straight, therefore align the text as well.
The left is the input image and the right should the fixed one.
Any solutions??
Thank you
  2 Commenti
Vic
Vic il 21 Gen 2013
Modificato: Walter Roberson il 21 Gen 2013
%# read and crop image
I = imread('C:\Users\Victoras\Desktop\attempt2.bmp');
%I = imread('http://i.stack.imgur.com/CJHaA.png');
I = I(:,1:end-3,:); %# remove small white band on the side
%# egde detection
BW = edge(rgb2gray(I), 'canny');
%# hough transform
[H T R] = hough(BW);
P = houghpeaks(H, 4, 'threshold',ceil(0.75*max(H(:))));
lines = houghlines(BW, T, R, P);
% shearing transforma
slopes = vertcat(lines.point2) - vertcat(lines.point1);
slopes = slopes(:,2) ./ slopes(:,1);
TFORM = maketform('affine', [1 -slopes(1) 0 ; 0 1 0 ; 0 0 1]);
II = imtransform(I, TFORM);
%# show edges
figure, imshow(BW)
%# show accumlation matrix and peaks
figure, imshow(imadjust(mat2gray(H)), [], 'XData',T, 'YData',R, 'InitialMagnification','fit')
xlabel('\theta (degrees)'), ylabel('\rho'), colormap(hot), colorbar
hold on, plot(T(P(:,2)), R(P(:,1)), 'gs', 'LineWidth',2), hold off
axis on, axis normal
%# show image with lines overlayed, and the aligned/rotated image
figure
subplot(121), imshow(I), hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1), xy(:,2), 'g.-', 'LineWidth',2);
end, hold off
subplot(122), imshow(II)
Matt J
Matt J il 21 Gen 2013
Reformatting:
%# read and crop image
I = imread('C:\Users\Victoras\Desktop\attempt2.bmp');
%I = imread('http://i.stack.imgur.com/CJHaA.png');
I = I(:,1:end-3,:); %# remove small white band on the side
%# egde detection
BW = edge(rgb2gray(I), 'canny');
%# hough transform
[H T R] = hough(BW);
P = houghpeaks(H, 4, 'threshold',ceil(0.75*max(H(:))));
lines = houghlines(BW, T, R, P);
% shearing transforma
slopes = vertcat(lines.point2) - vertcat(lines.point1);
slopes = slopes(:,2) ./ slopes(:,1);
TFORM = maketform('affine', [1 -slopes(1) 0 ; 0 1 0 ; 0 0 1]);
II = imtransform(I, TFORM);
%# show edges figure, imshow(BW)
%# show accumlation matrix and peaks figure, imshow(imadjust(mat2gray(H)), [], 'XData',T, 'YData',R, 'InitialMagnification','fit')
xlabel('\theta (degrees)'),
ylabel('\rho'),
colormap(hot),
colorbar hold on,
plot(T(P(:,2)), R(P(:,1)), 'gs', 'LineWidth',2),
hold off
axis on,
axis normal
%# show image with lines overlayed, and the aligned/rotated image figure subplot(121),
imshow(I),
hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1), xy(:,2), 'g.-', 'LineWidth',2);
end,
hold off
subplot(122),
imshow(II)

Accedi per commentare.

Risposte (2)

Matt J
Matt J il 21 Gen 2013
Not sure why you would apply a shear. Shouldn't it just be a rotation+translation?
  2 Commenti
Vic
Vic il 21 Gen 2013
Using Hough Transform to compute line angles so that I can perform a Shearing Transform to align the image. Isn't that the idea? Any ideas?
Thanks
Matt J
Matt J il 21 Gen 2013
I already told you my idea - to apply a rotation/translation instead of a shear.
However, there could be something I don't understand about the problem. That's why I also asked you what made you decide on a shear.

Accedi per commentare.


Image Analyst
Image Analyst il 21 Gen 2013
Once you determine the angle of that long line in the center, simply call imrotate(). Then crop if desired.
  8 Commenti
Vic
Vic il 21 Gen 2013
Things are getting interesting here, thanks for all the help and advice!!! In my mind right now the best way to do it is to compare the orientation of the line between the two images to find the rotation of the scanned image and using that difference apply a rotation. After this, I will use translation to find the the end point of the lines and again compare the (x,y) coordinates.
Am entirely new at this and if the above I wrote are correct could please provide me with the necessary functions from start to bottom in order to find a way do it on my own?
Many thanks!!!
Image Analyst
Image Analyst il 21 Gen 2013
Here's a start:
% Read in a demo image.
folder = 'C:\Users\Vic\Documents\Temporary';
baseFileName = 'testsheet.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage)
if numberOfColorBands > 1
% Convert to grayscale.
grayImage = rgb2gray(grayImage);
end
% Display the original gray scale image.
subplot(1, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
uiwait(msgbox('Locate the two endpoints of the line.'));
[actualx actualy] = ginput(2)
% Determine where the end points should be in the final, aligned image.
desiredx = mean(actualx);
desiredy = zeros(2,1);
desiredy(1) = actualy(1);
lineLength = hypot(actualx(1)-actualx(2), actualy(1)-actualy(2))
desiredy(2) = desiredy(1) + lineLength;
Then call maketform(), followed by tformfwd().

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by