-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathred_colorDetection.m
145 lines (130 loc) · 5.02 KB
/
red_colorDetection.m
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
% Red Color detection
function colorValue = red_colorDetection(currFrame, objFlag, bnw)
colorValue=struct('color','no','redObjectsMask',bnw);
eightBit= true;
if(objFlag==1)
[rgbImage storedColorMap] = imread('dump.jpg');
[rows columns numberOfColorBands] = size(rgbImage);
%subplot(3,3,1);
%imshow(rgbImage);
% If it's monochrome (indexed), convert it to color.
% Check to see if it's an 8-bit image needed later for scaling).
if strcmpi(class(rgbImage), 'uint8')
% Flag for 256 gray levels.
eightBit = true;
else
eightBit = false;
end
if numberOfColorBands == 1
if isempty(storedColorMap)
% Just a simple gray level image, not indexed with a stored color map.
% Create a 3D true color image where we copy the monochrome image into all 3 (R, G, & B) color planes.
rgbImage = cat(3, rgbImage, rgbImage, rgbImage);
else
% It's an indexed image.
rgbImage = ind2rgb(rgbImage, storedColorMap);
% ind2rgb() will convert it to double and normalize it to the range 0-1.
% Convert back to uint8 in the range 0-255, if needed.
if eightBit
rgbImage = uint8(255 * rgbImage);
end
end
end
else
[rgbImage]=currFrame;
[rows columns numberOfColorBands] = size(currFrame);
end
% Extract out the color bands from the original image
% into 3 separate 2D arrays, one for each color component.
redBand = rgbImage(:, :, 1);
greenBand = rgbImage(:, :, 2);
blueBand = rgbImage(:, :, 3);
% Display them.
%subplot(3, 3, 2);
%imshow(redBand);
%title('Red Band');
%subplot(3, 3, 3);
%imshow(greenBand);
%title('Green Band');
%subplot(3, 3, 4);
%imshow(blueBand);
%title('Blue Band');
% Assign the low and high thresholds for each color band.
redThresholdLow = graythresh(redBand);
redThresholdHigh = 255
greenThresholdLow = 0;
greenThresholdHigh = graythresh(greenBand);
blueThresholdLow = 0;
blueThresholdHigh = graythresh(blueBand);
if eightBit
redThresholdLow = uint8(redThresholdLow * 255); %changed from low to high
greenThresholdHigh = uint8(greenThresholdHigh * 255); % High to low
blueThresholdHigh = uint8(blueThresholdHigh * 255);
end
%redThresholdLow;
% Now apply each color band's particular thresholds to the color band
redMask = (redBand >= redThresholdLow) & (redBand <= redThresholdHigh);
greenMask = (greenBand >= greenThresholdLow) & (greenBand <= greenThresholdHigh);
blueMask = (blueBand >= blueThresholdLow) & (blueBand <= blueThresholdHigh);
% % Display the thresholded binary images.
%subplot(3, 3, 5);
%imshow(redMask, []);
%title('Is-Green Mask');
%subplot(3, 3, 3);
% imshow(greenMask, []);
% title('Is-Not-Green Mask');
% subplot(3, 3, 4);
% imshow(blueMask, []);
% title('Is-Not-Blue Mask');
% Combine the masks to find where all 3 are "true."
% Then we will have the mask of only the red parts of the image.
redObjectsMask = uint8(redMask & greenMask & blueMask);
%subplot(3, 3, 4);
%imshow(redObjectsMask, []);
%caption = sprintf('Mask of Only\nThe Red Objects');
%title(caption);
% filter out small objects.
smallestAcceptableArea = 100;
redObjectsMask = uint8(bwareaopen(redObjectsMask, smallestAcceptableArea));
%subplot(3, 3, 5);
%imshow(redObjectsMask, []);
%caption = sprintf('bwareaopen() removed objects\nsmaller than %d pixels', smallestAcceptableArea);
%title(caption);
% Smooth the border using a morphological closing operation, imclose().
structuringElement = strel('disk',4);
redObjectsMask = imclose(redObjectsMask, structuringElement);
%subplot(3, 3, 6);
%imshow(redObjectsMask, []);
%title('Border smoothed');
% Fill in any holes in the regions, since they are most likely red also.
redObjectsMask = uint8(imfill(redObjectsMask, 'holes'));
subplot(3, 3, 5);
imshow(redObjectsMask, []);
%title('Regions Filled');
% Save the binary image in the root folder
redObjectsMask = uint8( redObjectsMask ) * 255;
if(objFlag==1)
imwrite( redObjectsMask, 'red.jpg');
end
% pause(0.03);
% this can return the value of the color in the image and this image can be
% sent to find the shape of the object.
% Measure the mean RGB and area of all the detected blobs.
[meanRGB, areas, numberOfBlobs] = MeasureBlobs(redObjectsMask, redBand, greenBand, blueBand);
if numberOfBlobs > 0
% fprintf(1, '\n----------------------------------------------\n');
% fprintf(1, 'Blob #, Area in Pixels, Mean R, Mean G, Mean B\n');
% fprintf(1, '----------------------------------------------\n');
% for blobNumber = 1 : numberOfBlobs
% fprintf(1, '#%5d, %14d, %6.2f, %6.2f, %6.2f\n', blobNumber, areas(blobNumber), ...
% meanRGB(blobNumber, 1), meanRGB(blobNumber, 2), meanRGB(blobNumber, 3));
% end
colorValue.color= 'red';
colorValue.redObjectsMask = redObjectsMask;
else
% Alert user that no red blobs were found.
% message = sprintf('No red blobs were found in the image:\n%s', fullImageFileName);
% fprintf(1, '\n%s\n', message);
colorValue.color= 'no';
colorValue.redObjectsMask = redObjectsMask;
end