MATLAB Answers

Skew angle detection and correction in an image

109 views (last 30 days)
Aliyu Abdu
Aliyu Abdu on 28 Apr 2012
Commented: abhishek m on 20 Mar 2018
Can anyone help with steps or code on how to perform skew correction in an image that is turned at a certain angle using Hough Transformation or any simpler/easier way. The image is turned at an angle and I want to correct it by straightening it. You can view a sample of the image at this URL:-

  1 Comment

Image Analyst
Image Analyst on 29 Apr 2012
Rotation and spatial skewing are different things. Use imrotate to fix rotation, and imtransform to fix skewing.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 28 Apr 2012
Possibly, after you've supplied more information such as telling us what is skewed (histogram, image, ?), where to find your sample image (what URL did you upload it to), why you need to use Hough, specifically, to do the correction instead of any other routine that might be easier or more suitable, etc.
[ Update] See this demo:
clc; % Clear the command window.
clearvars;
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
format compact;
% Read in a standard MATLAB color demo image.
folder = 'C:\Users\Aliyu\Documents\Temporary';
baseFileName = 'test2.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Color 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')
% Make sure it's 8 bit gray scale, not 24 bit true color.
grayImage = rgb2gray(rgbImage);
% Binarize the image by thresholding.
binaryImage = grayImage > 128;
% Fill in the black letters so we just have a white block.
binaryImage = imfill(binaryImage, 'holes');
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Filled Image', 'FontSize', fontSize);
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage numberOfBlobs] = bwlabel(binaryImage, 8);
% Get all the blob properties.
blobMeasurements = regionprops(labeledImage, 'Orientation');
allOrientations = [blobMeasurements.Orientation]
% Extract just one orientation (there should only be one in this image).
angleToRotate = -allOrientations(1);
% Rotate the image.
rotatedImage = imrotate(grayImage, angleToRotate);
% Display the rotated image.
subplot(2, 2, 3);
imshow(rotatedImage, []);
title('Rotated Image', 'FontSize', fontSize);
It doesn't get it quite right but that's because the corners of your rectangle are missing - they've been clipped off by the edges of the image. If those were in there it would get it perfectly.

  3 Comments

Aliyu Abdu
Aliyu Abdu on 28 Apr 2012
I've supplied more info. to the question I previously asked. Perhaps this can give you an idea on how to help. I suggested Hough because I thought it was the easiest way to correct the skew angle but if there is a better, please help by providing me with it. Thanks.
Image Analyst
Image Analyst on 28 Apr 2012
Looks like imrotate() should do it. You just need to figure out the angle. Maybe call regionprops and ask for the "orientation" - that's what I'd try. See my File Exchange for a demo, BlobsDemo.
abhishek m
abhishek m on 20 Mar 2018
in rotated image box its showing same image and filled image is not showing. please help me

Sign in to comment.

More Answers (0)

Products


Translated by