Image Processing - How to making the Background black leaving barcode as white
1 view (last 30 days)
Show older comments
I'm new to Matlab. I wondering how to make the background black and leaving the barcode white. I read on some noise elimaination technique that this step can detect the barcode. I have tried and error but cannot make it fully. Anybody has any ideal?
Noise Elimination Techniques
1. Clear any group/s of pixels that are touching the edge/border of the image
2. Clear any group/s of pixels less than 100 pixels^2 in area for image sizes > [300 300] or 50 pixels^2 for those smaller.
3. Clear any group/s of pixels that are not straight (since eccentricity of a straight line is 1).
code:
rgb = imread('bar1.jpg');
rgb = imresize(rgb,0.33);
Igray= rgb2gray(rgb);
Ibw = im2bw(Igray, graythresh(Igray));
Ibw = ~Ibw
Iarea = bwareaopen(Ibw,10);
imshow(Iarea))
Original Image <http://www.mediafire.com/?o1hwqz954l85usi>
0 Comments
Accepted Answer
Image Analyst
on 23 Dec 2011
1. imclearborder()
2. bwareaopen()
3. use regionprops to get coordinates of each bar, then use polyfit to see how well it fits to a line.
7 Comments
More Answers (1)
Image Analyst
on 29 Dec 2011
Kim, your whole idea was flawed from the start. Now that I can see your image I can tell that you first want to do color segmentation on it before you do thresholding. See this demo. Once you have the bar code alone, then you can use Hough or some other ad hoc method to find the lines and their thicknesses.
workspace; % Make sure the workspace panel is showing.
fontSize = 18;
% Read in a standard MATLAB gray scale demo image.
folder = 'D:\Temporary stuff';
baseFileName = 'bar1.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Find white objects.
thresholdValue = 128;
binaryImage = redChannel > 128 & greenChannel > thresholdValue & blueChannel > thresholdValue;
subplot(2, 2, 2);
imshow(binaryImage);
title('Initial Binary Image', 'FontSize', fontSize);
% Fill in bar code background.
binaryImage = imfill(binaryImage, 'holes');
% Remove regions touching border.
binaryImage = imclearborder(binaryImage);
% Get rid of regions less than 200000 pixels in area.
binaryImage = bwareaopen(binaryImage, 200000);
% Display the cleaned up image.
subplot(2, 2, 3);
imshow(binaryImage);
title('Final Binary Image', 'FontSize', fontSize);
% Find left and right columns.
% Find top and bottom rows.
verticalProfile = any(binaryImage, 2);
topRow = find(verticalProfile, 1, 'first');
bottomRow = find(verticalProfile, 1, 'last');
% Find left and right columns.
horizontalProfile = any(binaryImage, 1);
leftColumn = find(horizontalProfile, 1, 'first');
rightColumn = find(horizontalProfile, 1, 'last');
% Mask out the bars
barOnly = greenChannel .* uint8(binaryImage);
barOnly(~binaryImage) = 255;
% Crop out the image
barOnly = barOnly(topRow:bottomRow, leftColumn:rightColumn);
subplot(2, 2, 4);
imshow(barOnly);
title('Cropped, Masked Bar-Only Image', 'FontSize', fontSize);
msgbox('Now use hough, houghlines, or other methods to find the lines and their thicknesses.');
7 Comments
vidyasagar S D
on 13 Oct 2019
can u please share the image used for it . the link is broken and not working
Image Analyst
on 13 Oct 2019
After 8 years, no one has it anymore. If you have a problem with your own image, please post it and your code in a new question.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!