Switch to unified view

a b/functions/functions_Reconstruct/reconstructImage.m
1
function [tmpImage, errV] = reconstructImage(resF, sortRes, gaborBank, powerMapF, indFiltersToUse, im, imageSize, sizeRes, param, plotta)
2
3
%init matrix indicating if for each position a wavelet
4
%with orient and scale already used
5
tmpRes = initTmpRes(gaborBank, powerMapF);
6
7
%init error vector
8
errV = zeros(param.numWavelets, 1);
9
10
%init reconstructed image
11
tmpImage = zeros(imageSize);
12
13
%create figure
14
if plotta
15
    fsfigure
16
end %if plotta
17
18
%init counters
19
countW = 1; %wavelet counter
20
ind = 1; %sorted response counter
21
%loop on wavelet responses
22
while (countW <= param.numWavelets && ind <= sizeRes)
23
    
24
    %get information of corresponding wavelet
25
    currScale = sortRes(ind, 2);
26
    currTheta = sortRes(ind, 3);
27
    currX = sortRes(ind, 4);
28
    currY = sortRes(ind, 5);
29
    currO = sortRes(ind, 6);
30
    
31
    %check if current wavelet at current position already
32
    %used
33
    if (tmpRes(currO).value(currY, currX) == 1)
34
        %increment counter
35
        ind = ind + 1;
36
        continue
37
    end %if (tmpRes(currY, currX, currO)
38
    
39
    %check if current wavelet among the best
40
    if ~ismember(currO,  indFiltersToUse)
41
        %increment counter and skip
42
        ind = ind + 1;
43
        continue
44
    end %if ~ismember(currO,  ind_o_counter_sort)
45
    
46
    %get filters with current parameters
47
    currGaborEven = gaborBank(currO).even;
48
    currGaborOdd = gaborBank(currO).odd;
49
    
50
    %get weights for gabor filters
51
    weightEven = resF(currO).even;
52
    weightOdd = resF(currO).odd;
53
    
54
    %compute filtersize
55
    filterSize = computeFilterSizeFromScale(currScale);
56
    %compute step
57
    step = computeStepFromScale(currScale);
58
    
59
    %get filter position
60
    %check x,y correct
61
    [posX, posY, posX_filter, posY_filter] = findFilterPosition(currX, currY, step, imageSize(1), filterSize);
62
    
63
    %shift filter to center filter at x,y
64
    %currGaborEven = adjustGaborPos(currGaborEven, currX, currY, imageSize);
65
    %currGaborOdd = adjustGaborPos(currGaborOdd, currX, currY, imageSize);
66
    
67
    %add to image
68
    %tmpImage = tmpImage + (weightEven(currY, currX) * currGaborEven);
69
    %tmpImage = tmpImage + (weightOdd(currY, currX) * currGaborOdd);
70
    
71
    %add to image
72
    tmpImage(posY, posX) = tmpImage(posY, posX) + (weightEven(currY, currX) * currGaborEven(posY_filter, posX_filter));
73
    tmpImage(posY, posX) = tmpImage(posY, posX) + (weightOdd(currY, currX) * currGaborOdd(posY_filter, posX_filter));
74
    
75
    %Mean Squared Error between original image and
76
    %reconstructed
77
    imOrigNorm = normalizzaImg(im);
78
    err = immse(imOrigNorm, normalizzaImg(tmpImage));
79
    errV(countW) = err;
80
        
81
    if plotta && mod(countW, 1000) == 0
82
        
83
        %get image limits
84
        climG = getImageLimits(currGaborEven);
85
        climI = getImageLimits(tmpImage);
86
        
87
        
88
        subplot(2,2,1);
89
        imshow(im,[])
90
        title('Original Image')
91
        
92
        subplot(2,2,2);
93
        imshow(currGaborEven, climG);
94
        title('Current wavelet')
95
        
96
        subplot(2,2,3);
97
        imshow(tmpImage, climI)
98
        title({['Reconstructed image with ' num2str(numel(indFiltersToUse)) ' filters, after ' num2str(countW) ' wavelets'], ['MSE: ' num2str(err)]});
99
        
100
        subplot(2,2,4);
101
        plot(errV(1:10:countW), 'LineWidth', 2);
102
        xlabel('Number of wavelets added')
103
        ylabel('MSE')
104
        title('MSE of reconstruction')
105
        
106
        pause(0.01)
107
        
108
    end %if mod(countW, 100) == 0
109
    
110
    %increment counters
111
    countW = countW + 1;
112
    ind = ind + 1;
113
    
114
    %threshold
115
    %if (err < param.minMSE) && (countW > param.minCountW)
116
    %break
117
    %end %if err < param.minMSE
118
    
119
end %while (countW <= param.numWavelets && ind <= sizeRes)
120