From: Matt J on

So basically, you need a way of estimating the rotation of the bar code image and then unrotating it?

I seem to remember an approach where somebody took the gradient of the image using

GradientX=diff(Image,1,1);
GradientY=diff(Image(2,2);

and then computed the angles

Angles=atan2(GradientX,GradientY);

Then you histogram the angles using histc and look for a peak to determine the orientation angle. In a perfectly/horizontal bar code image, the peaks would be at 0 and 90 degrees.

When you know the orientation angle theta, you can do

radon(Image, theta)

to see the bar code unrotated.
From: biswajit on
Thanks Matt..
As suggested,I try something as follows:

%Read a barcode image into the MATLAB workspace.
I = imread('1.JPG');

I=rgb2gray(I);

GradientX=diff(I,1,1);
GradientY=diff(I,2,2);
Angles=atan2(GradientX,GradientY); %i get an error here...
range=-pi:.1:pi;
temp=histc(Angles,range);
theta=max(temp);
%Then you histogram the angles using histc and look for a peak to
determine the orientation angle. In a perfectly/horizontal bar code
image, the peaks would be at 0 and 90 degrees.
%When you know the orientation angle theta, you can do
radon(I, theta)


The error I get is:
Function 'atan2' is not defined for values of class 'uint8'.Now
GradientX and GradientY have values between 0 and 255
correctly,depending on the difference between adjacent rows and
columns respectively.So what should be passed to atan2?
I am sure I am doing something wrong..can you please shed some
light ..moreover any reference to the source of this technique would
be very helpful.
thanks again
From: Matt J on
biswajit <biswami(a)gmail.com> wrote in message <673f2e59-5915-435f-8508-5da532e4e12f(a)u20g2000pru.googlegroups.com>...

> The error I get is:
> Function 'atan2' is not defined for values of class 'uint8'.
==============

Convert your I to double floating point type before doing any calculations with it:

I =double( imread('1.JPG') );

Certain operations are defined in MATLAB for integer arrays (e.g. addition and subtraction). That is why you were able to compute GradientX and GradientY without error messages, but in general the integer data formats in MATLAB are very limited.


>moreover any reference to the source of this technique would
> be very helpful.
=========

It was suggested by someone here in the NG a long time ago. I think it will be hard to track down that thread, but you could try...
From: ImageAnalyst on
Where did you post your image? Is this the same bar code issue (same
poster) that came up several weeks ago? If so, I don't have your link
handy so could you post it again? If not, post your image (hopefully
more than one!!!) for the first time.
From: biswajit on
Thanks Matt...that resolved the last error,but theres a new one with
the same function.
%Read an image into the MATLAB workspace.
I = double(imread('1.JPG'));

I=rgb2gray(I);

GradientX=diff(I,1,1);
GradientY=diff(I,1,2);
Angles=atan2(GradientX,GradientY);
Now GradientX is a 190x518 matrix while GradientY is a 191x517
matrix ..since they don't match,atan2( ) again throws error saying:
Matrix dimensions must agree.
Please shed some light.