How can I analyze black and white pixels inside a ROI?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Eva Sierra
el 19 de Jul. de 2017
Comentada: Image Analyst
el 24 de Jul. de 2017
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();
0 comentarios
Respuesta aceptada
Image Analyst
el 23 de Jul. de 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 comentarios
Image Analyst
el 24 de Jul. de 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().
Más respuestas (1)
Lipi
el 20 de Jul. de 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 comentarios
Lipi
el 23 de Jul. de 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.
Ver también
Categorías
Más información sobre Convert Image Type en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!