How to get black pixels and display them in static text
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
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 件のコメント
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 Center および File Exchange で Image Processing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!