How can I decide if it's dark or bright (barcode in image)

1 view (last 30 days)
Hey,
I'm doing a project and I have a problem, I have this picture and I need to figure from it a 8X8 barcode of dark (black) and bright (white), here is the picture:
I tried to do median of each block but the values look similar.
Thanks.

Answers (1)

Image Analyst
Image Analyst on 29 Mar 2014
Use blockproc to perform the mean of each block. After that you need to threshold to decide whether a block if bright or dark.
% Demo code to divide the image up into an 8 by 8 grid
% and replace each pixel in the block by the mean,
% of all the gray levels of the pixels in the block.
%
clc;
clearvars;
close all;
workspace;
fontSize = 16;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
if ~exist(folder, 'dir')
% If that folder does not exist, don't use a folder
% and hope it can find the image on the search path.
folder = [];
end
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage)
% Display the original gray scale image.
subplot(2, 1, 1);
imshow(grayImage, []);
axis on;
caption = sprintf('Original Gray Scale Image\n%d rows by %d columns', ...
rows, columns);
title(caption, 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Image Analysis Demo','numbertitle','off')
% Block process the image to replace every pixel in the
% 8 pixel by 8 pixel block by the mean of the pixels in the block.
% The image is 256 pixels across which will give 256/8 = 32 pixel by 32 pixel blocks.
meanFilterFunction = @(theBlockStructure) mean2(theBlockStructure.data(:));
blockSizeRows = floor(rows/8);
blockSizeCols = floor(columns/8);
blockSize = [blockSizeRows, blockSizeCols]
blockyImage32 = blockproc(grayImage, blockSize, meanFilterFunction);
[rowsOut, columnsOut] = size(blockyImage32);
% Display the block mean image.
subplot(2, 1, 2);
imshow(blockyImage32, []);
axis on;
grid on;
caption = sprintf('Block Mean Image\nInput block size = %d rows by %d columns\n%d rows by %d columns', ...
blockSizeRows, blockSizeCols, rowsOut, columnsOut);
title(caption, 'FontSize', fontSize);

Community Treasure Hunt

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

Start Hunting!