How to get black pixels and display them in static text
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
sofia cirne
le 19 Juin 2017
Commenté : Image Analyst
le 24 Juin 2017
I have an gray scale image and i want to know the total number of pixels, the number of black pixels, and display them in static text GUI. But i can't get the results to show in the static text.
a = handles.grey;
b = handles.imgData;
B = img2double(b);
A = im2double(a);
% black pixels
c = sum(A(:) == 0);
textLabel2 = sprintf(c);
set(handles.areaporos, 'String', textLabel2);
% total pixels
numPixels = numel(B);
textLabel = sprintf(numPixels);
set(handles.areatotal, 'String', textLabel);
Can someone help me figure out what I'm doing wrong? Thank you!
0 commentaires
Réponse acceptée
Image Analyst
le 19 Juin 2017
Use a format specifier string in sprintf():
textLabel2 = sprintf('# Black pixels = %d', c);
textLabel = sprintf('Total # of pixels = %d', numPixels);
7 commentaires
Image Analyst
le 24 Juin 2017
Use this code to make sure the image is gray scale, if you need to make sure it is:
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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!