在我做了'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;
答案 0 :(得分:1)
好吧,我已经在您之前的问题How to find the location of red region in an image using MATLAB?中解释过它,但是使用了opencv代码和输出图像。
而不是要求代码,请尝试自己实现。
下面是下一步该做什么。
1)将代码中的图像'C'转换为二进制。
2)施加一些侵蚀以消除小噪音。(这次,条形码区域也会收缩)
3)应用膨胀来补偿先前的侵蚀(大部分噪音会在以前的侵蚀中消除。所以它们不会再回来了)
4)在图像中找到轮廓。
5)找到他们的区域。最有可能的是,具有最大面积的轮廓将是条形码,因为其他东西,如字母,单词等将很小(您可以在您提供的灰度图像中理解它)
6)选择最大轮廓。区域。为它绘制一个边界矩形。
其结果已在您之前的问题中提供。它非常好用。尝试在MATLAB文档的帮助下自己实现它。只有当你得到一个你不明白的错误时才来这里。
答案 1 :(得分:0)
%%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,[]);
希望它有用