Azzera filtri
Azzera filtri

How to calculate the %defects in leaf image

1 visualizzazione (ultimi 30 giorni)
I have this binary image.I want to find the area of the leaf and the area of defects(holes) in the leaf.
Thanx.....

Risposta accettata

Image Analyst
Image Analyst il 14 Apr 2017
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 Commenti
Kumar Arindam Singh
Kumar Arindam Singh il 15 Apr 2017
thank you...it works perfectly fine
Sajitha K.N.
Sajitha K.N. il 5 Feb 2020
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

Accedi per commentare.

Più risposte (1)

linson vincent
linson vincent il 3 Gen 2018
Modificato: linson vincent il 3 Gen 2018
wonderful

Categorie

Scopri di più su Image Processing and Computer Vision in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by