counting pixels within an image file

2 次查看(过去 30 天)
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.

采纳的回答

Guillaume
Guillaume 2017-6-5
Most likely, your image is already greyscale. What is
size(originalImageX)
?
  2 个评论
Benjamin Baynard
Benjamin Baynard 2017-6-5
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.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2017-6-5
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.

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by