how do i fill in the shapes

20 views (last 30 days)
Daniel
Daniel on 16 Feb 2026 at 18:00
Edited: Walter Roberson on 17 Feb 2026 at 20:07
how do i fill in the outline of the shapes so i can recognise them later
subplot(3,2,1)
imshow(img);
title(sprintf('Loaded (Colour): %s', filename));
% Convert to HSV and show Image Saturation (helps separate coloured regions)
HSV = rgb2hsv(img);
S = HSV(:,:,2); % Extract the saturation channel
% Tweek saturation levels to remove shadow
gamma = 3; % started at value 1. keep adjusting up to remove shadow
S_tweaked = S .^ gamma;
subplot(3,2,2)
imshow(S_tweaked, []);
title('Saturation (gamma adjusted)');
% Show grayscale conversion
GRAY = im2gray(S_tweaked);
subplot(3,2,3)
imshow(GRAY);
title('Grayscale Image');
se2 = strel("sphere",10);
erode = imerode(GRAY, se2);
subplot(3,2,4);
imshow(erode);
title("erode");
% Convert to Binary
BW = imbinarize(erode);
se4 = strel("disk",10);
BW = imclose(BW, se4);
BW = imclearborder(BW);
subplot(3,2,6)
imshow(BW);
title('Binary Image');
% Invert Binary
BW_inv = ~BW;
BW_inv = imfill(BW_inv,'holes');
subplot(3,2,7)
imshow(BW_inv);
title('Inverted Binary Image');
  1 Comment
Daniel
Daniel on 17 Feb 2026 at 8:02
Moved: Image Analyst on 17 Feb 2026 at 16:00
this is a project and these images needs to be used. my end goal is to write a program that can identify the shapes in the images. currently im trying to get the shapes egdes straight so when i try to identify them its easier.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst about 6 hours ago
Edited: Image Analyst about 6 hours ago
If you're ultimately trying to say that "this shape is a yellow triangle" and "this shape is a blue square" then some of what you're doing is OK and some of what you're doing is unnecessary, and there is other stuff that you need to do but you're not doing it.
For simple shapes like these generally you want to identify the shapes first, like from the hue image, and then extract the hue value "MeanIntensity" with regionprops. Then you need to find out reference values for pure yellow, blue, red, etc. and find out which of those values is closest to your extracted value. This will tell you the reference color your shape is closest to.
Then you need to identify the shape. You can do this by counting the number of vertices there are. You can do this by getting the coordinates of the perimeter with bwboundaries and the centroid with regionprops and computing the distance between the perimeter and the centroid for every pixel on the perimeter. Now plot that distance and you'll see a curve with some peaks (except for the circle where all the distances are about the same). You need to find and count the peaks. There will be a peaks wherever there is a vertex. There are a variety of ways you can do that but you can use findpeaks if you have the Signal Processing Toolbox. Or you can normalize the plot with rescale and thereshold and then count the peaks with bwlabel.
I already have two demos on shape identification and I'm attaching them. Try to adapt them to your images and see how they work.

Image Analyst
Image Analyst on 16 Feb 2026 at 18:31
I have no idea what "how do i fill in the outline of the shapes so i can recognise them later " means. What do you really want to do? You're doing some color segmentation and some morphological operations but I'm not sure what the overall goal here is. What is the final answer/result that you want?
Also, your image capture environment is terrible, mostly because of the shadows. Try rearranging your lighting to minimize the shadows.
If you have any more questions, then attach your input images with the paperclip icon after you read this:

Community Treasure Hunt

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

Start Hunting!