4

After I did a 'imclearborder', there are still a bit of unwanted object around the barcode. How can I remove those objects to isolate the barcode? I have pasted my code for your reference.

Original Image imshow(C) Processed Image

  rgb = imread('barcode2.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;
3
  • I think you're at the point where you have to try to decode it as a barcode. If it validates, it's a barcode, if not, it probably wasn't.
    – Alex
    Jan 31, 2012 at 2:36
  • Thanks Alex! but how am I going to write the code?? Sorry I am new to MATLAB
    – Kim
    Jan 31, 2012 at 2:42
  • Can you upload the resulting image of the command 'figure;imshow(c)'? Jan 31, 2012 at 8:56

2 Answers 2

1

Well, i have already explained it in your previous question How to find the location of red region in an image using MATLAB? , but with a opencv code and output images.

Instead of asking for code, try to implement it yourself.

Below is what to do next.

1) convert image 'C' in your code to binary.

2) Apply some erosion to remove small noises.( this time, barcode region also shrinks)

3) Apply dilation to compensate previous erosion.(most of noise will have removed in previous erosion. So they won't come back)

4) Find contours in the image.

5) Find their area. Most probably, contour which has maximum area will be the barcode, because other things like letters, words etc will be small ( you can understand it in the grayscale image you have provided)

6) Select contour with max. area. Draw a bounding rectangle for it.

Its result is already provided in your previous question. It works very nice. Try to implement it yourself with help of MATLAB documentation. Come here only when you get an error which you don't understand.

2
  • I used erosion and dilation but there are still object which cannot be removed!! (Maybe it is too big) But anyway, some object is join close to the barcode and erosion and dilation will not have any use!!
    – Kim
    Feb 1, 2012 at 5:29
  • you will have to use erosion and dilation multiple times. It works. And you can see result in previous answer. It is same image you provided. Feb 1, 2012 at 8:12
0

Input Image Result

     %%hi, i am ading my code to yours at the end of your code%%%%
     clear all;  

     rgb = imread('barcode.jpeg');
     % Resize Image
     rgb = imresize(rgb,0.33);
     figure(),imshow(rgb);
     % Convert from RGB to Gray
       Igray = double(rgb2gray(rgb));
      Igrayc = Igray;
       % Calculate the Gradients
      [dIx, dIy] = gradient(Igray);
      B = abs(dIx) - abs(dIy);
      % Low-Pass Filtering
      H = fspecial('gaussian', 10, 5);
      C = imfilter(B, H);

      C = imclearborder(C);
      imshow(Igray,[]);
      figure(),imagesc(C);colorbar;


      %%%%%%%%%%%%%%%%%%%%%%%%from here my code starts%%%%%%%%%%%%%%%%
      bw = im2bw(C);%%%binarising the image
      %  imshow(bw);

        %%%%if there are letters or any other noise is present around the barcode
        %%Note: the size of the noise and letters should be smaller than the
        %%barcode size

        labelImage = bwlabel(bw,8);
        len=0;labe=0;
        for i=1:max(max(labelImage))
        a = find(labelImage==i);
        if(len<length(a))
        len=length(a);
        labe=i;
        end
        end

       imag = zeros(size(l));
       imag(find(labelImage==labe))=255;

      %  imtool(imag);

      %%%if Necessary do errossion 
     %  se2 = strel('line',10,0);
     %  imag= imerode(imag,se2);
     %  imag= imerode(imag,se2);

       [r c]= find(imag==255);

      minr = min(r);
      maxc = max(c);
      minc = min(c);
      maxr = max(r);
      imag1 = zeros(size(l));

     for i=minr:maxr
     for j=minc:maxc

      imag1(i,j)=255;
    end

    end


   %  figure,imtool(imag1);

  varit = find(imag1==0);

    Igrayc(varit)=0;
   %%%%%result image having only barcode
   imshow(Igrayc,[]);
   %%%%%original image 
   figure(),imshow(Igray,[]);

Hope it is useful

3
  • The outcome of the result isit all white??
    – Kim
    Feb 1, 2012 at 3:27
  • Hi, i am not able to upload my results but if u can send me any mail id i will send you the program and results i got over a set of images..
    – G453
    Feb 1, 2012 at 4:49
  • You just email me thru my email address will do!! Thanks!!
    – Kim
    Feb 1, 2012 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.