How to calculate the %defects in leaf image

5 次查看(过去 30 天)
I have this binary image.I want to find the area of the leaf and the area of defects(holes) in the leaf.
Thanx.....

采纳的回答

Image Analyst
Image Analyst 2017-4-14
See attached 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;
% Check that user has the specified Toolbox installed and licensed.
hasLicenseForToolbox = license('test', 'image_toolbox'); % license('test','Statistics_toolbox'), license('test','Signal_toolbox')
if ~hasLicenseForToolbox
% User does not have the toolbox installed, or if it is, there is no available license for it.
% For example, there is a pool of 10 licenses and all 10 have been checked out by other people already.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in a gray scale demo image.
folder = pwd;
baseFileName = '001bin.jpg';
% Get the full filename, with path prepended.
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
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale 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')
% Binarize the image
binaryImage = grayImage < 128;
% Fill holes.
binaryImage = imfill(binaryImage, 'holes');
% Get the leaf area
leafArea = sum(binaryImage(:))
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image, leaf mask', 'FontSize', fontSize, 'Interpreter', 'None');
% Get the defects
defectImage = grayImage > 128; % Binarize
% Get rid of background
defectImage = imclearborder(defectImage);
% Display the image.
subplot(2, 2, 3);
imshow(defectImage, []);
title('Defects', 'FontSize', fontSize, 'Interpreter', 'None');
% Get the defect area
defectArea = sum(defectImage(:))
message = sprintf('The leaf area = %d pixels.\nThe defect Area = %d pixels = %.1f%%',...
leafArea, defectArea, defectArea/leafArea*100);
uiwait(msgbox(message));
  3 个评论
Sajitha K.N.
Sajitha K.N. 2020-2-5
I also have this same problem. But this code is not working. I converted my rgb image into LAB color space. Then I seperated each channel. I want to select 'a' or 'b' channel according to some function. After this selected channel image, I want to apply this method. Please help me

请先登录,再进行评论。

更多回答(1 个)

linson vincent
linson vincent 2018-1-3
编辑:linson vincent 2018-1-3
wonderful

类别

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