Can I get automatic image segmentation code for the leaf?

 Respuesta aceptada

Use the Color Thresholder App like I did:
% Demo to find leaf. By Image Analyst, Nov. 1, 2020.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'leaf1.jpeg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% It's not an RGB image! It's an indexed image, so read in the indexed image...
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
[mask, maskedRGBImage] = createMask(rgbImage);
% Take just the largest regions:
mask = bwareafilt(mask, 1);
% Fill Holed.
mask = imfill(mask, 'holes');
% Display the initial mask image.
subplot(2, 2, 2);
imshow(mask, []);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
axis('on', 'image');
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
% Display the final masked image.
subplot(2, 2, 3);
imshow(maskedRgbImage, []);
axis('on', 'image');
title('Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display the final masked image of the background by inverting the mask.
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
backgroundImage = bsxfun(@times, rgbImage, cast(~mask, 'like', rgbImage));
subplot(2, 2, 4);
imshow(backgroundImage, []);
axis('on', 'image');
title('Background Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
%-------------------------------------------------------------------------------------------------------------
% Make measurements
props = regionprops(mask, 'Area', 'Centroid')
allAreas = [props.Area];
xyCentroids = vertcat(props.Centroid);
subplot(2, 2, 2);
hold on;
for k = 1 : length(props)
x = xyCentroids(k, 1);
y = xyCentroids(k, 2);
txt = sprintf(' (x, y) = (%.1f, %.1f). Area = %d', ...
x, y, allAreas(k));
text(x, y, txt, 'Color', 'r', 'FontWeight', 'bold');
plot(x, y, 'r+', 'MarkerSize', 25, 'LineWidth', 2);
end
% Get boundary.
boundaries = bwboundaries(mask);
boundaries = boundaries{1}; % Extract from cell.
x = boundaries(:, 2);
y = boundaries(:, 1);
plot(x, y, 'r-', 'LineWidth', 3);
fprintf('Done running %s.m ...\n', mfilename);
msgbox('Done!');
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 01-Nov-2020
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.183;
channel1Max = 0.400;
% 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 = 1.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

17 comentarios

Manoj - I haven't heard from you. DId you try this demo I spent time putting together for you? Did it work or not? If it worked, can you please "Accept this answer"?
The above code is working properly sir. But in mask image getting that red line and +(x,y)=(8020.7,644.4). Are this formula as you shown in the image. that should not come i need background black and sharp edges like how you have shown in background image.
That was optional. Just delete everything after this line
% Make measurements
and it won't call plot() and won't make the red annotations. Regardless if you plot the red or not, the mask variable is still the mask - it's just a binary image with no red annotation on it. The red was just in the overlay, not in the mask image of course.
To be more precise that what i have mentioned in the above image i need that kind of sharp edges in the below image without red colour and formula
Yes, I understood. That's why I said to delete everything at the end of the script so that you don't call bwboundaries(), text(), plot(), etc. Looks like you have not done that yet. It's so easy to do (simply highlight and hit the delete key) that I did it for you and it's in the attached m-file.
Sir got it but please i need sharp edges in mask that what you have shown in background image sharp edges. please help me out in this work.
Not sure what you mean. The edge of the mask, where it goes from black to white, are sharp. It changes over a single pixel. The edge is not blurry at all. Please explain more explicitly what you need and why what I did does not create the mask you need.
I know that i have given a lot of work to you sorry for that and thank for your assistance sir.
how we segment these types of images
I have no idea what you want to find there. You might have to take a deep learning approach, or else just just hand-trace the thing you want with drawfreehand().
sir i want code for autosaving the leaf. like if a camera taking an image, that image should be save automatically.
sir i want code for autosaving the leaf. like if a camera taking an image, that image should be save automatically. sir and i want that masked image background in gray color. Can you tell how to change the background color in gray.
You can call imwrite() right after you call getsnapshot() to save the image array variable to disk.
Once you have the mask, you can make it 3D and then assign all background pixels to gray.
backgroundMask3d = ~cat(3, mask, mask, mask); % Background will be "true".
rgbImage(backgroundMask3d) = 128; % Assign to gray level 128.
Sir I want the code to segment the nail image from the finger with automatic segmentation then it store and display the four nail image in single window
If you want to do something with hand images, why would you
  • hide your question in an old unrelated thread about leaves
  • not provide any information about the image(s) or any specific requirements?
Wouldn't you think it makes more sense to ask a question and provide enough information that people can understand the scope of what you're asking about?

Iniciar sesión para comentar.

Más respuestas (2)

Categorías

Más información sobre Agriculture en Centro de ayuda y File Exchange.

Preguntada:

el 1 de Nov. de 2020

Comentada:

el 2 de En. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by