How to get black pixels and display them in static text
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
sofia cirne
il 19 Giu 2017
Commentato: Image Analyst
il 24 Giu 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 Commenti
Risposta accettata
Image Analyst
il 19 Giu 2017
Use a format specifier string in sprintf():
textLabel2 = sprintf('# Black pixels = %d', c);
textLabel = sprintf('Total # of pixels = %d', numPixels);
7 Commenti
Image Analyst
il 24 Giu 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
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!