-
Notifications
You must be signed in to change notification settings - Fork 1
/
Image2ColourSpace.m
113 lines (93 loc) · 3.04 KB
/
Image2ColourSpace.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
function [colourIm, colourImUInt8] = Image2ColourSpace(im, colourType)
% Changes the colourType of an image
%
% im: Image, double or UINT8, grayscale (size(im,3) == 1) or Rgb
% colourType: String denoting colourType. Possibilities:
% - Rgb (no change)
% - Hsv
% - H, H channel of Hsv
% - C, C from Color Invariance, Geusebroek et al.
% - HS. HS channels of Hsv
% - Intensity (grayscale)
% - RgbNorm, Normalized RGB
% - RGI, Normalized RG channels plus intensity (B is redundant)
% - Lab, LAB colour space
% - Opp, Opponent colour space
%
% colourIm: Transformed image as a double
% colourImUint8:Transformed image as UINT8
%
% Jasper Uijlings - 2012
if isa(im, 'double')
doubleIm = im;
im = im2uint8(im);
else
doubleIm = im2double(im);
end
% Adjust for grey values images
if size(im,3) == 1
if strcmp(colourType, 'Intensity')
colourIm = doubleIm;
colourImUInt8 = im2uint8(colourIm);
return;
else
im = repmat(im, [1 1 3]);
end
end
switch colourType
case 'Rgb' % No conversion ;-)
colourImUInt8 = im;
colourIm = doubleIm;
case 'Hsv'
% Convert to HSV
hsvIm = im2uint8(rgb2hsv(doubleIm));
colourImUInt8 = hsvIm;
colourIm = im2double(hsvIm);
case 'H'
% Convert to HSV
hsvIm = im2uint8(rgb2hsv(doubleIm));
hIm = hsvIm(:,:,1);
colourImUInt8 = hIm;
colourIm = im2double(hIm);
case 'C'
cIm = im2uint8(Rgb2C(doubleIm));
cIm(:,:,3) = 0;
colourImUInt8 = cIm;
colourIm = im2double(cIm);
case 'HS'
% Convert to HSV
hsvIm = im2uint8(rgb2hsv(doubleIm));
hsvIm(:,:,3) = 0;
colourImUInt8 = hsvIm;
colourIm = im2double(hsvIm);
case 'Intensity'
colourIm = rgb2gray(doubleIm);
colourImUInt8 = im2uint8(colourIm);
case 'RgbNorm'
rgbNormIm = im2uint8(Rgb2Rg(im));
colourImUInt8 = rgbNormIm;
colourIm = im2double(rgbNormIm);
case 'RGI'
% Convert to RGI
rgiIm = im2uint8(Rgb2Rgi(im));
colourImUInt8 = rgiIm;
colourIm = im2double(rgiIm);
case 'Lab'
cform = makecform('srgb2lab');
labIm = applycform(im,cform);
colourImUInt8 = labIm;
colourIm = im2double(labIm);
case 'Opp'
% Convert to opponent colour space
oppIm = Rgb2Ooo(im);
% Normalize stuff
oppIm(:,:,1) = NormalizeArray(oppIm(:,:,1));
oppIm(:,:,2) = NormalizeArray(oppIm(:,:,2));
oppIm(:,:,3) = NormalizeArray(oppIm(:,:,3));
% Back to uint8 for the segmentation
oppIm = im2uint8(oppIm);
colourImUInt8 = oppIm;
colourIm = im2double(oppIm);
otherwise
warning('Invalid colour type: %s\n', colorType);
end