I am pretty sure that we do not know what your specifications are. For example it gets a bit confusing about which parts of the stem on the right are parts of the foreground leaf and which are parts of a different leaf and we do not know what you want to do about that. Ah, the straight portion near the lower right edge is a bit of a torn part of the leaf so the leaf continues right down to the tube. But is that part of your specifications… we surely do not know.
MATLAB: How to Segment this image efficiently
color segmentationimage analysisimage processingImage Processing Toolboximage segmentationleaf
Related Solutions
Convert to grayscale and then threshold and find the bounding box
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;format compact;fontSize = 20;% Read in the color image.
folder = pwd;baseFileName = 'm2.bmp';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);if ~exist(fullFileName, 'file') % Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName); uiwait(warndlg(errorMessage)); return; endendrgbImage = imread(fullFileName);% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);% Display the original color image.
subplot(2, 3, 1);imshow(rgbImage, []);axis on;title('Original Color Image', 'FontSize', fontSize);drawnow;% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);% Histogram the red channel, but there is a huge spike at 255 so let's not count those.
subplot(2, 3, 2);grayImage = rgb2gray(rgbImage);imshow(grayImage, []);title('Gray Scale version', 'FontSize', fontSize);subplot(2, 3, 3);histogram(grayImage(grayImage<255))grid on;title('Histogram of Gray Scale Image', 'FontSize', fontSize);someThresholdValue = 150;binaryImage = grayImage > someThresholdValue;% Take largest blob
binaryImage = bwareafilt(binaryImage, 1);subplot(2, 3, 4);imshow(binaryImage);grid on;title('Binary Image', 'FontSize', fontSize);% Label the binary image.
labeledImage = bwlabel(binaryImage);props = regionprops(labeledImage, 'BoundingBox');bbox = props.BoundingBoxcroppedImageRGB = imcrop(rgbImage, bbox);subplot(2, 3, 5);imshow(croppedImageRGB);grid on;title('Cropped RGB Image', 'FontSize', fontSize);% Gray scale version of cropped image
croppedImageGray = imcrop(grayImage, bbox);subplot(2, 3, 6);imshow(croppedImageGray);grid on;title('Cropped Gray Scale Image', 'FontSize', fontSize);
Alright, try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;format compact;fontSize = 20;%===============================================================================
% Read in good demo image.
folder = pwd;baseFileName = 'contour.jpg';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);% Check if file exists.
if ~exist(fullFileName, 'file') % File doesn't exist -- didn't find it there. Check the search path for it.
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; endendrgbImage = imread(fullFileName);% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorChannels] = size(rgbImage);if numberOfColorChannels > 1 % It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = rgbImage(:, :, 2); % Take green channel.
else grayImage = rgbImage; % It's already grayscale.
end% Display the image.
subplot(2, 3, 1);imshow(grayImage, []);axis on;title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);% 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.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off') % Threshold the image
binaryImage = grayImage > 128;% Display the image.subplot(2, 3, 2);imshow(binaryImage, []);axis on;title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');% Get rid of huge white surround
binaryImage = imclearborder(binaryImage);% Display the image.subplot(2, 3, 3);imshow(binaryImage, []);axis on;title('Border-cleared Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');% Close the image
se = strel('disk', 2, 0);binaryImage = imclose(binaryImage, se);% Display the image.subplot(2, 3, 4);imshow(binaryImage, []);axis on;title('Closed Image', 'FontSize', fontSize, 'Interpreter', 'None');% Fill holes
binaryImage = imfill(binaryImage, 'holes');% Display the image.subplot(2, 3, 5);imshow(binaryImage, []);axis on;title('Final Image', 'FontSize', fontSize, 'Interpreter', 'None');
Best Answer