How can I Segment this image efficiently

조회 수: 10 (최근 30일)
Daniel Allkenmach
Daniel Allkenmach 2015년 12월 14일
댓글: Image Analyst 2015년 12월 30일

So I've been trying everything in order to get an automated system where the leaf is left by itself, but nothing has met to my specifications. How can I segment an image with the entire leaf part being left over? heres an example image:

  댓글 수: 2
Walter Roberson
Walter Roberson 2015년 12월 14일
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.
Daniel Allkenmach
Daniel Allkenmach 2015년 12월 19일
편집: Walter Roberson 2015년 12월 19일
Heres a better image.
All I would like to do is segment the image in its entirety

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2015년 12월 28일
Daniel, try this code:
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 image.
rgbImage=imread('8.jpg');
subplot(2,2,1);
imshow(rgbImage);
title('Original RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Threshold
% Convert RGB image to chosen color space
hsvImage = rgb2hsv(rgbImage);
% Define thresholds for Hue based on histogram settings
hueMin = 0.139;
hueMax = 0.356;
% Define thresholds for Saturation based on histogram settings
saturationMin = 0.451;
saturationMax = 1.000;
% Create mask based on chosen histogram thresholds
mask = (hsvImage(:,:,1) >= hueMin ) & (hsvImage(:,:,1) <= hueMax) & ...
(hsvImage(:,:,2) >= saturationMin ) & (hsvImage(:,:,2) <= saturationMax);
% Fill holes.
mask=imfill(mask,'holes');
% Extract biggest blob.
mask = bwareafilt(mask, 1);
subplot(2,2,2);
imshow(mask)
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
% Display final masked image
subplot(2,2,3);
imshow(maskedRgbImage)
title('Masked RGB 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')
  댓글 수: 2
Daniel Allkenmach
Daniel Allkenmach 2015년 12월 29일
I'm blown away. Thank you
Image Analyst
Image Analyst 2015년 12월 30일
If you think this is the best answer, then can you go ahead and Accept it? Thanks in advance.
Be sure to check out the color thresholder app on the Apps tab.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2015년 12월 19일
I have done leaves so many times before for people. Surely you can find some of my code and adapt it to your images. See http://www.mathworks.com/matlabcentral/answers/?term=tag%3A%22leaf%22
Or else look for more general color segmentation routines in my File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
  댓글 수: 6
Image Analyst
Image Analyst 2015년 12월 19일
I've uploaded a new version that uses line() instead of stem() because for some reason MATLAB release R2015b did not like how I used stem even though release R2014, that I originally developed the code with, was perfectly happy with the way I used stem().
Sorry about that, but sometimes new releases of MATLAB can break old versions of code that worked perfectly fine in the older MATLAB release.
Thanks for letting me know and please download and try the new version and let me know if it works now. Make sure your version has line() and not stem() in the function called PlaceThresholdBars().
Image Analyst
Image Analyst 2015년 12월 19일
Be sure to also try out the color thresholder app on the App tab of the toolbar ribbon.

댓글을 달려면 로그인하십시오.


harjeet singh
harjeet singh 2015년 12월 22일
hello dear use this code for extraction
clear all
close all
clc
img=imread('leaf.jpg');
figure(1)
imshow(img)
drawnow
img=double(img);
img_1=(img(:,:,2)-img(:,:,1)>1 & img(:,:,2)-img(:,:,1)<40 & img(:,:,3)<90);
img_1=imfill(img_1,'holes');
figure(2)
imshow(img_1)
drawnow
[lab,num]=bwlabel(img_1);
for i=1:num
[r,c]=find(lab==i);
a(i,:)=[i length(r)];
end
a=sortrows(a,2);
roi=lab==a(end,1);
img_3(:,:,1)=img(:,:,1) .* double(roi);
img_3(:,:,2)=img(:,:,2) .* double(roi);
img_3(:,:,3)=img(:,:,3) .* double(roi);
img_3=uint8(img_3);
figure(3)
imshow(img_3)
drawnow
  댓글 수: 3
Image Analyst
Image Analyst 2015년 12월 27일
The for loop and the "a" variable and all that stuff about labeling is unnecessary. It could be done more simply like this:
rgbImage=imread('8.jpg');
subplot(2,2,1);
imshow(rgbImage)
% Threshold
img_1=(rgbImage(:,:,2)-rgbImage(:,:,1)>1 & rgbImage(:,:,2)-rgbImage(:,:,1)<40 & rgbImage(:,:,3)<90);
% Fill holes.
img_1=imfill(img_1,'holes');
% Extract biggest blob.
img_1 = bwareafilt(img_1, 1);
subplot(2,2,2);
imshow(img_1)
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(img_1, 'like', rgbImage));
subplot(2,2,3);
imshow(maskedRgbImage)
This is better but this is not very robust. Later I'll post a more robust method using thresholding in HSV color space.
harjeet singh
harjeet singh 2015년 12월 28일
thanks @image analyst

댓글을 달려면 로그인하십시오.

카테고리

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