How to find the Feret Diameters of a leaf

2 ビュー (過去 30 日間)
nkumar
nkumar 2013 年 3 月 20 日
編集済み: Image Analyst 2023 年 6 月 4 日
I have images of a leaf. Now I want to find the diameter of the leaf, and the size of the leaf in the image.
Kindly assist.

回答 (1 件)

Image Analyst
Image Analyst 2023 年 6 月 4 日
Did you search the tags for leaf? Because there are lots of questions about analyzing leaves. Here is a solution for a single leaf:
% Demo to find area and Feret extents of a leaf.
% By Image Analyst.
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 = 18;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = pwd;
baseFileName = 'leaf.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
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the RGB image full size.
subplot(2, 1, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original 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';
% Threshold the image to find the leaf.
blueChannel = rgbImage(:, :, 3); % Faster than rgb2gray().
mask = ~imbinarize(blueChannel);
% Fill the blobs.
mask = imfill(mask, 'holes');
% Take the largest blob only.
mask = bwareafilt(mask, 1);
% Display the binary image.
subplot(2, 1, 2);
imshow(mask, []);
axis('on', 'image');
caption = sprintf(' Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Get the area 2 different ways.
area1 = bwarea(mask) % Weights according to the shape of the local outline.
area2 = sum(mask(:)) % This is a simple pixel count.
% Get the area (pixel count) and Feret diameters.
props = regionprops(mask, 'Area', 'MaxFeretProperties', 'MinFeretProperties')
caption = sprintf('Mask Image. Area = %d pixels. MaxFeretDiameter = %.1f pixels',...
props.Area, props.MaxFeretDiameter);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Draw a line from one extreme end to the other along the long direction.
x = props.MaxFeretCoordinates(:, 1)
y = props.MaxFeretCoordinates(:, 2)
hold on;
markerSize = 40;
plot(x, y, 'r.-', 'LineWidth', 3, 'MarkerSize', markerSize)
% Draw a line across the narrow dimension.
x = props.MinFeretCoordinates(:, 1)
y = props.MinFeretCoordinates(:, 2)
hold on;
plot(x, y, 'r.-', 'LineWidth', 3, 'MarkerSize', markerSize)

Community Treasure Hunt

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

Start Hunting!

Translated by