How to count the number of white pixels
28 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
filename = 'untitled.jpg';
I = imread(filename);
figure, imshow(I);
sum(I(:) == 255)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/996375/image.jpeg)
ans = 592185
and
filename = 'untitled2.jpg';
I = imread(filename);
figure, imshow(I);
sum(I(:) == 255)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/996380/image.jpeg)
ans = 626229
....
This is strange that figure1 has much lower white pixels, because i'm agricultural student, so i do not know well about matlab, coding,,,
What is wrong about my script??
0 commentaires
Réponse acceptée
Chunru
le 13 Mai 2022
First, you should use gray scale image.
Second you have a large white margin in your data (which is not seen from imshow). You can see the margin if you use imagesc instead.
If you need to count the white pixels inside. You can clip the image to remove the white margin first.
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/996375/image.jpeg';
I = imread(filename);
I = rgb2gray(I);
figure, imagesc(I); colorbar; %imshow(I);
sum(I(:) >= 250)
sum(I(200:600, 200:600)>250, 'all')
figure; histogram(I(:))
%whos
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/996380/image.jpeg';
I = imread(filename);
I = rgb2gray(I);
figure, imagesc(I); colorbar; %imshow(I);
figure; histogram(I(:))
sum(I(:) >= 250)
sum(I(200:600, 200:600)>250, 'all')
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!