Image Processing - How to separate barcode from the background

조회 수: 8 (최근 30일)
Kim
Kim 2011년 12월 22일
답변: mostafa alhamdany 2015년 12월 4일
I'm wondering how to separate the barcode from background itself, and I have tried some different to remove the unwanted object but cannot remove fully , anybody has an idea!
%Read Image
RGB = imread('barcode.jpg');
%Resize Image
RGB = imresize(RGB, 0.33);
%Convert from RGB to Gray
Gray = rgb2gray(RGB);
%Threshold Image to black and white
Threshold = graythresh(Gray);
BW = im2bw(Gray, Threshold);
%Remove Noise
BW2 = bwareaopen(BW,25000);
imshow(BW2)

채택된 답변

Chandra Kurniawan
Chandra Kurniawan 2011년 12월 22일
Hello, Kim
Here I have the code to do segmentation on your barcode image.
I hope this helps
rgb = imread('barcode.jpg');
rgb = imresize(rgb,0.33);
Igray= rgb2gray(rgb);
Ibw = im2bw(Igray, graythresh(Igray));
Iarea = bwareaopen(Ibw,25000);
Iarea = imfill(Iarea,'holes');
stat = regionprops(Iarea,'boundingbox');
for cnt = 1 : length(stat)
area(cnt) = stat(cnt).BoundingBox(3) * stat(cnt).BoundingBox(4);
end
[C I] = min(area);
Icropped = imcrop(Ibw,stat(I).BoundingBox);
imshow(Icropped);
Original Image :
Cropped Image :
  댓글 수: 2
Kim
Kim 2011년 12월 22일
Just nice!! This is what I need!! Thanks! :)
Malta
Malta 2013년 11월 11일
Why is there an error like this? Error using area (line 42) Must supply Y data or X and Y data as first argument(s).

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

noosa
noosa 2012년 1월 25일
I am trying to run this code but I get this error :
??? Error using ==> area at 44 Must supply Y data or X and Y data as first argument(s).
Can you help me please???
  댓글 수: 10
Chandra Kurniawan
Chandra Kurniawan 2012년 1월 25일
Just find the largest area and then use that value as threshold
Eq:
I = imread('cameraman.tif');
I = im2bw(I);
stat = regionprops(I,'area');
C(1:length(stat)) = stat(1:end).Area;
m = max(C);
J = bwareaopen(I,m);
imshow(J);
noosa
noosa 2012년 1월 25일
thank you ! I will try it :))

댓글을 달려면 로그인하십시오.


mostafa alhamdany
mostafa alhamdany 2015년 12월 4일
hi this code is not working and the error is : Error using area (line 35) Must supply Y data or X and Y data as first argument(s).
Error in barcode (line 15) [C I] = min(area); can anyone help me to solve it , please

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by