[fname path]=uigetfile('*.jpg','select an image'); fname=strcat(path,fname); im=imread(fname); grayImage = imread(fname); % 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 % Display the image. imshow(grayImage, []); title('Original Grayscale Image', 'FontSize', 20, 'Interpreter', 'None'); % Assuming grayImage is a binary image... numWhitePixels = nnz(grayImage); % Sum of non-zero pixels. numPixels = numel(grayImage); numBlackPixels = numPixels - numWhitePixels
why do not white pixels equal black pixels
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
mengqi zhang
el 9 de En. de 2017
I'm sure the picture is half black and half white, but when I program the code, the whitepixels are not equal blackpixels why??
[fname path]=uigetfile('*.jpg','select an image');
fname=strcat(path,fname);
im=imread(fname);
grayImage = imread(fname);
% 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
% Display the image.
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', 20, 'Interpreter', 'None');
% Assuming grayImage is a binary image...
numWhitePixels = nnz(grayImage); % Sum of non-zero pixels.
numPixels = numel(grayImage);
numBlackPixels = numPixels - numWhitePixels
<<
<<

>>
>>
3 comentarios
Adam
el 9 de En. de 2017
Why are you 'sure'?
The obvious answer would just be that you are wrong in your assumption!
Respuesta aceptada
Stephen23
el 9 de En. de 2017
Editada: Stephen23
el 9 de En. de 2017
Both your assumptions and your code are wrong. The pixels are not exclusively black or white, because there are also many many gray pixels in that image:
>> I = imread('photo.jpg');
>> U = unique(I(:));
>> H = histc(I(:),U);
>> [uint32(U),H]
ans =
0 6000
1 234000
54 1200
253 1200
254 1200
255 236400
The first column shows the RGB value, and the second column shows how many occurrences of each value are in that image.
This is a good example of how beginners often have some belief about what code is doing. But they do not bother to actually check. Code does not care is in your head, nor what you imagine it to be doing. Your job is to check what it is actually doing. Just because you imagine that all pixels are exactly black or white does not mean that they are. And any code that made that assumption would be pretty awful code!
0 comentarios
Más respuestas (0)
Ver también
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!