How to find the green area?

조회 수: 15 (최근 30일)
Epah
Epah 2013년 10월 23일
댓글: Image Analyst 2017년 9월 1일
I am trying to find the area within the green boundaries. How should I go about with the codes?
  댓글 수: 2
Image Analyst
Image Analyst 2013년 10월 23일
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 2013년 10월 23일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2013년 10월 23일
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
Epah
Epah 2013년 10월 25일
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 2013년 10월 27일
편집: Image Analyst 2013년 10월 27일
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];

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

aredhel_vlsi
aredhel_vlsi 2014년 2월 19일
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
Image Analyst
Image Analyst 2014년 2월 19일
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

댓글을 달려면 로그인하십시오.


riya reetu
riya reetu 2017년 9월 1일
편집: riya reetu 2017년 9월 1일
Could you please give me code for Detection of roadside vegetation

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by