-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathipixswitch.m
116 lines (107 loc) · 3.65 KB
/
ipixswitch.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
%IPIXSWITCH Pixelwise image merge
%
% OUT = IPIXSWITCH(MASK, IM1, IM2) is an image where each pixel is selected
% from the corresponding pixel in IM1 or IM2 according to the corresponding
% pixel values in MASK. If the element of MASK is zero IM1 is selected,
% otherwise IM2 is selected.
%
% IM1 or IM2 can contain a color descriptor which is one of:
% - A scalar value corresponding to a greyscale
% - A 3-vector corresponding to a color value
% - A string containing the name of a color which is found using COLORNAME.
%
% IPIXSWITCH(MASK, IM1, IM2) as above but the result is displayed.
%
% Example::
% Read a uint8 image
% im = iread('lena.pgm');
% and set high valued pixels to red
% a = ipixswitch(im>120, im, uint8([255 0 0]));
% The result is a uint8 image since both arguments are uint8 images.
%
% a = ipixswitch(im>120, im, [1 0 0]);
% The result is a double precision image since the color specification
% is a double.
%
% a = ipixswitch(im>120, im, 'red');
% The result is a double precision image since the result of colorname
% is a double precision 3-vector.
%
% Notes::
% - IM1, IM2 and MASK must all have the same number of rows and columns.
% - If IM1 and IM2 are both greyscale then OUT is greyscale.
% - If either of IM1 and IM2 are color then OUT is color.
% - If either one image is double and one is integer then the integer
% image is first converted to a double image.
%
% See also COLORIZE, COLORNAME.
% Copyright 2022-2023 Peter Corke, Witold Jachimczyk, Remo Pillat
function co = ipixswitch(mask, I1, I2)
if ischar(I1)
% image is a string color name
col = colorname(I1);
if isempty(col)
error('unknown color %s', col);
end
I1 = icolor(ones(size(mask)), col);
elseif isscalar(I1)
% image is a scalar, create a greyscale image same size as mask
I1 = ones(size(mask), class(I1))*I1;
elseif ndims(I1) == 2 && all(size(I1) == [1 3])
% image is 1x3, create a color image same size as mask
I1 = icolor(ones(size(mask), class(I1)), I1);
else
% actual image, check the dims
s = size(I1); s = s(1:2);
if ~all(s == size(mask))
error('input image sizes do not conform');
end
end
if ischar(I2)
% image is a string color name
col = colorname(I2);
if isempty(col)
error('unknown color %s', col);
end
I2 = icolor(ones(size(mask)), col);
elseif isscalar(I2)
% image is a scalar, create a greyscale image same size as mask
I2 = ones(size(mask), class(I2))*I2;
elseif ndims(I2) == 2 && all(size(I2) == [1 3])
% image is 1x3, create a color image same size as mask
I2 = icolor(ones(size(mask), class(I2)), I2);
else
% actual image, check the dims
s = size(I2); s = s(1:2);
if ~all(s == size(mask))
error('input image sizes do not conform');
end
end
if isfloat(I1) && isinteger(I2)
I2 = idouble(I2);
elseif isinteger(I1) && isfloat(I2)
I1 = idouble(I1);
end
nplanes = max(size(I1,3), size(I2,3));
if nplanes == 3
%mask = repmat(mask, [1 1 3]);
if size(I1,3) == 1
I1 = repmat(I1, [1 1 3]);
end
if size(I2,3) == 1
I2 = repmat(I2, [1 1 3]);
end
end
% in case one of the images contains NaNs we cant blend the images
% using arithmetic
% out = mask .* I1 + (1-mask) .* I2;
out = I2;
mask = logical(mask);
mask = repmat(mask, [1 1 nplanes]);
out(mask) = I1(mask);
if nargout > 0
co = out;
else
idisp(out);
shg
end