-
Notifications
You must be signed in to change notification settings - Fork 1
/
change1colorOnRGBToAnother.m
102 lines (76 loc) · 2.5 KB
/
change1colorOnRGBToAnother.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
function rgbImageModified = change1colorOnRGBToAnother(rgbImage, toleranceOfColorDetectedForChange, FromColor, ToColor)
%CHANGE1COLORONRGBTOANOTHER
[rowSize colSize numberOfRGB_planes] = size(rgbImage);
redPlane = rgbImage(:,:,1);
greenPlane = rgbImage(:,:,2);
bluePlane = rgbImage(:,:,3);
% [colors observed may not be of absolute values, tolerance range required]
% tolerance = 100;
tolerance = toleranceOfColorDetectedForChange;
% default: 'black'
redPlaneToBeChanged = 0:tolerance;
greenPlaneToBeChanged = 0:tolerance;
bluePlaneToBeChanged = 0:tolerance;
valueRange = (255-tolerance):255;
%%%%%%%%%%%%%%%%%% From This Color
switch lower(FromColor)
case {'red', 'r'}
% Assign red to the thresholded part.
redPlaneToBeChanged = valueRange;
case {'blue', 'b'}
bluePlaneToBeChanged = valueRange;
case {'green', 'g'}
greenPlaneToBeChanged = valueRange;
case {'white', 'w'}
redPlaneToBeChanged = valueRange;
greenPlaneToBeChanged = valueRange;
bluePlaneToBeChanged = valueRange;
case {'yellow', 'y'}
redPlaneToBeChanged = valueRange;
greenPlaneToBeChanged = valueRange;
case 'black'
otherwise
disp('Unknown color.')
end
%%%%%%%%%%%%%%%%%% To This Color
% default: 'black'
redPlaneToBeMappedTo = 0;
greenPlaneToBeMappedTo = 0;
bluePlaneToBeMappedTo = 0;
switch lower(ToColor)
case {'red', 'r'}
% Assign red to the thresholded part.
redPlaneToBeMappedTo = 255;
case {'blue', 'b'}
bluePlaneToBeMappedTo = 255;
case {'green', 'g'}
greenPlaneToBeMappedTo = 255;
case {'white', 'w'}
redPlaneToBeMappedTo = 255;
greenPlaneToBeMappedTo = 255;
bluePlaneToBeMappedTo = 255;
case {'yellow', 'y'}
redPlaneToBeMappedTo = 255;
greenPlaneToBeMappedTo = 255;
case 'black'
otherwise
disp('Unknown color.')
end
%%%%%%%%%%%%%%%%%%%%%%%% START Chnaging the colors
for i = 1:rowSize
for j = 1:colSize
if ( sum( redPlane(i,j) == redPlaneToBeChanged) && ...
sum( greenPlane(i,j) == greenPlaneToBeChanged) && ...
sum( bluePlane(i,j) == bluePlaneToBeChanged) ...
)
redPlane(i,j) = redPlaneToBeMappedTo;
greenPlane(i,j) = greenPlaneToBeMappedTo;
bluePlane(i,j) = bluePlaneToBeMappedTo;
end
end
end
%%%%%%%%%%%%%%%%%%%%%
% Make a color version of the image
rgbImageModified = cat(3, redPlane, greenPlane, bluePlane);
% imshow(rgbImageModified, []);
end