Using same thresholding in different images to create binary

4 views (last 30 days)
Hello all,
I am on the process to analyze RGB pictures of grass cover. We grew the grass homogeneusly before applying some treatments. I used the color thresholding app to create threshold for the baseline (homogeneous cover) filtering out small black spaces between leaves and consider all the rest as green. From this I extracted this code:
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 17-Mar-2023
%------------------------------------------------------
% Convert RGB image to chosen color space
I = RGB;
% Define thresholds for channel 1 based on histogram settings
channel1Min = 35.000;
channel1Max = 109.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 50.000;
channel2Max = 149.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 23.000;
channel3Max = 131.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
Now I want to use those treshold to identify green area in the treatments. Some grass is alive and green some other dead and brown. How can I apply this threshold to other images to have binary of them all so I can transform them in a matrix for further analysis? Is there any color detection app or code I can use?
Thank you all in advance for any suggestion.
Cheers

Answers (1)

Ranjeet
Ranjeet on 12 Apr 2023
Hi Enrico,
Since, you already have exported the createMask function by thresholding a baseline image having grass cover, you can use the function with other RGB images as the output of the function is a binary (BW) along with RGB (maskedRGBImage) image.
Here is a mask function that I exported by using some example images with grass cover and tuned to filter green patches, you may use this or try experimenting with the Color Thresholder App.
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 11-Apr-2023
%------------------------------------------------------
% Convert RGB image to chosen color space
I = RGB;
% Define thresholds for channel 1 based on histogram settings
channel1Min = 38.000;
channel1Max = 187.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 82.000;
channel2Max = 255.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 188.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!