Skip to content

Commit 47da61f

Browse files
committed
add ConvertScalarImageToRGB.m
1 parent 3710662 commit 47da61f

File tree

6 files changed

+267
-3
lines changed

6 files changed

+267
-3
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
classdef ConvertScalarImageToRGB < imagem.actions.CurrentImageAction
2+
% Convert an intensity or grayscale image to color RGB.
3+
%
4+
% Class ConvertScalarImageToRGB
5+
%
6+
% Example
7+
% ConvertScalarImageToRGB
8+
%
9+
% See also
10+
%
11+
12+
% ------
13+
% Author: David Legland
14+
15+
% Created: 2020-03-02, using Matlab 9.7.0.1247435 (R2019b) Update 2
16+
% Copyright 2020 INRAE - BIA-BIBS.
17+
18+
19+
%% Properties
20+
properties
21+
end % end properties
22+
23+
24+
%% Constructor
25+
methods
26+
function obj = ConvertScalarImageToRGB(varargin)
27+
% Constructor for convertScalarImageToRGB class
28+
29+
end
30+
31+
end % end constructors
32+
33+
34+
%% Methods
35+
methods
36+
function run(obj, frame) %#ok<INUSL>
37+
38+
% get handle to current doc
39+
doc = currentDoc(frame);
40+
41+
img = doc.Image;
42+
if ~isIntensityImage(img) && ~isGrayscaleImage(img)
43+
warning('require intensity or grayscale image');
44+
end
45+
46+
colormapNames = imagem.util.enums.ColorMaps.allLabels;
47+
range = [min(img) max(img)];
48+
colorNames = imagem.util.enums.BasicColors.allLabels;
49+
50+
% Creates the Dialog for choosing options
51+
gd = imagem.gui.GenericDialog('Binary Overlay');
52+
addChoice(gd, 'Color Map:', colormapNames, colormapNames{1});
53+
addNumericField(gd, 'Min Value:', range(1));
54+
addNumericField(gd, 'Max Value:', range(2));
55+
addChoice(gd, 'Background Color:', colorNames, colorNames{1});
56+
57+
% displays the dialog, and waits for user
58+
showDialog(gd);
59+
% check if ok or cancel was clicked
60+
if wasCanceled(gd)
61+
return;
62+
end
63+
64+
% parse user inputs
65+
colorMapItem = imagem.util.enums.ColorMaps.fromLabel(getNextString(gd));
66+
cmap = createColorMap(colorMapItem, 256);
67+
vmin = getNextNumber(gd);
68+
vmax = getNextNumber(gd);
69+
colorItem = imagem.util.enums.BasicColors.fromLabel(getNextString(gd));
70+
bgColor = colorItem.RGB;
71+
72+
rgb = double2rgb(img, cmap, [vmin vmax], bgColor);
73+
74+
name = 'NoName-rgb';
75+
if ~isempty(img.Name)
76+
name = [img.Name '-rgb'];
77+
end
78+
rgb.Name = name;
79+
80+
% add image to application, and create new display
81+
newDoc = addImageDocument(frame, rgb);
82+
83+
% add history
84+
string = sprintf('cmap = createColorMap(imagem.util.enums.BasicColors.%s, 256);\n', ...
85+
colorItem.Name);
86+
addToHistory(frame, string);
87+
string = sprintf('%s = double2rgb(%s, %s, [%g %g], [%g %g %g]);\n', ...
88+
newDoc.Tag, doc.Tag, 'cmap', vmin, vmax, bgColor);
89+
addToHistory(frame, string);
90+
end
91+
92+
function b = isActivable(obj, frame)
93+
b = [email protected](obj, frame);
94+
if ~b
95+
return;
96+
end
97+
98+
img = currentImage(frame);
99+
b = isIntensityImage(img) || isGrayscaleImage(img);
100+
end
101+
end % end methods
102+
103+
104+
end % end classdef
105+

ImageM/+imagem/+actions/+process/+binary/BinaryImageOverlay.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function run(obj, frame) %#ok<INUSL>
3535
imageNames = getImageNames(app);
3636
colorNames = imagem.util.enums.BasicColors.allLabels;
3737

38-
% Creates the Dialogfor choosing options
38+
% Creates the Dialog for choosing options
3939
gd = imagem.gui.GenericDialog('Binary Overlay');
4040
addChoice(gd, 'Reference Image:', imageNames, imageNames{1});
4141
addChoice(gd, 'Binary Image:', imageNames, imageNames{1});

ImageM/+imagem/+gui/FrameMenuBuilder.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ function buildImageFrameMenu(obj, hf)
139139
addMenuItem(obj, convertMenu, ConvertImage3DToVectorImage(), '3D Image to Vector Image');
140140
addMenuItem(obj, convertMenu, ConvertVectorImageToImage3D(), 'Vector Image to 3D Image');
141141
addMenuItem(obj, convertMenu, ConvertVectorImageToRGB(), 'Vector Image to RGB');
142+
addMenuItem(obj, convertMenu, ConvertScalarImageToRGB(), 'Intensity Image to RGB', 'Separator', 'on');
142143
addMenuItem(obj, convertMenu, UnfoldVectorImage(), 'Unfold Vector Image to Table', 'Separator', 'on');
143144
addMenuItem(obj, convertMenu, UnfoldVectorImageWithMask(), 'Unfold Vector Image Within Mask to Table...');
144145
addMenuItem(obj, imageMenu, ReshapeImage(), 'Reshape...');

ImageM/+imagem/+tools/Brush.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function processCurrentPosition(obj)
6868
if isa(obj.Viewer, 'imagem.gui.Image3DSliceViewer')
6969
iz = obj.Viewer.SliceIndex;
7070
end
71-
ic = 1:channelNumber(img);
71+
ic = 1:channelCount(img);
7272
it = 1; % not managed for the moment
7373

7474
if ~isempty(obj.PreviousPoint)

ImageM/+imagem/+util/+enums/BasicColors.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
classdef BasicColors
2-
% One-line description here, please.
2+
% A list of basic colors associated with names.
33
%
44
% Enumeration BasicColors
55
%
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
classdef ColorMaps
2+
% Various color maps for generating color displays.
3+
%
4+
% Enumeration ColorMaps
5+
%
6+
% Example
7+
% label = 'Parula';
8+
% item = imagem.util.enums.ColorMaps.fromLabel(label);
9+
% cmap = createColorMap(item, 256);
10+
% size(cmap)
11+
% 256 3
12+
%
13+
% See also
14+
% BasicColors
15+
16+
% ------
17+
% Author: David Legland
18+
19+
% Created: 2021-01-05, using Matlab 9.8.0.1323502 (R2020a)
20+
% Copyright 2021 INRAE - BIA-BIBS.
21+
22+
23+
%% Enumerates the different cases
24+
enumeration
25+
Parula('Parula');
26+
Jet('Jet');
27+
Gray('Gray');
28+
Hsv('Hsv');
29+
Hot('Hot');
30+
Cool('Cool');
31+
end % end properties
32+
33+
34+
%% Static methods
35+
methods (Static)
36+
function res = allNames()
37+
% Returns a cell list with all enumeration names.
38+
mc = ?imagem.util.enums.ColorMaps;
39+
itemList = mc.EnumerationMemberList;
40+
nItems = length(itemList);
41+
res = cell(1, nItems);
42+
43+
for i = 1:nItems
44+
% retrieve current enumeration item
45+
mitem = itemList(i);
46+
res{i} = mitem.Name;
47+
end
48+
end
49+
50+
function res = fromName(name)
51+
% Identifies a ColorMaps from its name.
52+
if nargin == 0 || ~ischar(name)
53+
error('requires a character array as input argument');
54+
end
55+
56+
mc = ?imagem.util.enums.ColorMaps;
57+
itemList = mc.EnumerationMemberList;
58+
for i = 1:length(itemList)
59+
% retrieve current enumeration item
60+
mitem = itemList(i);
61+
item = imagem.util.enums.ColorMaps.(mitem.Name);
62+
if strcmpi(name, char(item))
63+
res = item;
64+
return;
65+
end
66+
end
67+
68+
error('Unrecognized ColorMaps name: %s', name);
69+
end
70+
71+
function res = allLabels()
72+
% Returns a cell list with all enumeration names.
73+
mc = ?imagem.util.enums.ColorMaps;
74+
itemList = mc.EnumerationMemberList;
75+
nItems = length(itemList);
76+
res = cell(1, nItems);
77+
78+
for i = 1:nItems
79+
% retrieve current enumeration item
80+
mitem = itemList(i);
81+
item = imagem.util.enums.ColorMaps.(mitem.Name);
82+
res{i} = item.Label;
83+
end
84+
end
85+
86+
function res = fromLabel(label)
87+
% Identifies a ColorMaps from its label.
88+
if nargin == 0 || ~ischar(label)
89+
error('requires a character array as input argument');
90+
end
91+
92+
mc = ?imagem.util.enums.ColorMaps;
93+
itemList = mc.EnumerationMemberList;
94+
for i = 1:length(itemList)
95+
% retrieve current enumeration item
96+
mitem = itemList(i);
97+
item = imagem.util.enums.ColorMaps.(mitem.Name);
98+
if strcmpi(label, item.Label)
99+
res = item;
100+
return;
101+
end
102+
end
103+
104+
error('Unrecognized ColorMaps label: %s', label);
105+
end
106+
end % end methods
107+
108+
109+
%% Constructor
110+
methods
111+
function map = createColorMap(obj, varargin)
112+
% Create a N-by-3 color array.
113+
114+
% determine color number
115+
n = 256;
116+
if ~isempty(varargin)
117+
n = varargin{1};
118+
end
119+
120+
% create color map
121+
switch obj
122+
case imagem.util.enums.ColorMaps.Parula
123+
map = parula(n);
124+
case imagem.util.enums.ColorMaps.Jet
125+
map = jet(n);
126+
case imagem.util.enums.ColorMaps.Hsv
127+
map = hsv(n);
128+
case imagem.util.enums.ColorMaps.Hot
129+
map = hot(n);
130+
case imagem.util.enums.ColorMaps.Cool
131+
map = cool(n);
132+
case imagem.util.enums.ColorMaps.Gray
133+
map = gray(n);
134+
otherwise
135+
error(['Unprocessed color map: ' obj.Name]);
136+
end
137+
end
138+
139+
end % end constructors
140+
141+
142+
%% Constructor
143+
methods
144+
function obj = ColorMaps(label, varargin)
145+
% Constructor for ColorMaps class.
146+
obj.Label = label;
147+
end
148+
149+
end % end constructors
150+
151+
152+
%% Properties
153+
properties
154+
Label;
155+
end % end properties
156+
157+
end % end classdef
158+

0 commit comments

Comments
 (0)