/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk
2 by Suren A. Chilingaryan
Support for different optimization modes
1
function xyinput = dic_cpcorr(CORRSIZE, PRECISION, OPTIMIZE, hwid, base, xyinput_in, input)
1 by Suren A. Chilingaryan
Initial import
2
%   Copyright 1993-2004 The MathWorks, Inc.
3
%   $Revision: 1.16.4.4 $  $Date: 2004/10/20 17:54:13 $
4
5
if any(xyinput_in(:)<0.5) || any(xyinput_in(:,1)>size(input,2)+0.5) || ...
6
   any(xyinput_in(:,2)>size(input,1)+0.5)
7
    msg = sprintf('In function %s, Control Points must be in pixel coordinates.',mfilename);
8
    eid = sprintf('Images:%s:cpPointsMustBeInPixCoord',mfilename);
9
    error(eid,msg);
10
end
11
12
13
% get all rectangle coordinates
14
rects_input = dic_calc_rects(xyinput_in, CORRSIZE, input);
15
16
ncp = size(xyinput_in,1);
17
18
xyinput = xyinput_in; % initialize adjusted control points matrix
19
40 by Suren A. Chilingaryan
Few more fixes
20
parfor icp = 1:ncp
1 by Suren A. Chilingaryan
Initial import
21
22
    if base(icp).skip
23
	% baned by base image (Nan, Inf, Near to edge, ...)
24
	continue
25
    end
26
    
27
    if isequal(rects_input(icp,3:4),[0 0])
28
        % near edge, unable to adjust
29
        continue
30
    end
31
    
32
    sub_input = imcrop(input,rects_input(icp,:));
33
34
    inputsize = size(sub_input);
35
36
    % make sure finite
37
    if any(~isfinite(sub_input(:))) 
38
        % NaN or Inf, unable to adjust
39
        continue
40
    end
41
42
    sum_T = sum(sub_input(:));
3 by Suren A. Chilingaryan
Add error checking to initialization process
43
    % DS: double is required by 2007+ versions of Matlab for not known reasons
44
    denom_T = std(double(sub_input(:)));
1 by Suren A. Chilingaryan
Initial import
45
46
    % check that template rectangle sub_input has nonzero std
47
    if denom_T==0
48
        % zero standard deviation of template image, unable to adjust
49
        continue
50
    end
51
8 by Suren A. Chilingaryan
Complete elimination of cpcorr
52
    norm_cross_corr = dic_normxcorr2(OPTIMIZE, hwid, icp, sub_input, base(icp), sum_T, denom_T);
1 by Suren A. Chilingaryan
Initial import
53
54
    % get subpixel resolution from cross correlation
55
    subpixel = true;
56
    [xpeak, ypeak, amplitude] = findpeak(norm_cross_corr,subpixel);
57
58
    % eliminate any poor correlations
59
    THRESHOLD = 0.5;
60
    if (amplitude < THRESHOLD) 
61
        % low correlation, unable to adjust
62
        continue
63
    end
64
    
65
    % offset found by cross correlation
66
    corr_offset = [ (xpeak-inputsize(2)-CORRSIZE) (ypeak-inputsize(1)-CORRSIZE) ];
67
68
69
    % eliminate any big changes in control points
70
    ind = find(abs(corr_offset) > (CORRSIZE-1));
71
    if ~isempty(ind)
72
        % peak of norxcorr2 not well constrained, unable to adjust
73
        continue
74
    end
41 by Suren A. Chilingaryan
Synchronize with Chris version, set precision to 1, initial implementation of labview wrapper
75
    
76
    PRECISION=1;
1 by Suren A. Chilingaryan
Initial import
77
    input_fractional_offset = xyinput(icp,:) - round(xyinput(icp,:)*PRECISION)/PRECISION;
78
    
79
    % adjust control point
80
    xyinput(icp,:) = xyinput(icp,:) - input_fractional_offset - corr_offset + base(icp).base_fractional_offset;
41 by Suren A. Chilingaryan
Synchronize with Chris version, set precision to 1, initial implementation of labview wrapper
81
    %xyinput(icp,:) = round(xyinput(icp,:)) - corr_offset + base(icp).base_fractional_offset;
1 by Suren A. Chilingaryan
Initial import
82
83
end
84