
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
0 个评论
采纳的回答
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):

5 个评论
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!