How to calculate percentage of green and yellow in the image

10 visualizaciones (últimos 30 días)
I did segmentation of green and yellow color from a picture by creating the function.
After that, I need to calculate the percentage of green and yellow. I am not sure how to do that
Greenfunction is the one I segmented green, Yellowfunction is the one I segmented yellow,
I=imread("0_0_0.png");
imshow(I);
RGB=im2uint8(I);
[BW,maskedRGBImage] = Greenfunction(RGB)
figure;
imshow(maskedRGBImage)
[BW,maskedRGBImage] = Yellowfunction(RGB)
figure;
imshow(maskedRGBImage)

Respuesta aceptada

Image Analyst
Image Analyst el 14 de En. de 2022
Editada: Image Analyst el 14 de En. de 2022
Try this (untested)
rgbImage = imread("peppers.png");
subplot(2, 2, 1);
imshow(rgbImage);
RGB = im2uint8(rgbImage);
[greenMask,maskedRGBImage] = Greenfunction(RGB);
subplot(2, 2, 2);
imshow(maskedRGBImage)
[yellowMask,maskedRGBImage] = Yellowfunction(RGB);
subplot(2, 2, 3);
imshow(maskedRGBImage)
% Compute percentages.
greenPercentage = 100 * nnz(greenMask) / numel(greenMask);
yellowPercentage = 100 * nnz(yellowMask) / numel(yellowMask);
  2 comentarios
Xinzheng Chen
Xinzheng Chen el 14 de En. de 2022
You are a life saver, this code works perfectly
Thank you for teaching me
Image Analyst
Image Analyst el 14 de En. de 2022
You're welcome, and thanks for Accepting the answer. I just made an edit correction (copy/paste error) on the last line. It should be
yellowPercentage = 100 * nnz(yellowMask) / numel(yellowMask);
instead of
yellowPercentage = 100 * nnz(greenMask) / numel(yellowMask);
but I guess you figured that out.

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by