From ed1c0ca5328c4e2bb41be8f9d4e94ad73dd4c8e4 Mon Sep 17 00:00:00 2001 From: omegion Date: Sun, 6 Jan 2019 14:23:44 +0100 Subject: [PATCH] Update README.md --- README.md | 6 ++++++ main.m | 18 +++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 89db91f..f018d8f 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ # MatLab - Primitive Object Detection + +This small project detects the circular and rectangular objects in the given image. + +## Example + +![example](https://i.imgur.com/YIDd9P2.png) diff --git a/main.m b/main.m index 8a2dd14..be2a77e 100644 --- a/main.m +++ b/main.m @@ -1,9 +1,9 @@ -clear all; close all; clc; % clear console and close all windows +clear all; close all; clc; % clear everything im = imread('objects.jpg'); % circles are greater than rectangles -% im = imread('objects_1.jpg'); % circles and rectangles are equal +%im = imread('objects_1.jpg'); % circles and rectangles are equal -[h, w, ~] = size(im); % get height and width of the image +[h, w, ~] = size(im); % get height and width of image distR = double(im(:, :, 1)) - 0; % since we have only one dimension, first dimension of image will be enough @@ -17,7 +17,6 @@ results = bwmorph(results, 'open', 2); results = bwmorph(results, 'close', 2); - stats = regionprops(results, 'BoundingBox'); % this gets the stats of each object figure, imshow(results); @@ -25,16 +24,17 @@ circle_num = 0; rec_num = 0; + for i = 1 : length(stats) dimensions = stats(i).BoundingBox; - text = sprintf(int2str(i)); % put the iteration number to variable to print as string + inserted_text = sprintf(int2str(i)); % put the iteration number to variable to print as string - if dimensions(3) == dimensions(4) % 3 and 4 stand for height and width of object, if the values are equal it is circle otherwise ellipse + if dimensions(3)== dimensions(4) % 3 and 4 stand for height and width of object, if the values are equal it is circle otherwise ellipse circle_num = circle_num+1; - hText = text(dimensions(1) + dimensions(3)/2,dimensions(2)+dimensions(4)/2,text,'Color',[1 0 1],'FontSize',15); % put the pink string to image - elseif dimensions(3) ~= dimensions(4) + hText = text(dimensions(1) + dimensions(3)/2,dimensions(2)+dimensions(4)/2,inserted_text,'Color',[1 0 1],'FontSize',15); %put the pink string to image + elseif dimensions(3)~= dimensions(4) rec_num = rec_num+1; - hText = text(dimensions(1) + dimensions(3)/2,dimensions(2)+dimensions(4)/2,text,'Color',[0.1 0 0.5],'FontSize',15); % put the blue string to image + hText = text(dimensions(1) + dimensions(3)/2,dimensions(2)+dimensions(4)/2,inserted_text,'Color',[0.1 0 0.5],'FontSize',15); %put the blue string to image end rectangle('Position', stats(i).BoundingBox, 'EdgeColor', 'g', 'LineWidth', 3); % draw a rectangle