【问题标题】:How to remove non-barcode region in an image? - MATLAB如何删除图像中的非条形码区域? - MATLAB
【发布时间】:2012-02-22 19:09:34
【问题描述】:

在我做了“imclearborder”之后,条形码周围仍然有一些不需要的物体。如何删除这些对象以隔离条形码?我已经粘贴了我的代码供您参考。

  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;

【问题讨论】:

  • 我认为您正处于必须尝试将其解码为条形码的地步。如果它验证,它是一个条形码,如果不是,它可能不是。
  • 谢谢亚历克斯!但是我将如何编写代码?抱歉,我是 MATLAB 新手
  • 你能上传命令'figure;imshow(c)'的结果图像吗?

标签: image matlab image-processing computer-vision barcode


【解决方案1】:

好吧,我已经在你之前的问题How to find the location of red region in an image using MATLAB? 中解释过了,但是使用了 opencv 代码和输出图像。

与其要求代码,不如尝试自己实现。

下面是接下来要做什么。

1) 将代码中的图像“C”转换为二进制。

2) 应用一些侵蚀来去除小噪音。(这一次,条形码区域也缩小了)

3) 应用膨胀来补偿先前的侵蚀。(大部分噪声将在先前的侵蚀中消除。因此它们不会回来)

4) 在图像中查找轮廓。

5) 找到他们的区域。最有可能的是,具有最大面积的轮廓将是条形码,因为其他东西,如字母、单词等会很小(你可以在你提供的灰度图像中理解它)

6) 选择最大轮廓。区域。为它画一个边界矩形。

它的结果已在您之前的问题中提供。它工作得很好。尝试在 MATLAB 文档的帮助下自己实现它。仅当您遇到不理解的错误时才来这里。

【讨论】:

  • 我使用了腐蚀和膨胀,但仍然有无法移除的对象!! (可能是太大了)不过反正有些物体靠近条码加入,腐蚀和膨胀是没有用的!!
  • 您将不得不多次使用腐蚀和膨胀。有用。您可以在之前的答案中看到结果。与您提供的图片相同。
【解决方案2】:

     %%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,[]);

希望有用

【讨论】:

  • 结果的结果是全白??
  • 您好,我无法上传我的结果,但如果您可以给我发送任何邮件 ID,我将向您发送我通过一组图像获得的程序和结果..
  • 您只需通过我的电子邮件地址给我发电子邮件就可以了!谢谢!!
猜你喜欢
  • 2012-05-01
  • 2018-01-01
  • 2021-12-21
  • 1970-01-01
  • 2014-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多