# How can I change the formula to detect the barcode in every angle? - MATLAB

How can I change the formula to detect the barcode in every angle?

 rgb = imread('barcode4.jpg');
% Resize Image
rgb = imresize(rgb,0.33);
figure(),imshow(rgb);
% Convert from RGB to Gray
Igray = double(rgb2gray(rgb));
B = abs(dIx) - abs(dIy);
% Low-Pass Filtering
H = fspecial('gaussian', 20, 10);
C = imfilter(B, H);
C = imclearborder(C);
figure(),imagesc(C);colorbar


If the filter you have works well enough for you, you could simply use imrotate to create several rotated versions of the source image, and use the filter you have on each of them (make sure to use bilinear to get sensible derivatives)

• Thanks for the advice. how can I create several rotated versions?? Sorry, I'm new to MatLab – Kim Jan 30 '12 at 16:58
• I manage to get the barcode to vertical or horizontal. However the formula can only detect horizontal barcode. Can I change the formula so that both vertical and horizontal can detect the barcode. – Kim Feb 2 '12 at 6:44
• mathworks.com/matlabcentral/fileexchange/31727-barcode-reader you can check out this link – vini Apr 8 '12 at 15:40

Your equation highlights areas where the magnitude of the gradient in the X direction is consistently higher than it is in the Y direction. To make this work in all directions, then you probably want areas where the magnitude of the gradient in any direction is high.

Try using the following:

B = double(sqrt(dIx.^2 + dIy.^2) > 0)


Not that this will basically highlight every edge, but the low-pass filter will mean that you need multiple edges close together.

• Thanks for the coding! But I don't know where to put the code as I try replacing the formula, everything went green. – Kim Jan 30 '12 at 16:51
• Try my updated version. – Nzbuu Jan 30 '12 at 19:27
• But now the whole image turn Red in color!! – Kim Jan 31 '12 at 8:34
• I manage to get the barcode to vertical or horizontal. However the formula can only detect horizontal barcode. Can I change the formula so that both vertical and horizontal can detect the barcode. – Kim Feb 2 '12 at 6:45
• I test this furmule '' B = double(sqrt(dIx.^2 + dIy.^2) > 0) " it's not work. – user4975 Jul 5 '13 at 19:20