counting pixels within an image file

2 views (last 30 days)
I have an image and I'm trying to count the number of white and non white pixels of an image. The original image is 16-bit TIF but I converted to 8-bit JPG so I could upload the file.
The code I'm using is below. I'm just a few days with using MATLAB and I'm having issues getting just this simple piece of code working. I get errors starting on line 3
originalImageX = imread('10mgml.tif');
originalImage = rgb2gray(originalImageX);
imagedetails = ifinfo;
display (imagedetails)
binaryImage = originalImage ;
binaryImage = imfill(binaryImage, 'holes');
labeledImage = bwlabel(binaryImage, 8);
blobMeasurements = regionprops(labeledImage, originalImage, 'EulerNumber');
numberOfBlobs = size(blobMeasurements, 1);
The errors that I'm getting.
Error using rgb2gray>parse_inputs (line 80)
MAP must be a m x 3 array.
Error in rgb2gray (line 52)
isRGB = parse_inputs(X);
Error in TestscriptforMatt (line 3)
originalImage = rgb2gray(originalImageX);
Thanks for the help everyone.

Accepted Answer

Guillaume
Guillaume on 5 Jun 2017
Most likely, your image is already greyscale. What is
size(originalImageX)
?
  2 Comments
Benjamin Baynard
Benjamin Baynard on 5 Jun 2017
YOu nailed it. I was checking for the grey scale when it was already grey scale. I've scraped this piece of code and found a better version. Thanks everyone.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 5 Jun 2017
originalImageX is already gray scale. Don't call regb2gray() automatically. Do this:
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
This calls it only if it's a color image and should avoid the error.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!