-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry.m
More file actions
66 lines (55 loc) · 1.95 KB
/
try.m
File metadata and controls
66 lines (55 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
clear;
close all;
clc;
% Load image
img = imread('objects1.png');
% Convert image to HSV color space
hsvImg = rgb2hsv(img);
figure,
imshow(hsvImg);
% Define color ranges for objects of interest
colorRanges = {[0, 0.15; 0.8, 1; 0.6, 1], ... % red
[0.5, 0.7; 0.4, 1; 0.4, 1], ... % orange
[0.1, 0.2; 0.4, 1; 0.4, 1], ... % yellow
[0.2, 0.5; 0.4, 1; 0.4, 1], ... % green
[0.5, 0.7; 0.4, 1; 0.4, 1], ... % blue
[0, 0.1; 0.5, 0.6; 0.0, 1]}; ... % brown
% Create masks for each object of interest
colorMasks = cell(1, length(colorRanges));
for i = 1:length(colorRanges)
colorMasks{i} = createColorMask(hsvImg, colorRanges{i});
end
% Combine masks into a single binary image
binaryImg = cat(3, colorMasks{:});
binaryImg = any(binaryImg,3);
figure,
imshow(binaryImg);
% Remove small objects from binary image
binaryImg = bwareaopen(binaryImg, 40);
% Fill the holes to make a solid object
BW2 = imfill(binaryImg,'holes');
figure,
imshow(BW2);
% Count objects in binary image and get their properties
props = regionprops(binaryImg, 'BoundingBox');
% Display original image with red bounding boxes around counted objects
figure;
imshow(img);
hold on;
for i = 1:length(props)
bb = props(i).BoundingBox;
rectangle('Position', bb, 'EdgeColor', 'r', 'LineWidth', 2);
label = ['Object ', num2str(i)];
text(bb(1), bb(2)-10, label, 'Color', 'r', 'FontSize', 11);
end
hold off;
% Display binary image with number of objects in title
numObjects = length(props);
title(['Number of Objects: ', num2str(numObjects)]);
% Function to create color mask for a given color range
function mask = createColorMask(img, colorRange)
hueMask = (img(:,:,1) >= colorRange(1,1)) & (img(:,:,1) <= colorRange(1,2));
satMask = (img(:,:,2) >= colorRange(2,1)) & (img(:,:,2) <= colorRange(2,2));
valMask = (img(:,:,3) >= colorRange(3,1)) & (img(:,:,3) <= colorRange(3,2));
mask = hueMask & satMask & valMask;
end