Clear Filters
Clear Filters

How can I analyze black and white pixels inside a ROI?

7 views (last 30 days)
I am working with an image and I would like to select a ROI and analyse the pixels inside. Inside that ROI, I have black and white pixels. I would like to know the number of black pixels inside the ROI and the percentage of black pixels in that area. I was using binaryImage but then, I have all pixels white inside. Can anyone help me? This is my code:
[file, pathname] = uigetfile('*.jpg','Load Image');
a=imread(file);%color image
% Get the number of rows and columns, and, most importantly, the number of color channels.
[rows, columns, numberOfColorChannels] = size(a);
if numberOfColorChannels > 1
% It's a true color RGB image. We need to convert to gray scale.
Igray = rgb2gray(a);
end
% Show picture converted into gray.
imshow(Igray, []);
fontSize = 16;
% Draw the region with pixels you're interested in
hFH = imfreehand();
% Create a binary image ("mask") from the ROI object.
binaryImage = hFH.createMask();

Accepted Answer

Image Analyst
Image Analyst on 23 Jul 2017
You said that Igray has only black and white pixels - no "gray" pixels in between. Like 0 and 1 or 0 and 255. You can use this code to count the number of black and white pixels within the mask ("binaryImage") only
% Get a vector -- a 1-D list -- of pixel values in the mask.
pixelsInsideMask = Igray(binaryImage);
numTotalPixels = length(pixelsInsideMask);
numWhitePixels = nnz(pixelsInsideMask); % Count of number of non-zero pixels
numBlackPixels = numTotalPixels - numWhitePixels;
  2 Comments
Eva Sierra
Eva Sierra on 24 Jul 2017
Thanks for your answer, Image Analyst. I think I made a mistake. I converted the coloured image using rgb2gray, so I think I have gray pixels too. With your code, I can now select the pixels inside the ROI but number of black pixels is 0 and the amount of white pixels equals the total amount (exploring the data, all my values are 255). How can I solved this? Sorry, I'm not expierienced using Matlab. Thanks in advance!
Image Analyst
Image Analyst on 24 Jul 2017
So, since you have no pure black, or pure white pixels, but a bunch of gray pixels, you can get the relative counts of each gray level using imhist() or histogram().

Sign in to comment.

More Answers (1)

Lipi
Lipi on 20 Jul 2017
I understand you are want to analyze the number of black pixels in your ROI. The binary mask that you are creating basically gives you white pixels in the ROI. You would still need to apply the mask to your image in order to extract the region from your image. Some code that you might want to use following your code (once the binary mask is created):
>> Imask = Igray;
>> Imask(~(binaryImage)) = 255; % This would apply the mask to extract ROI from your original image
You can then view the image using:
>> imshow(Imask)
Then for analyzing the black pixels, you could use the following:
>> black=nnz(~Imask) % Imask would have values of zero for black elements. Taking the inverse of that would mean black pixels are non-zero. We simply count the number of non-zero elements.
For getting the percentage, you could simply divide this count of black pixels by the total number of elements in your ROI (which could be obtained from your mask, or number of ones in your 'binaryImage').
  3 Comments
Lipi
Lipi on 23 Jul 2017
For obtaining the total number of pixels in your ROI only, you will have to count number of white pixels in 'binaryImage' (logical 'ones' in your variable since they represent your ROI). The following portion of the code that you are using
nWhite = sum(Imask(:));
seems to be an incorrect usage of the function 'sum'. That function sums up values of the elements and does not count them.
Regarding your number of black pixels coming out to zero, if there are gray pixels that you have in your image and you want to count them, you will have to define a range of values in which the grey ones will lie. MATLAB gives a value to black pixels as 0 and white as 255. All the ones in between would be gray. To count purely black you can count the zeros, as for the grey ones you will have to decide which range of values you want to consider.
You might want to read this article on how MATLAB represents grayscale images: https://www.mathworks.com/company/newsletters/articles/how-matlab-represents-pixel-colors.html
For counting white pixels in your ROI you might have to perform some manipulation of 'Imask'. Since 'Imask' has the white pixels outside your ROI also, you could invert the mask so that all your white pixels become black. You could then count non-black pixels in your 'Imask'. Subtracting that number from number of pixels in your ROI (obtained above) will give you white pixels in your ROI.
Eva Sierra
Eva Sierra on 24 Jul 2017
Thank you for the answer. I'm not experienced with Matlab so I will try to follow your instructions and see if I have the correct solution

Sign in to comment.

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!