Borrar filtros
Borrar filtros

How to get black pixels and display them in static text

1 visualización (últimos 30 días)
sofia cirne
sofia cirne el 19 de Jun. de 2017
Comentada: Image Analyst el 24 de Jun. de 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!

Respuesta aceptada

Image Analyst
Image Analyst el 19 de Jun. de 2017
Use a format specifier string in sprintf():
textLabel2 = sprintf('# Black pixels = %d', c);
textLabel = sprintf('Total # of pixels = %d', numPixels);
  7 comentarios
sofia cirne
sofia cirne el 23 de Jun. de 2017
That was it. thank you! its was now!
Image Analyst
Image Analyst el 24 de Jun. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Image Processing Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by