-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimlowhigh.m
More file actions
210 lines (198 loc) · 7.36 KB
/
imlowhigh.m
File metadata and controls
210 lines (198 loc) · 7.36 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
function [varargout] = imlowhigh(filename, type, fc, normalize, sharpness)
%
% IMLOWHIGH
%
% NB : In this new version, if filename is numeric, it is treated as the actual
% image data. and image data is returned as output.
%
% Description:
% - filters image using 2D FIR low/high/band-pass filter,
% - normalizes filtered image background luminance and contrast (optional),
% - displays original and filtered images on screen,
% - writes filtered image on disk.
%
% Usage:
% >> imlowhigh(filename, type, fc, normalize);
% >> imlowhigh(filename, type, fc, normalize, sharpness);
%
% Input arguments:
% - filename = input filename
% - type = filter type ('low'/'high'/'band')
% - fc = cutoff frequency (in cpi = cycles per image)
% - normalize = normalize filtered image contrast? ('true'/'false')
% - sharpness = frequency cutoff sharpness (optional) [0.10]
%
% Example:
% >> imlowhigh('face.bmp', 'low', 20, 'true');
% >> imlowhigh('face.bmp', 'low', 10, 'true', 0.20);
%
% Notes:
% When using non-square images, cycles per image refer to smallest side.
% Usually, try to keep cycles per images between 8 and 32.
% Filtered images are linearly normalized in terms of background luminance to
% match original images. Optionally, filtered images can also be normalized
% in terms of contrast to match original images.
% Increase frequency cutoff sharpness if warning is displayed.
%
% Author:
% Valentin Wyart (valentin.wyart@chups.jussieu.fr)
%
% Version:
% 2007/02/08
% modified by max. 2008/11/06
%
% check number of input arguments
if nargin < 5
sharpness = 0.10;
end
if nargin < 4
error('Not enough input arguments.');
end
if isnumeric(filename)
im = double(filename)./255;
else
% read original image
im = double(imread(filename))./255;
% remove extension from input filename
while ~strcmp(filename(end), '.')
filename(end) = [];
end
filename(end) = [];
end
% select which processing to apply
switch type
% apply low-pass filter
case 'low'
imf = lowpass(im, fc, sharpness, normalize);
if not(isnumeric(filename))
titlef = sprintf('imlowhigh(%s, %s, %dcpi, %s, %4.2f)', filename, type, fc, normalize, sharpness);
filenamef = sprintf('%s_%s_%dcpi_%s_%4.2f.png', filename, type, fc, normalize, sharpness);
end
% apply high-pass filter
case 'high'
imf = highpass(im, fc, sharpness, normalize);
if not(isnumeric(filename))
titlef = sprintf('imlowhigh(%s, %s, %dcpi, %s, %4.2f)', filename, type, fc, normalize, sharpness);
filenamef = sprintf('%s_%s_%dcpi_%s_%4.2f.png', filename, type, fc, normalize, sharpness);
end
% apply band-pass filter
case 'band'
imf = bandpass(im, fc, sharpness, normalize);
if not(isnumeric(filename))
titlef = sprintf('imlowhigh(%s, %s, %dto%dcpi, %s, %4.2f)', filename, type, fc(1), fc(2), normalize, sharpness);
filenamef = sprintf('%s_%s_%dto%dcpi_%s_%4.2f.png', filename, type, fc(1), fc(2), normalize, sharpness);
end
% unknown filter type
otherwise
error('Unknown filter type.');
end
% % create new figure
% figure('Toolbar', 'none');
% % display original image
% subplot(1, 2, 1);
% imagesc(im, [0 1]);
% axis('image');
% colormap('gray');
% title('Original image');
% % display filtered image
% subplot(1, 2, 2);
% imagesc(imf, [0 1]);
% axis('image');
% colormap('gray');
% title('Filtered image');
if isnumeric(filename)
varargout{1} = imf;
else
% write filtered image on disk
imwrite(imf, filenamef);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INTERNAL FUNCTION: CREATE LOW-PASS FILTER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function h = lowpassfilter(n, wc)
% create low-pass frequency response
[f1 f2] = freqspace(n, 'meshgrid');
r = sqrt(f1.^2 + f2.^2);
Hd = ones(n);
Hd(r > wc) = 0;
% create corresponding 2D FIR low-pass filter
h = fwind1(Hd, hamming(n));
% check frequency cutoff
hf = freqz2(h); hmax = max(hf(:));
if hmax < 0.8, warning('Increase sharpness.'); end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INTERNAL FUNCTION: CREATE HIGH-PASS FILTER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function h = highpassfilter(n, wc)
% create high-pass frequency response
[f1 f2] = freqspace(n, 'meshgrid');
r = sqrt(f1.^2 + f2.^2);
Hd = ones(n);
Hd(r < wc) = 0;
% create corresponding 2D FIR high-pass filter
h = fwind1(Hd, hamming(n));
% check frequency cutoff
hf = freqz2(h); hmin = min(hf(:));
if hmin > 0.2, warning('Increase sharpness.'); end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INTERNAL FUNCTION: CREATE BAND-PASS FILTER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function h = bandpassfilter(n, wc)
% create band-pass frequency response
[f1 f2] = freqspace(n, 'meshgrid');
r = sqrt(f1.^2+f2.^2);
Hd = ones(n);
Hd((r < wc(1))|(r > wc(2))) = 0;
% create corresponding 2D FIR band-pass filter
h = fwind1(Hd, hamming(n));
% check frequency cutoff
hf = freqz2(h); hmin = min(hf(:)); hmax = max(hf(:));
if hmin > 0.2 || hmax < 0.8, warning('Increase sharpness.'); end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INTERNAL FUNCTION: APPLY LOW-PASS FILTER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function imf = lowpass(im, fc, sharpness, normalize)
% create low-pass filter
n = round(sharpness*min(size(im))/2)*2+1;
wc = 2*fc/min(size(im));
h = lowpassfilter(n, wc);
% apply low-pass filter
imf = imfilter(im, h, 'replicate');
% normalize contrast (optional)
if strcmp(normalize, 'true')
imf = (imf-min(imf(:))).*(max(im(:))-min(im(:)))./(max(imf(:))-min(imf(:)));
end
% normalize background luminance
imf = imf+im(1,1)-imf(1,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INTERNAL FUNCTION: APPLY HIGH-PASS FILTER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function imf = highpass(im, fc, sharpness, normalize)
% create high-pass filter
n = round(sharpness*min(size(im))/2)*2+1;
wc = 2*fc/min(size(im));
h = highpassfilter(n, wc);
% apply high-pass filter
imf = imfilter(im, h, 'replicate');
% normalize contrast (optional)
if strcmp(normalize, 'true')
imf = (imf-min(imf(:))).*(max(im(:))-min(im(:)))./(max(imf(:))-min(imf(:)));
end
% normalize background luminance
imf = imf+im(1,1)-imf(1,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INTERNAL FUNCTION: APPLY BAND-PASS FILTER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function imf = bandpass(im, fc, sharpness, normalize)
% create band-pass filter
n = round(sharpness*min(size(im))/2)*2+1;
wc = 2*fc/min(size(im));
h = bandpassfilter(n, wc);
% apply band-pass filter
imf = imfilter(im, h, 'replicate');
% normalize contrast (optional)
if strcmp(normalize, 'true')
imf = (imf-min(imf(:))).*(max(im(:))-min(im(:)))./(max(imf(:))-min(imf(:)));
end
% normalize background luminance
imf = imf+im(1,1)-imf(1,1);