Image Processing - How to separate barcode from the background

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 日

2 投票

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 日

0 投票

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 日
Please post your code.
noosa
noosa 2012 年 1 月 25 日
I am trying to run your code and I get that error linked to area command. How can I solve it please?
Thank you Chandra for your quick response !
Chandra Kurniawan
Chandra Kurniawan 2012 年 1 月 25 日
Hi, your error message said 'Error using ==> area at 44 Must supply Y data or X and Y data as first argument(s).'
It seem you have named your m-file with 'area.m', right?
However in your script 'area' was a variable.
Please try to rename your script with other name.
noosa
noosa 2012 年 1 月 25 日
No I haven't name my file 'area.m', I named it 'barcode'
I got this error message because area is already a default command in matlab, so I renamed it 'v', I got this error:
??? Undefined function or variable 'v'.
Error in ==> barcode at 16
[C I] = min(v);
Then I have thought to check length(stat), I am surprised that it return 0 !
So the loop
for cnt = 1 : length(stat)
v(cnt) = stat(cnt).BoundingBox(3) * stat(cnt).BoundingBox(4);
end
will not work.
Do you know why length(stat) returns 0 ??
Chandra Kurniawan
Chandra Kurniawan 2012 年 1 月 25 日
Yes, I see the problem.
before line 'stat = regionprops(Iarea,'boundingbox');'
you should do area opening by 'Iarea = bwareaopen(Ibw,25000);', right?
It seem bwareaopen remove all object in your picture.
In other word, all objects in your image have area less than 25000.
That's the reason why length(stat) returns 0.
noosa
noosa 2012 年 1 月 25 日
oh I see ! so how to fix the parameter 25000? is there any automatic method?
Chandra Kurniawan
Chandra Kurniawan 2012 年 1 月 25 日
Yes, just decrease the value.
Eq 24000 or less.
noosa
noosa 2012 年 1 月 25 日
But I need to do this with many images, so I can't change the value each time :((( that's why I asked for automated method to determine this threshold !
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 日

0 投票

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

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

質問済み:

Kim
2011 年 12 月 22 日

回答済み:

2015 年 12 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by