Matlab图像 - 如何计算白色像素的数量

时间:2016-06-13 15:14:48

标签: image matlab pixels

下面的matlab代码将图像分割成许多较小的图像。然后计算图像中的黑色像素数,并将其显示为图片中像素总数的百分比。 example of image

我的问题是 - 如何计算黑色像素并显示百分比,如何计算白色像素? (基本上相反!)

由于

% Divide an image up into blocks (non-overlapping tiles).

clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
workspace;  % Make sure the workspace panel is showing.
fontSize = 20;

% Read the image from disk.
rgbImage = imread('edge-diff.jpg');

% 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 = 400; % Rows in block.
blockSizeC = 400; % 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

percentBlack = cellfun(@(x)sum(sum(all(x == 0, 3))) / (numel(x) / size(x,3)), ca); 


% 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);
        ax2 = subplot(numPlotsR, numPlotsC, plotIndex);
        % Extract the numerical array out of the cell
        % just for tutorial purposes.
        rgbBlock = ca{r,c};
        imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
        [rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
        set(ax2, 'box', 'on', 'Visible', 'on', 'xtick', [], 'ytick', []);
        % Make the caption the block number.
        averageBlack = percentBlack(r,c);
        disp(numPlotsR);
        disp(averageBlack);
        caption = sprintf('Frame #%d of %d\n Percentage information content %0.2f', ...
             plotIndex, numPlotsR*numPlotsC, averageBlack*100);
        title(caption);
        drawnow;
        % Increment the subplot to the next location.
        plotIndex = plotIndex + 1;
    end
end

1 个答案:

答案 0 :(得分:1)

这一行:

percentBlack = cellfun(@(x)sum(sum(all(x == 0, 3))) / (numel(x) / size(x,3)), ca); 

特别是all(x == 0, 3)表示“所有颜色通道的值为0”的部分。您想将其更改为“所有颜色通道的值为1(或255取决于您的图像)”

所以基本上,将0更改为1255,如果您的图片为unit8double

,请加深