2016-06-13 5 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

가능한 중복 [이미지의 셀 배열에 대한 검정 픽셀의 비율] (0120)을 참조하십시오. – Suever

+0

동일한 정확한 논리 'all (x == 0, 3)'을'all (x == 255, 3)'으로 변경하는 대신에 흰색을 찾습니다. – Suever

+1

당신은 또한 대답을 받아들이는 것이 어떻게 작동 하는지를 배웁니다. 마지막 질문으로, 당신은 정답을 정당한 것으로 결코 받아들이지 않았습니다. –

답변

1

이 줄 : all(x == 0, 3)는 "모든 색상 채널 값 0이"의미라고

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

특히 일부입니다. 당신은 "모든 색상 채널 값 1 (또는 255 이미지에 따라 다름)"그래서 기본적으로

을에, 변경 변경하려는 01 또는 255, deependinf 이미지 인 경우 unit8 또는 double

관련 문제