How do i mask green pixels?
8 views (last 30 days)
Show older comments
Yoon ThiriZaw
on 3 Jun 2018
Commented: Image Analyst
on 2 May 2020
i am doing plant disease detection and classification. i do preprocessing step, then image segmentation. In segmentation, i do background removal in RGB image using color thresholder app in matlab. Then, i will do masking green pixels, the pixels where the level of green is higher than red and blue are identified and removed by application of the mask. This is based on the fact that these green pixels most probably represent healthy areas in the leaves. Thus, after removing the background and green pixels, the region left in an image is the region of our interest. How do i mask green pixels? Please, answer me! This is my main code:
a = imread('LB.jpg');
subplot(2,3,1);
imshow(a);title('Input Image');
b = rgb2gray(a);
subplot(2,3,2);
imshow(b);title('Grey Image');
c = medfilt2(b,[3 3]);
subplot(2,3,3);
imshow(c);title('Filtered Image using 3*3 window');
[bw,rgb] = background_removal(a);
subplot(2,3,4);
imshow(bw);title('Binary Image');
subplot(2,3,5);
imshow(rgb);title('Background Removed');
And, this code is background removing using color thresholder in matlab:
% Auto-generated by colorThresholder app on 26-Mar-2018
%-------------------------------------------------------
function [BW,maskedRGBImage] = createMask(RGB)
% Convert RGB image to chosen color space
RGB = im2double(RGB);
cform = makecform('srgb2lab', 'AdaptedWhitePoint', whitepoint('D65'));
I = applycform(RGB,cform);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.408;
channel1Max = 99.533;
% Define thresholds for channel 2 based on histogram settings
channel2Min = -27.701;
channel2Max = 14.325;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 5.282;
channel3Max = 50.539;
% Create mask based on chosen histogram thresholds
BW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
% 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;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/190996/image.png)
Thanks!
1 Comment
Accepted Answer
KALYAN ACHARJYA
on 3 Jun 2018
Edited: KALYAN ACHARJYA
on 3 Jun 2018
% code %Concept Learned from Image Analyst Sir k=imread('flower.jpg'); mask=(k(:,:,2)>k(:,:,1)) & (k(:,:,2)>k(:,:,3)); threshold_image=bsxfun(@times, k, cast(imcomplement(mask), 'like', k)); subplot(121),imshow(k); subplot(122),imshow(threshold_image);
![](/matlabcentral/answers/uploaded_files/119995/44.jpg)
5 Comments
Sudipto Dhar
on 2 May 2020
dear yoon, could you find the way of masking? can you share your code with me? I need to get some basic idea from it. thank you.
Image Analyst
on 2 May 2020
Try the Color Thresholder app on the Apps tab of the tool ribbon. Or else see My File Exchange
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!