![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173950/image.png)
How to calculate the %defects in leaf image
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Kumar Arindam Singh
el 14 de Abr. de 2017
Comentada: Sajitha K.N.
el 5 de Feb. de 2020
I have this binary image.I want to find the area of the leaf and the area of defects(holes) in the leaf.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/162943/image.jpeg)
Thanx.....
0 comentarios
Respuesta aceptada
Image Analyst
el 14 de Abr. de 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));
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173950/image.png)
3 comentarios
Sajitha K.N.
el 5 de Feb. de 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
Más respuestas (1)
linson vincent
el 3 de En. de 2018
Editada: linson vincent
el 3 de En. de 2018
wonderful
0 comentarios
Ver también
Categorías
Más información sobre Image Processing and Computer Vision en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!