How to remove an area without a barcode in the image? - MATLAB

After I made "imclearborder", there is still a bit of objectionable object throughout the barcode. How to remove these objects to isolate the barcode? I applied my code for reference.

Original imageimshow (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; 
+4
source share
2 answers

Well, I already explained this in the previous question. How to find the location of the red area in the image using MATLAB? but with opencv code and image output.

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

What follows next is what follows.

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

2) Apply some erosion to remove small noise (this time the barcode area is also compressed)

3) Apply dilatation to compensate for previous erosion (most of the noise will be removed with previous erosion, so they will not return)

4) Find the contours of the image.

5) Find your area. Most likely, the outline that has the maximum area will be a barcode, because other things, such as letters, words, etc., will be small (you can understand this in the grayscale image that you provided)

6) Select a circuit with max. area. Draw a bounding box for it.

Its result is already presented in your previous question. It works very well. Try to implement it yourself using the MATLAB documentation. Only come here when you get an error that you don’t understand.

+1
source

Input imageResult

  %%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); [rc]= 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 this is helpful

0
source

Source: https://habr.com/ru/post/1393875/


All Articles