2016-06-13 153 views
-1

下面的matlab代碼將圖像分割成許多較小的圖像。然後計算圖像中黑色像素的數量,並將其顯示爲圖片中像素總數的百分比。 example of imageMatlab圖像 - 如何計算白色像素的數量

我的問題是 - 而不是計算黑色像素和顯示百分比,我怎麼能計算白色像素? (基本上相反!)

由於

% 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 
+2

可能的複製[圖像單元陣列的黑色像素百分比](http://stackoverflow.com/questions/35845864/percentage-of-black-pixels-for-a-cell-array-of-images) – Suever

+0

相同的確切邏輯除了將'all(x == 0,3)'改爲'all(x == 255,3)'來代替白色。 – Suever

+1

你還應該學習如何接受答案在SO中工作。最後一個問題,你從來沒有接受答案是有效的。 –

回答

1

這條線:

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

具體地說,指出all(x == 0, 3)表示 「所有顏色通道具有值0」 的部分。你想改變它爲「所有顏色通道具有值1(或255取決於你的圖像上)」

所以基本上,改變01255,deependinf如果您的圖像unit8double