Looking for help with image- processing.
3 views (last 30 days)
Show older comments
I am working a program that is supposed to correctly identify and label shapes in an image (square, triangle, circle, rectangle). I am working with a dataset of 9 images:
The dataset includes 2D images of shapes and 3D images of shapes.
The outline for the program is as follows:
- Reading images and pre-processing to workable level.
- Identify non accident properties and segment regions of concavity.
- Develop equations to define geometric primitives.
- Matching components to objective representation and object identification.
I have tried many techniques already in order to achieve this program including using equations to identify the shapes but i have not had much luck. On my lates version of my code I am using blob analysis which has enabled me to identify the 2D shapes with about 80% accuracy. I am also having a lot of trouble with the 3D shapes as they are all different colour and they create shadows and some are touching eachother.
I am fairly new to MatLab and coding in general. I am not sure if I am going the right way about it with the code I am currenty using. If anyone could look at my code and the dataset and maybe give me some example code to achieve the goal of the program, that would be a great help.
Unfortunately, I was not able to upload my code as a file or the dataset (MatLab told me i have exceeded my daily upload of 10 files even though have never uploaded a file before). I have included the link to the dataset above and the code is below.
% Read in the image file
originalImage = imread('1.png');
% Extract the red, green, and blue color channels
redChannel = originalImage(:, :, 1);
greenChannel = originalImage(:, :, 2);
blueChannel = originalImage(:, :, 3);
% Threshold values to isolate objects effectively
levelr = 0.20;
levelg = 0.20;
levelb = 0.31;
% Binarise each layer and add them together
redBinarize = imbinarize (redChannel, levelr);
greenBinarize = imbinarize (greenChannel, levelg);
blueBinarize = imbinarize (blueChannel, levelb);
rgbSum = (redBinarize&greenBinarize&blueBinarize);
% Compliment the image and fill in holes (Black as background, white as
% foreground)
icomp = imcomplement (rgbSum);
% Edge detection using Canny method
edgeImage = edge(icomp, 'Canny');
% Morphological operations to enhance edge detection
se = strel('square', 3);
edgeImage = imdilate(edgeImage, se);
edgeImage = imfill(edgeImage, 'holes');
% Blob analysis to find regions and their properties
blobAnalysis = regionprops(edgeImage, 'all');
% Loop through each region and label shapes
figure;
imshow(icomp);
hold on;
for k = 1:length(blobAnalysis)
% Get region properties
thisBlob = blobAnalysis(k);
perimeter = thisBlob.Perimeter;
area = thisBlob.Area;
circularity = 4 * pi * area / perimeter^2;
centroid = thisBlob.Centroid;
% Determine shape based on circularity
if circularity >= 0.9
% Blob is circular
shape = 'circle';
elseif circularity < 0.9 && circularity >= 0.7
% Blob is square or rectangle
ratio = thisBlob.MinorAxisLength / thisBlob.MajorAxisLength;
if ratio >= 0.9 && ratio <= 1.1
% Blob is square
shape = 'square';
else
% Blob is rectangle
shape = 'rectangle';
end
elseif circularity < 0.7 && circularity >= 0.5
% Blob is triangle
shape = 'triangle';
else
% Blob is irregular
shape = 'irregular';
end
% Label shape on image
text(centroid(1), centroid(2), shape, 'Color', 'r', 'FontWeight', 'bold');
end
1 Comment
Rena Berman
on 25 May 2023
(Answers dev) @zero, I have attached the images so you can reference them and run your code in the editor.
Answers (0)
See Also
Categories
Find more on Tracking and Motion Estimation 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!