function imo = CS4640_dot(im,H,mag_check)
% CS4640_dot - compute cos of image window and template (viewed as vectors)
% On input:
%     im (MxN array): input image
%     H (k1xk2 array): template
%     mag (Boolean): if 1, factor vector magnitude in, else don't
% On output:
%     imo (MxN array): cos template and image windows
% Call:
%     imo = CS4640_dot(im,H,0);
% Author:
%     T. Henderson
%     UU
%     Spring 2018
%

[M,N] = size(im);
[p,q] = size(H);
k1 = (p-1)/2;
k2 = (q-1)/2;
H = H(:);
H_len = norm(H);
imo = H_len*ones(M,N);
Hm = mean(H);
im_spread = max(max(im)) - min(min(im));

for r = 1+k1:M-k1
    for c = 1+k2:N-k2
        W = im(r-k1:r+k1,c-k2:c+k2);
        W = W(:);
        W_len = norm(W);
        if W_len>0&H_len>0
            imo(r,c) = dot(W,H)/(H_len*W_len);
        end
        if r==160&c==306
            tch = 0;
        end
        if mag_check==1
            dist = norm(H-W);
            if dist>0.4*H_len
                imo(r,c) = 0.5*imo(r,c);
            end
        end
        imo(r,c) = sum(abs(H-W));
    end
end
