How to fill color in the specified area?
MATLAB: How to fill color in the specified area
colorimage processingImage Processing Toolbox
Related Solutions
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; endendrgbImage = 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);
No, that's not right. It's as I told you at first.
When you do
binaryImage = grayImage < 128; % Find dark pixels. Dark = true, 1, white.
you're creating a matrix that is "true" wherever the image is dark . If you sum that, it treats the "true" pixels as 1, and thus, counts them - counts the dark pixels. So you're getting the sum of the "true/1/white" pixels in the binary image which means your getting the count of the dark pixels of the gray scale image. Doing it your complicated way would count the bright pixels. By the way, if you wanted the binary image to be false where the gray scale image was dark, you'd flip the less than sign and then sum the inverse, which is much simpler than doing the multi-step process you did.
binaryImage = grayImage > 128; % Find bright pixels instead of dark pixels.
numDarkPixels = sum(~binaryImage(:)); % Notice I had to invert the image with~.
Best Answer