how i can segment the red area from the leaf

2 次查看(过去 30 天)
can anybody help how i can segment the read area in two pictures , this area is a disease please can any body help

采纳的回答

Mark Sherstan
Mark Sherstan 2018-11-14
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 个评论
alaa shamasneh
alaa shamasneh 2018-11-15
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
Mark Sherstan
Mark Sherstan 2018-11-16
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Agriculture 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by