-
Notifications
You must be signed in to change notification settings - Fork 0
/
imoverlay.m
71 lines (65 loc) · 2.32 KB
/
imoverlay.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
function out = imoverlay(in, mask, color)
%IMOVERLAY Create a mask-based image overlay.
% OUT = IMOVERLAY(IN, MASK, COLOR) takes an input image, IN, and a binary
% image, MASK, and produces an output image whose pixels in the MASK
% locations have the specified COLOR.
%
% IN should be a grayscale or an RGB image of class uint8, uint16, int16,
% logical, double, or single. If IN is double or single, it should be in
% the range [0, 1]. If it is not in that range, you might want to use
% mat2gray to scale it into that range.
%
% MASK should be a two-dimensional logical matrix.
%
% COLOR should be a 1-by-3 vector of values in the range [0, 1]. [0 0 0]
% is black, and [1 1 1] is white.
%
% OUT is a uint8 RGB image.
%
% Examples
% --------
% Overlay edge detection result in green over the original image.
%
% I = imread('cameraman.tif');
% bw = edge(I, 'canny');
% rgb = imoverlay(I, bw, [0 1 0]);
% imshow(rgb)
%
% Treating the output of peaks as an image, overlay the values greater than
% 7 in red. The output of peaks is not in the usual grayscale image range
% of [0, 1], so use mat2gray to scale it.
%
% I = peaks;
% mask = I > 7;
% rgb = imoverlay(mat2gray(I), mask, [1 0 0]);
% imshow(rgb, 'InitialMagnification', 'fit')
% Steven L. Eddins, The MathWorks, Inc.
% $Revision: 1.2 $ $Date: 2007/08/15 13:18:08 $
% If the user doesn't specify the color, use white.
DEFAULT_COLOR = [1 1 1];
if nargin < 3
color = DEFAULT_COLOR;
end
% Make the uint8 the working data class. The output is also uint8.
in_uint8 = im2uint8(in);
color_uint8 = im2uint8(color);
% Initialize the red, green, and blue output channels.
if ndims(in_uint8) == 2
% Input is grayscale. Initialize all output channels the same.
out_red = in_uint8;
out_green = in_uint8;
out_blue = in_uint8;
else
% Input is RGB truecolor.
out_red = in_uint8(:,:,1);
out_green = in_uint8(:,:,2);
out_blue = in_uint8(:,:,3);
end
% Replace output channel values in the mask locations with the appropriate
% color value.
out_red(mask) = color_uint8(1); %%??
out_green(mask) = color_uint8(2);
out_blue(mask) = color_uint8(3);
% Form an RGB truecolor image by concatenating the channel matrices along
% the third dimension.
out = cat(3, out_red, out_green, out_blue);