How to find the green area?

16 views (last 30 days)
Epah
Epah on 23 Oct 2013
Commented: Image Analyst on 1 Sep 2017
I am trying to find the area within the green boundaries. How should I go about with the codes?
  2 Comments
Image Analyst
Image Analyst on 23 Oct 2013
Why are you starting a new thread instead of continuing your prior duplicate thread? Even if you didn't like my answer, you don't need to start a new discussion for others to answer.
Epah
Epah on 23 Oct 2013
Image Analyst, I apologise for the new thread. The answer that you have provided me on the previous thread does not help me with my matlab code. Thank you.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 23 Oct 2013
All right, well let's start all over again here.
Do you have the coordinates of the green dots on the perimeter? I think you must because you put the green dots into the overlay, or someone did. If you don't want to use concave hull, then alpha shapes will certainly scare you off, so another approach we can take is morphology. Take the green coordinates and make it a binary image, or if someone else gave you this image and won't tell you the green coordinates, then you can extract it from the image. But with morphology because some of the green dots are spaces quite far apart compared to the others, to get those to close the gap we need to use a large structuring element which will distort the boundary. It won't be exact like the convave hull solution I gave you in your duplicate question. Nonetheless, here is a morphological solution.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
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 a color demo image.
folder = 'C:\Users\Sharifah\Documents\Temporary';
baseFileName = 'Screen Shot 2013-10-23 at 8.18.51 PM.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
axis on;
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
binaryImage = greenChannel >= 250 & redChannel == 0;
% Display the binary image.
subplot(2, 2, 2);
imshow(binaryImage);
title('Binary Image', 'FontSize', fontSize);
% Do a morphological closing on it.
se = strel('disk',15);
closedImage = imclose(binaryImage, se);
% Fill the image.
filledImage = imfill(closedImage, 'holes');
% Display the binary image.
subplot(2, 2, 3);
imshow(filledImage);
title('Closed, filled Image', 'FontSize', fontSize);
area = bwarea(filledImage)
message = sprintf('The Area = %.3f pixels', area);
msgbox(message);
  2 Comments
Epah
Epah on 25 Oct 2013
Hi Image Analyst,
I tried to use regionprops to find the Area and MajorAxisLength, but the computed result is not in pixels but instead in struct array(what does this means?) :
results = regionprops(filledImage, 'Area', 'MajorAxisLength');
results =
8x1 struct array with fields:
Area
MajorAxisLength
May I know what is the error? Is there any other way to find the major length axis? Thank you.
Image Analyst
Image Analyst on 27 Oct 2013
Edited: Image Analyst on 27 Oct 2013
That is not an error. You have an array of structures. Each element is a structure that describes one single object with whatever measurements you asked for. You can extract them all into individual arrays if you want:
allAreas = [results.Area];
allMajorAxesLengths = [results.MajorAxesLength];

Sign in to comment.

More Answers (2)

aredhel_vlsi
aredhel_vlsi on 19 Feb 2014
Hello people, I would like to locate the green pixels of the image and extract their position in the image. For example at this image : folder = fullfile(matlabroot, '\toolbox\images\imdemos'); baseFileName = 'peppers.png';
I would like to remove the green peppers, and know their position at my image. Could you please help me in that ? Is there any tool I could use? I don't have skills at Image processing, I just want to use it for extracting information. I have managed so far to read the image, imageshow the R,G,B channels but I am stuck now. Please help !Thank you in advance !
  1 Comment
Image Analyst
Image Analyst on 19 Feb 2014
Look at my color segmentation demos. It's simple matter to adapt the thresholds in the demo to pick out green. http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862

Sign in to comment.


riya reetu
riya reetu on 1 Sep 2017
Edited: riya reetu on 1 Sep 2017
Could you please give me code for Detection of roadside vegetation

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!