how i can segment the red area from the leaf

can anybody help how i can segment the read area in two pictures , this area is a disease please can any body help

 Accepted Answer

I would reccomend using the Color Thresholder App which is part of the Image Processing Toolbox. You can load in an image either from a GUI or from your workspace (use imread).
Use HSV color space and adjust your V value as you want the darker region.
Your MATLAB script would look something like this:
I = imread('image1.jpg');
[BW,maskedRGBImage] = createMask(I);
imshow(BW)
Where the createMask function is autogenerated from the 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 13-Nov-2018
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 1.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 0.236;
% 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
The result is (in black and white):
image.png
From there you can use regionprops to get all the information you need.

5 Comments

thanks for your answer , but what about the seconed image same code not working on it
if there is any code can segment the second image , the second image have different disease from the first one
and can you help me how i can using regionprops her to extract features from the segmented area , cause i want to use this feature for next step as input for nueral network.
I had to modify the channels slightly. The results still arent perfect and I will leave it up to you to optimize the values based on the remainder of your data. My results currently look like following (theres one image on the left and another on the right):
Screen Shot 2018-11-14 at 9.14.18 AM.png
To get information such as area, bounding box, etc... run commands such as:
stats2.Area
stats2.BoundingBox
etc...
The updated code is as follows:
clear all
close all
% Read in images and display
I1 = imread('image1.jpg');
I2 = imread('image2.jpg');
figure
imshowpair(I1, I2, 'montage')
% Mask for areas of interest, fill any holes, and only consider areas
% larger than 50 pixels
BW1 = createMask(I1);
BW2 = createMask(I2);
BW1 = imfill(BW1,'holes');
BW2 = imfill(BW2,'holes');
BW1 = bwareafilt(BW1,[50 inf]);
BW2 = bwareafilt(BW2,[50 inf]);
figure
imshowpair(BW1, BW2, 'montage')
% Find information regarding the areas of interest
stats1 = regionprops(BW1);
stats2 = regionprops(BW2);
And the updated masking function:
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 14-Nov-2018
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.040;
channel1Max = 0.129;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.507;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.122;
channel3Max = 0.425;
% 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
Thank you so much mark.
sir do you know if i have group of picture how i can let the code read all images and apply a mask on it using loop
cause its diffecult to make one by one i have more than 150 images thanks
Try something along these lines:
% Read in images
imgFolder = fullfile('images');
imgs = imageDatastore(imgFolder);
numOfImgs = length(imgs.Files);
subPlotColumn = 3;
subPlotRow = ceil(numOfImgs/subPlotColumn);
% View all images
figure(1)
for i = 1:numOfImgs
subplot(subPlotRow,subPlotColumn,i)
I = readimage(imgs,i);
imshow(I)
end
saveas(gcf,'allImages.png')
You can used the saved image and optimize the mask by looking at all the pictures at once or at least a chosen subset of images.
% Threshold the image --> Focus on red
imgs.ReadFcn = @(filename)readAndPreprocessImage(filename);
From the structure of the imgs variable we can use the 'ReadFcn' function handle to read files and process them quickly and effciently (you dont even need to loop here). Look at imageDatastore for more info.
function Iout = readAndPreprocessImage(filename)
img = imread(filename);
[BW,maskedRGBImage] = createMask(img);
% Do other image stuff here
end
And that should more or less solve your problem!

Sign in to comment.

More Answers (0)

Categories

Find more on Agriculture in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!