From: ImageAnalyst on
Well . . . you're doing it wrong. You're summing the original image,
not the binary image, like you said in your first post. So you're
only getting the number of pixels that are exactly zero in your
original image, not the number of pixels on one side of your
threshold.

Study this demo code and I think you'll understand better.
Be sure to join any lines where the newsreader splits it into two (or
more) lines (i.e., the sprintf line).

% Change the current folder to the folder of this m-file.
% (The line of code below is from Brett Shoelson of The Mathworks.)
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
clc; % Clear command window.
clear all; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

% Read in standard MATLAB gray scale demo image.
grayImage = imread('cameraman.tif');
[rows columns numberOfColorBands] = size(grayImage);
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', 20);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

% Just for fun, let's get its histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.

binaryImage = grayImage > 25;
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);

numberOfBlackPixels = sum(sum(binaryImage == 0));
numberOfWhitePixels = sum(sum(binaryImage));
totalNumberOfPixels = rows * columns;
percentBlackPixels = 100.0 * numberOfBlackPixels /
totalNumberOfPixels;
percentWhitePixels = 100.0 * numberOfWhitePixels /
totalNumberOfPixels;
message = sprintf('Done!\nTotal number of pixels = %d\nBlack pixels =
%d = %.1f%%\nWhite pixels = %d = %.1f%%', ...
totalNumberOfPixels, numberOfBlackPixels, percentBlackPixels, ...
numberOfWhitePixels, percentWhitePixels);
msgbox(message);
From: JordanPainter Painter on
ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <d0431c1f-4636-474d-88f4-a9db4987f58d(a)y14g2000yqm.googlegroups.com>...
> Well . . . you're doing it wrong. You're summing the original image,
> not the binary image, like you said in your first post. So you're
> only getting the number of pixels that are exactly zero in your
> original image, not the number of pixels on one side of your
> threshold.
>
> Study this demo code and I think you'll understand better.
> Be sure to join any lines where the newsreader splits it into two (or
> more) lines (i.e., the sprintf line).
>
> % Change the current folder to the folder of this m-file.
> % (The line of code below is from Brett Shoelson of The Mathworks.)
> if(~isdeployed)
> cd(fileparts(which(mfilename)));
> end
> clc; % Clear command window.
> clear all; % Delete all variables.
> close all; % Close all figure windows except those created by imtool.
> imtool close all; % Close all figure windows created by imtool.
> workspace; % Make sure the workspace panel is showing.
> fontSize = 20;
>
> % Read in standard MATLAB gray scale demo image.
> grayImage = imread('cameraman.tif');
> [rows columns numberOfColorBands] = size(grayImage);
> subplot(2, 2, 1);
> imshow(grayImage, []);
> title('Original Grayscale Image', 'FontSize', 20);
> set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
>
> % Just for fun, let's get its histogram.
> [pixelCount grayLevels] = imhist(grayImage);
> subplot(2, 2, 2);
> bar(pixelCount);
> title('Histogram of original image', 'FontSize', fontSize);
> xlim([0 grayLevels(end)]); % Scale x axis manually.
>
> binaryImage = grayImage > 25;
> subplot(2, 2, 3);
> imshow(binaryImage, []);
> title('Binary Image', 'FontSize', fontSize);
>
> numberOfBlackPixels = sum(sum(binaryImage == 0));
> numberOfWhitePixels = sum(sum(binaryImage));
> totalNumberOfPixels = rows * columns;
> percentBlackPixels = 100.0 * numberOfBlackPixels /
> totalNumberOfPixels;
> percentWhitePixels = 100.0 * numberOfWhitePixels /
> totalNumberOfPixels;
> message = sprintf('Done!\nTotal number of pixels = %d\nBlack pixels =
> %d = %.1f%%\nWhite pixels = %d = %.1f%%', ...
> totalNumberOfPixels, numberOfBlackPixels, percentBlackPixels, ...
> numberOfWhitePixels, percentWhitePixels);
> msgbox(message);

Thanks for all the help. I've used the code above but cropped out the stuff i didn't need. So i'm left with just counting the percentage of black and white pixels. I'm confused by how it's counting the black and white pixels though. This is just out of interest really because it's part of a project and i need to understand it fully before i feel confident using it. How do i alter where it decides which pixels are black and white? I can't see a threshold in there that's all. Sorry for these basic questions, this is the first time i've used matlab for image analysis.
Jordan.
From: ImageAnalyst on
I think you understand it, you just don't think you do. Yes, in this
example, it's simply the threshold that divides foreground and
background. Change the treshold from 25 to something else in
binaryImage = grayImage > 25;
and you'll change the pixels that are considered foreground (white)
and background (black), which, of course, changes the number of those
pixels also. For example, if your array is [10 20 30 40 50] and I set
a threshold of 25 and am looking to call pixels brighter than 25 as my
foreground then I will have 3 foreground pixels and 2 background
pixels. If I now change the foreground to be 15, I will have 4
foreground pixels and 1 background pixel. If instead I use
binaryImage = grayImage < 25;
then now my foreground is the dark things and I will have 2 foreground
pixels (the 10 and the 20) and 3 background pixels (the 30, the 40,
and the 50).

You might like to run and study my image processing demo:
http://www.mathworks.com/matlabcentral/fileexchange/25157
for more fancy things that you can do. But it's well documented so I
think you should be able to follow along very well.
From: JordanPainter Painter on
Yeah it's all working fine now. I've just got to figure out how to analyse 50 photos at once and then plot the white pixel count for each one. Just a quick question though, since counting the pixels for a new image i've got the pixel count in three stages:

percentBlackPixels(:,:,1) =67.6205


percentBlackPixels(:,:,2) = 97.2661


percentBlackPixels(:,:,3) = 99.9580

Originally this didn't happen so i don't know what i've done to change anything. I was just wondering what it meant. At first i ignored the first two values because they originally came back as zero, but now i'm getting actual values for them it's worried me a bit. Sorry to keep hassling and thanks a lot for the help already. It's been really helpful :)
From: ImageAnalyst on
Depends on what color you define as "white." Maybe you'd like to see
my color detection demo:
http://www.mathworks.com/matlabcentral/fileexchange/26420-simplecolordetection
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: Help with function and syms
Next: Bar graph in Matlab