在附加的代码中,程序将图像分成块并尝试计算每个块的每行中的白色像素的数量。原始图片如下。
问题在于代码有效地计算整个图像中的像素的次数,即我将图像分割的次数而不是每个单独的块。
如果有人对如何使该程序识别图像的每个块(我认为这是主要问题)有任何改进,那么它将解决这个问题。
谢谢!
:::: EDIT ::::
我已经解决了我的问题,然而这是一个新的挑战(耶)。此代码的结果给出了输出:
rows =
148
217
475
521
567
724
thickness =
69
258
46
46
157
plotindex = 2, c=2, r=1
rows =
148
217
475
596
650
716
thickness =
69
258
121
54
66
我想存储"厚度"数组中的值(列中分隔白色像素的黑色像素数),以便稍后处理此数据。
%%%%%%%%%%%%%%%%%%%%%%%
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
rgbImage = imread('http://i.imgur.com/gnmVFLq.png');
%just any image for reference
% Display image full screen.
imshow(rgbImage);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
drawnow;
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage)
%==========================================================================
% The first way to divide an image up into blocks is by using mat2cell().
blockSizeR = 844; % Rows in block.
blockSizeC = 665; % Columns in block.
% Figure out the size of each block in rows.
% Most will be blockSizeR but there may be a remainder amount of less than that.
wholeBlockRows = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, wholeBlockRows), rem(rows, blockSizeR)];
% Figure out the size of each block in columns.
wholeBlockCols = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, wholeBlockCols), rem(columns, blockSizeC)];
% Create the cell array, ca.
% Each cell (except for the remainder cells at the end of the image)
% in the array contains a blockSizeR by blockSizeC by 3 color array.
% This line is where the image is actually divided up into blocks.
if numberOfColorBands > 1
% It's a color image.
ca = mat2cell(rgbImage, blockVectorR, blockVectorC, numberOfColorBands);
else
ca = mat2cell(rgbImage, blockVectorR, blockVectorC);
end
% Now display all the blocks.
plotIndex = 1;
numPlotsR = size(ca, 1);
numPlotsC = size(ca, 2);
for r = 1 : numPlotsR;
for c = 1 : numPlotsC;
fprintf('plotindex = %d, c=%d, r=%d\n', plotIndex, c, r);
% Specify the location for display of the image.
subplot(numPlotsR, numPlotsC, plotIndex);
% Extract the numerical array out of the cell
rgbBlock = ca{r,c};
imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
[rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
% Make the caption the block number.
caption = sprintf('Block #%d of %d\n%d rows by %d columns', ...
plotIndex, numPlotsR*numPlotsC, rowsB, columnsB);
title(caption);
drawnow;
% Increment the subplot to the next location.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
imBinary = double(im2bw(rgbBlock)); %//Convert to binary,
%// then make double for sum
histogram = sum(imBinary,2); %// Compute row-wise histograms
stem(1:size(imBinary,1), histogram); %// Histogram of white pixels
xlabel('Row number');
ylabel('White pixel count');
grid;
diffs = diff([0; histogram]);
threshold = 100; %// Threshold that determines whether it is black or white
rows = find(diffs >= threshold)
thickness = diff(rows)
plotIndex = plotIndex + 1;
end
end