/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk

« back to all changes in this revision

Viewing changes to dic_cpcorr.m

  • Committer: Suren A. Chilingaryan
  • Date: 2009-01-15 13:50:29 UTC
  • Revision ID: csa@dside.dyndns.org-20090115135029-wleapicg9a4593tp
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function xyinput = dic_cpcorr(CORRSIZE, PRECISION, hwid, base, xyinput_in, input)
 
2
%   Copyright 1993-2004 The MathWorks, Inc.
 
3
%   $Revision: 1.16.4.4 $  $Date: 2004/10/20 17:54:13 $
 
4
 
 
5
%input = double(input);
 
6
 
 
7
if any(xyinput_in(:)<0.5) || any(xyinput_in(:,1)>size(input,2)+0.5) || ...
 
8
   any(xyinput_in(:,2)>size(input,1)+0.5)
 
9
    msg = sprintf('In function %s, Control Points must be in pixel coordinates.',mfilename);
 
10
    eid = sprintf('Images:%s:cpPointsMustBeInPixCoord',mfilename);
 
11
    error(eid,msg);
 
12
end
 
13
 
 
14
 
 
15
% get all rectangle coordinates
 
16
rects_input = dic_calc_rects(xyinput_in, CORRSIZE, input);
 
17
 
 
18
ncp = size(xyinput_in,1);
 
19
 
 
20
xyinput = xyinput_in; % initialize adjusted control points matrix
 
21
 
 
22
for icp = 1:ncp
 
23
 
 
24
    if base(icp).skip
 
25
        % baned by base image (Nan, Inf, Near to edge, ...)
 
26
        continue
 
27
    end
 
28
    
 
29
    if isequal(rects_input(icp,3:4),[0 0])
 
30
        % near edge, unable to adjust
 
31
        continue
 
32
    end
 
33
    
 
34
    sub_input = imcrop(input,rects_input(icp,:));
 
35
 
 
36
    inputsize = size(sub_input);
 
37
 
 
38
    % make sure finite
 
39
    if any(~isfinite(sub_input(:))) 
 
40
        % NaN or Inf, unable to adjust
 
41
        continue
 
42
    end
 
43
 
 
44
    sum_T = sum(sub_input(:));
 
45
    denom_T = std(sub_input(:));
 
46
 
 
47
    % check that template rectangle sub_input has nonzero std
 
48
    if denom_T==0
 
49
        % zero standard deviation of template image, unable to adjust
 
50
        continue
 
51
    end
 
52
 
 
53
    norm_cross_corr = double(normxcorr_hw(hwid, 11, icp, rot90(sub_input,2), sum_T, denom_T));
 
54
 
 
55
    % get subpixel resolution from cross correlation
 
56
    subpixel = true;
 
57
    [xpeak, ypeak, amplitude] = findpeak(norm_cross_corr,subpixel);
 
58
 
 
59
    % eliminate any poor correlations
 
60
    THRESHOLD = 0.5;
 
61
    if (amplitude < THRESHOLD) 
 
62
        % low correlation, unable to adjust
 
63
        continue
 
64
    end
 
65
    
 
66
    % offset found by cross correlation
 
67
    corr_offset = [ (xpeak-inputsize(2)-CORRSIZE) (ypeak-inputsize(1)-CORRSIZE) ];
 
68
 
 
69
 
 
70
    % eliminate any big changes in control points
 
71
    ind = find(abs(corr_offset) > (CORRSIZE-1));
 
72
    if ~isempty(ind)
 
73
        % peak of norxcorr2 not well constrained, unable to adjust
 
74
        continue
 
75
    end
 
76
 
 
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;
 
81
 
 
82
end
 
83