-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageColorMap.m
185 lines (142 loc) · 5.96 KB
/
ImageColorMap.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
classdef ImageColorMap
%ImageColorMap Reads a set of images from a text file. Uses these
%images to map points to a color
properties
Images = {};
Scalars = [];
Offsets = [];
Sections = [];
Color = [];
end
properties (Constant = true)
end
methods
function Obj = ImageColorMap(CMapFileName)
%If CMapFileName has a relative path before the filename we use
%the same path to open image files
[rel_path, FileName, ~] = fileparts(CMapFileName);
if ~exist(CMapFileName, 'file')
disp(['Image color maps not found at ' CMapFileName]);
numRows = 0;
Obj.Sections = zeros(numRows,1);
Obj.Scalars = zeros(numRows, 2);
Obj.Offsets = zeros(numRows, 2);
Obj.Images = cell(numRows,1);
return;
end
Data = importdata(CMapFileName);
numRows = length(Data) - 1; %-1 for header row
Obj.Sections = zeros(numRows,1);
Obj.Scalars = zeros(numRows, 2);
Obj.Offsets = zeros(numRows, 2);
Obj.Images = cell(numRows,1);
%Offsets used to read file
iSection = 1;
iXOffset = 2;
iYOffset = 3;
iXScale = 4;
iYScale = 5;
iRed = 6;
iGreen = 7;
iBlue = 8;
iFilename = 9;
iNextImage = 1;
for(iLine = 2:length(Data))
C = textscan(Data{iLine}, '%d%f%f%f%f%f%f%f%s');
XOffset = C{iXOffset};
YOffset = C{iYOffset};
XScale = C{iXScale};
YScale = C{iYScale};
filename = fullfile(rel_path, C{iFilename});
%Colors are optional, if they aren't present assume [1 1 1]
inputColor = [];
if(isempty(C{iBlue}))
inputColor = [1 1 1];
else
inputColor = [C{iRed} C{iGreen} C{iBlue}];
end
XScale = 1/XScale;
YScale = 1/YScale;
Obj.Sections(iLine-1, :) = C{iSection};
Obj.Offsets(iLine-1, :) = [XOffset YOffset];
Obj.Scalars(iLine-1, :) = [YScale XScale];
Obj.Color(iLine-1, :) = inputColor;
disp(['Loading image color map: ' filename{1}]);
if exist(filename{1}, 'file')
I = imread(filename{1});
I = single(I) ./ 255.0; %We just assume uint8
Obj.Images{iNextImage} = I;
iNextImage = iNextImage + 1;
else
disp(' Could not read image!');
end
end
end
%Returns a tuple for the color that matches the coordinates.
%Otherwise and empty array if the point can't be mapped
%X Y Z can be a vector of multiple coordinates. In this case
%We blend all intersecting color maps
function [C] = GetColor(Obj, X,Y,Z,Radius)
C = [];
%Figure out which sections are overlapping
[MatchingSections, iSections, iZ] = intersect(Obj.Sections,Z);
if(isempty(iSections))
return;
end
Offset = Obj.Offsets(iSections,:);
Scalar = Obj.Scalars(iSections,:);
ChannelContrib = Obj.Color(iSections,:);
Radius = Radius .* max(Scalar,[],2);
P = [Y X];
P = P .* Scalar;
P = P + Offset;
P = round(P);
P = int32(P);
Colors = zeros(length(iSections),3);
for(i = 1:length(iSections))
I = Obj.Images{iSections(i)};
[YDim, XDim] = size(I);
if(max(P(i,1) <= 0 || P(i,1) > YDim) > 0)
try
disp(['Color mapping out of bounds: ' num2str([X Y Z]) ' -> ' num2str(P)]);
catch e
end
continue;
end
if(max(P(i,2) <= 0 || P(i,2) > XDim) > 0)
try
disp(['Color mapping out of bounds: ' num2str([X Y Z]) ' -> ' num2str(P)]);
catch e
end
continue;
end
W = Window(I, P(i,2), P(i,1), Radius(i) * sin(pi/4));
% C = squeeze(C)';
C = mean(W);
C = C .* Obj.Color(iSections(i),:); %Adjust color for this sections' map
Colors(i,:) = C;
end
%OK, average the colors together
Colors(isnan(Colors)) = [];
if(isempty(Colors))
Colors = [0.5 0.5 0.5];
elseif(sum(sum(Colors)) == 0)
Colors = [0.5 0.5 0.5];
end
ColorSum = sum(Colors,1);
ChannelDivisors = sum(ChannelContrib,1);
for(iChannel = 1:3)
if(ChannelDivisors(iChannel) < 1)
ChannelDivisors(iChannel) = 1;
end
end
C = ColorSum ./ ChannelDivisors;
if(max(C) > 1)
disp(['Bad color ' num2str(C)]);
end
if(min(C) < 0)
disp(['Bad color ' num2str(C)]);
end
end
end
end