14

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

Formula

Original Image Processed Image

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

2 Answers2

8

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)

Niki Estner
  • 6,181
  • 1
  • 30
  • 30
  • Thanks for the advice. how can I create several rotated versions?? Sorry, I'm new to MatLab –  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 02 '12 at 06:44
  • http://www.mathworks.com/matlabcentral/fileexchange/31727-barcode-reader you can check out this link – vini Apr 08 '12 at 15:40
7

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.

Nzbuu
  • 271
  • 2
  • 6
  • Thanks for the coding! But I don't know where to put the code as I try replacing the formula, everything went green. –  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 08: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 02 '12 at 06:45
  • I test this furmule '' B = double(sqrt(dIx.^2 + dIy.^2) > 0) " it's not work. –  Jul 05 '13 at 19:20