View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Image correspondences using cross-correlation

5.0 | 6 ratings Rate this file 46 Downloads (last 30 days) File Size: 764 KB File ID: #27269 Version: 1.3
image thumbnail

Image correspondences using cross-correlation


David Young (view profile)


16 Apr 2010 (Updated )

Find matching features in pairs of images using normalised cross-correlation: class file and demo.

| Watch this File

File Information

Finding corresponding features in a pair of images is the basis of many optic flow, stereo vision and image registration algorithms. One straightforward approach to finding a match is to take a small patch of one image, compute its sliding cross-correlation with the other image, and find a peak. This submission supplies a class which implements this method.

There are many other ways of finding correspondences, but normalised cross-correlation is relatively easy to understand, and fairly effective if a sparse set of matches is sufficient and the change in viewpoint is not too large. This submission is intended largely as a learning aid, though it may be usable for some applications. A demonstration script is therefore included.

The algorithm extends the basic idea in two ways. First, a reverse match may be done on each feature pair to test for consistency; this eliminates many incorrect matches. Second, the correlation computation may be greatly speeded up using the SVD trick implemented in convolve2, at the cost of some accuracy and density of matches.

The core of the submission is object-oriented to facilitate its use with image sequences and to allow efficient reuse of parts of the computation.


Fast 2 D Convolution and Extended Array Indexing inspired this file.

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (25)
18 Feb 2016 giovanni edgina

05 Jun 2014 David Young

David Young (view profile)

sush: Maybe you have a file called edge.m on your path that is hiding the toolbox edge function. What is the output of the command "which edge"? If the result isn't in the Image Processing Toolbox, you could try renaming your edge.m file.

Comment only
05 Jun 2014 sush

sush (view profile)

its showing error dat "attemp to call edge as script function " why ? ?

Comment only
21 Oct 2013 MD JAYED Hussan

18 Oct 2013 David Young

David Young (view profile)

farah: Yes, you can use another set of images. You'll see that the images are read from disk at the start of the demo, and you can change those lines to read different images. However, it will be better in the end to write your own code to call correlCorresp rather than relying on the demo. If you are not sure how to proceed, you may need to work through "Getting Started with Matlab" in the documentation.

MD JAYED Hussan: The corresponding coordinates are held in the corresps property of a correlCorresp object, after a call to the findCorresps method. You can see how the vectors are computed by inspecting the code for correspDisplay. It's all in the documentation, by the way.

Comment only
18 Oct 2013 MD JAYED Hussan

The code is working great. Can point me where i need to modify it to get the length of the blue lines so that i have calculate the velocity of shifting between two slides when i have a frame rate.

Comment only
17 Oct 2013 farah

farah (view profile)

cant I use another set of Images ?If yes then what change is required in which part of the code

Comment only
18 Jun 2013 David Young

David Young (view profile)

katia - sorry not to get back sooner. I hope you've solved the problem by now. It's easy - just look at the code that does the display for the demo and modify it to do what you want.

Comment only
12 Sep 2012 katia subeva

Is there any way to display the feature points in every one of the two images before showing the correspondence between them?

Comment only
27 Nov 2011 Garry

Garry (view profile)

30 Jun 2011 Lyuboslav

16 Feb 2011 David Young

David Young (view profile)

vamsi - It is possible to get an intermediate view using these correspondences, but how successful it will be depends on the complexity of the flow field. Extending the demo in the FEX package, you can do something like this:

x1 = cc.corresps(1,:);
y1 = cc.corresps(2,:);
x2 = cc.corresps(3,:);
y2 = cc.corresps(4,:);
input_points = [x1' y1'];
base_points = [(x1+x2)' (y1+y2)']/2; % half way between matchine features
% Generate transform from image1 to half-way position
tform = cp2tform(input_points, base_points, 'piecewise linear');
% Apply it to image 1
interp = imtransform(image1, tform, 'Xdata', [1 size(image1,2)], 'Ydata', [1 size(image1,1)]);

to get an image intermediate between the two images. I've found that for complex flow fields a piecewise linear transform can't be found, but the lwm option can work. I can't make a recommendation though - you have to experiment with your own data set.

Comment only
15 Feb 2011 vamsi

vamsi (view profile)

david-I am trying to get an intermediate view between these two images with out disparity map. So i need to know the amount by which each pixel has been moved. Can i go for image morphing with these available sparse set? In such a case, which morphing technique would you suggest..

Comment only
15 Feb 2011 David Young

David Young (view profile)

vamsi - I'm glad it's helpful. I am sorry, but I don't know the best way to make a dense set - that's a difficult problem, and there's a lot of research literature to look at. Interpolation is not good because the flow field is discontinuous, and so it's necessary to go back to the original images to do segmentation. Sorry not to be more help, but it's a big question.

Comment only
14 Feb 2011 vamsi

vamsi (view profile)

Hi david. Thankyou so mcuh for this work. It really helped me a lot. After getting the correspondences, how can i make it dense set from the sparse set obtained. I tried some interpolation techniques. But they did not worked. Please help me

26 May 2010 David Young

David Young (view profile)

Marco - It's necessary to convert rgb images to greyscale (using rgb2gray).

Comment only
25 May 2010 Marco Marienberg

David - So what was the resolution since I'm getting the same errors that Michael did. I am running 7.9.0 (R2009b).

Comment only
24 May 2010 David Young

David Young (view profile)

Note to potential users: the issue identified by Michael was resolved between us - there isn't a problem with the code.

Comment only
09 May 2010 David Young

David Young (view profile)

That's puzzling! What does "which conv2 -all" print?

Comment only
08 May 2010 Michael Coughlin

Hi again,

Forward matches: ??? Undefined function or method 'conv2' for input arguments of
type 'double'
and attributes 'full 3d real'.

Error in ==> convolve2>doconv at 95
y = conv2(conv2(x, u(:,1)*s(1), shape), vp(1,:), shape);

Error in ==> convolve2 at 71
y = doconv(x, m, shape, tol);

Error in ==> patch_var at 29
a = convolve2(x, m, shape);

Error in ==> varPeaks at 17
vars = patch_var(im, patchsize);

Error in ==> correlCorresp.correlCorresp>correlCorresp.findFeatures at 466
[r, c] = varPeaks(cc.im1, cc.fPS, cc.rT);

Error in ==> correlCorresp.correlCorresp>correlCorresp.findCorrespsFwd at 545
cc = cc.findFeatures;

Error in ==> correlCorresp.correlCorresp>correlCorresp.findCorresps at 489
cc = cc.findCorrespsFwd;

Error in ==> correspDemo_1 at 38
cc = cc.findCorresps;

>> conv2
??? Error using ==> conv2
Not enough input arguments.

So I see conv2 but it does not seem to like it, might you know why?


Comment only
08 May 2010 Michael Coughlin

Ah silly me. 2010a, right above. I will give that a go and see how if that fixes it.

Comment only
08 May 2010 David Young

David Young (view profile)

Hi Michael,

I'm not sure what the problem is, but I wonder if it's a version problem - what version of Matlab are you using?

You don't need to put the files in a special "@" directory - they just go in a folder on your path as normal. But I don't think that in itself would cause the error you observe.


Comment only
08 May 2010 Michael Coughlin

Hi David,

So I am a little new to class definitions in Matlab, but I placed correlCorresp.m in a folder @correlCorresp and modified the demo to include 2 of my .png files.

>> correspDemo_1
??? Error: File: /mnt/qfs4/mcoughlin/IM/daily/2010_05_01/plots/crossCorr/@correl
Corresp/correlCorresp.m Line: 190 Column: 28
Undefined function or variable 'private'.

Error in ==> correspDemo_1 at 24
cc = correlCorresp('image1', image1, 'image2', image2, 'printProgress', 100);

So that means it is complaining about this line:
properties (Dependent, SetAccess = private)

Do you happen to know what I am doing incorrectly?

Thank you for your help,

Comment only
29 Apr 2010 David Young

David Young (view profile)

Thanks, Ulrich. I don't have a publication on this code, so no real reference, though the SVD trick that speeds up the correlations is described here: 'Computer and Robot Vision' Vol I, by R.M. Haralick and L.G. Shapiro (Addison-Wesley 1992), pp. 298-299.

Subpixel displacements: yes, I've been thinking about this, but I haven't done it yet.

Comment only
28 Apr 2010 Ulrich

Ulrich (view profile)

Wow, this is great. I'm trying to get similar results for landslide displacements using DEMs quite some time now. And this is faster, more reliably and gave me results straight ahead. Thanks for this. Now I have to modify it a bit for subpixel displacements. The reverse matching sort out nicely mismatches in my case. Is there any reference I can use when I include this in my thesis?

19 Apr 2010 1.1

Code tidied; demo extended.

21 May 2010 1.2

Revised the properties that control user-specified feature locations and propagation of features in image sequences.

09 Dec 2011 1.3

Minor updates to comments. Update to method used by findpeaks, which now calls imregionalmax.

Contact us