MATLAB: How to find the green area

image processingimage segmentation

I am trying to find the area within the green boundaries. How should I go about with the codes?

Best Answer

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);