필터 지우기
필터 지우기

How to get black pixels and display them in static text

조회 수: 1 (최근 30일)
sofia cirne
sofia cirne 2017년 6월 19일
댓글: Image Analyst 2017년 6월 24일
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!

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by