a b/libraries/lib_1Shot-MaxPol/utilities/radSpec.m
1
function [spec, r_grid_bin] = radSpec(inputImage, n_bins)
2
3
%RADSPEC radial spectrum
4
%
5
%   [radSpec] = radSpec(im, n_bins)
6
%
7
%   Returns amplitude spectrum of image in frequency domain using circular
8
%   bounds for calculation
9
%
10
%   Input(s):
11
%   'im'        input image N1xN2xN3
12
%   'n_bins'    number of bins for histogram calculation
13
%
14
%   Output(s):
15
%   'radSpec'   spectrum response
16
%
17
%   See also DEBLURRING_KERNEL_ESTIMATION
18
%
19
%
20
%   Copyright (c) 2019 Mahdi S. Hosseini
21
%
22
%   University of Toronto
23
%   The Edward S. Rogers Sr. Department of,
24
%   Electrical and Computer Engineering (ECE)
25
%   Toronto, ON, M5S3G4, Canada
26
%   Tel: +1 (416) 978 6845
27
%   email: mahdi.hosseini@mail.utoronto.ca
28
29
[N_1, N_2, ~] = size(inputImage);
30
if mod(N_1, 2) == 0
31
    inputImage = padarray(inputImage, [1, 0], 'pre');
32
end
33
if mod(N_2, 2) == 0
34
    inputImage = padarray(inputImage, [0, 1], 'pre');
35
end
36
[N_1, N_2, N_3] = size(inputImage);
37
r_max = floor(max([N_1, N_2])/2);
38
39
for channel = 1: N_3
40
    I = inputImage(:,:,channel);
41
    FT = fftshift(fft2(I, 2*r_max+1, 2*r_max+1));
42
    frequency_spectrum = abs(FT);
43
    if false
44
        frequency_spectrum(:, floor(N_2/2)+1) = 0;
45
        frequency_spectrum(floor(N_1/2)+1, :) = 0;
46
    end
47
    h_bin = 1/n_bins;
48
    r_grid_bin = [h_bin:h_bin:1]'*pi;
49
    r_grid = [-r_max: r_max]/r_max*pi;
50
    [X, Y] = meshgrid(r_grid);
51
    Y = -Y;
52
    A = sqrt(X.^2 + Y.^2);    
53
    circle_mask = A <= pi;
54
    pixel_area = pi^3/sum(circle_mask(:));
55
    for iteration = 1: n_bins
56
        mask_out =  A <= r_grid_bin(iteration);
57
        if iteration ~= 1
58
            mask_in = A <= r_grid_bin(iteration-1);
59
            mask = xor(mask_out, mask_in);
60
        else
61
            mask = mask_out;
62
        end
63
        S(iteration) = pixel_area * sum(mask(:));
64
        spec(iteration, channel) = ...
65
            sum(frequency_spectrum(mask))/S(iteration);
66
    end
67
    spec(:, channel) = spec(:, channel)/spec(1, channel);
68
end