You are on page 1of 5

Lule Tekniska Universitet

Anita Enmark
R7011R 2014

Assignment 1
GEOMETRIC CORRCETION
Control Points

Exercise 1: Geometric correction with control points.


Goal:
In this exercise you shall get familiar with a method for geometric correction using
polynomial models for transformation. You shall try to correct an image f(x,y) by
finding the polynomials p and q that maps the points (xref,yref) in a scene to the points
(x,y) in an image with geometric distortion
x=p(xref,yref)
y=q(xref,yref)
You can assume shifting and scaling in x- and y-direction, and in example 2, rotation.
You shall determine the coefficients in the polynomial with the help of control points
marked in a map containing the image area.
When the coefficients in the polynomials are determined, the image is resampled. For
simplicity we have chosen nearest neighbour resampling.
You shall test the result when using different number of control points (determined
and over determined systems), and for the case of a misplaced control point and the
wrong polynomial.
Preparations:
Fetch the material for the lab from the course room: This instruction , geocorr.mat and
cntrfit.m.
Study the lecture notes and chapters in the book. Choose suitable polynomials for the
correction (knowing the type of distortion and also by looking at the images).
Report:
You shall work in groups of two students. You must convince yourself that you
understand the different steps in the correction. A report template will be available on
the course room. You must follow the template!
The report should include an image showing the best correction you could achieve.
You should also include: What polynomials you have assumed. Your control points
for the different cases, the coefficients and the error vectors. It shall also include
comments on the results. Hand in one report/group named
Geometric<studentname>.
Instructions:
The map (template) and the distorted image are saved as MATLAB matrixes on the
course home page (geocorr.mat) . You can load them into MATLAB using the load
command:
load geocorr
who
Your variables are:
etest
e
r

r and e are images of the school building. r is the reference (map with control
points) and e the image to be corrected. In order to get familiar with the procedure
you can test with an image etest with a simpler distortion (just scaling and translation)
PART I

1.
First try with etest :
The image etest is only scaled and shifted in x- and y-direction (compared to r) so the
polynomials are
x=a0 + a1xref
y=b0 + b1yref
In cntrfit.m (download from course home page) Matlabs polyfit is used to
calculate the coefficients. When you correct the image e later, you must change this
part of the code so it use matrixes (as in the examples from the lectures) and change
the polynomial model to include rotation.
Study the images and choose good control points. Try with both 2 och 4 points .
Mark the control points in the two images with the Matlab function ginput,
NOTE! y-coordinate first:
imshow(r,[])
[yr,xr]=ginput(2) %click on two points with the mouse
yr =
78.5600
122.9018
xr =
23.0751
67.4169
And then the other image:
imshow(etest, []); %[]=> contrast enhancement
[ye,xe]=ginput(2)
ye =
53.8035
105.9443
xe =
50.1774
58.7991
This example gives the control points:
(xref,yref)
CP#1
CP#2

( 23.0751 , 78.56)
(67,4169 , 122.9018)

(x,y)
(50.1774 , 53.8035)
(58.7991 , 105.9443)
3

Resample with the file cntrfit.m (on course room). The file content is also
appended in this instruction. Study the file. MATLAB has library routines for this, but
they are hard to understand. Remember that this course is not on numerical methods,
but on basic image processing tools and methods. The code is not written for speed
and smartness but for clarity of the method.
Calls to cntrfit.m is done by:
np=cntrfit(etest,[xr yr],[xe ye],size(r));
The new image np can be viewed with imshow. Compare the result with the map r
(you can for example look at the image (r-np). Is the correction good?? Do you find
the 4 point image better than the 2 point image?
Inside cntrfit.m the polynomial coefficients is calculated with the help of
MATLABs polyfit function (change this to pseudo inverse with matrixes in Part
II):
polyfit(xr,xe,1)
ans =
0.1944
45.6908
polyfit(yr,ye,1)
ans =
1.1759 -38.5739

%1 means fitting to 1st degree polynomial


% a1 and a0

% b1 and b0

PART II
Now do the same with the rotated, shifted and scaled image e. Make the necessary
changes in ctrlfit.m so you get the right polynomial model. You shall use
matrices, not polyfit.
2. Decide what polynomial model to use.

3. First test with a minimum of control points (how many ?) and the correct
polynomial
Look at the error vectors between the points calculated with the polynomials and the
marked ones e=||WA-X||2, when using a minimum number of control point. Discuss
the error in other places in the image.

4. Then use an over determined system.


Do the same but with more control points.

5.
Do the same but misplace one of the control points.

6.
Repeat (3-5) for an incorrect polynomial (to many coefficients than necessary)
function [newp]=cntrfit(oldp,ref,p,coords);
%Cntrfit corrects the image oldp using a polynomial model.
%The function can correct for shifting and scaling in x and y :
%
xp=a0+a1*xref
%
yp=b0+b1*yref
%ref holds the control points from reference and shall be on the
%format [xpref ypref]
%p holds the control points from distorted and shall be on the
%format [xp yp]
%coords holds the size of the corrected image [rows cols]
%"nearest neighbour" resampling is used

xpref=ref(:,1);ypref=ref(:,2);
xp=p(:,1);yp=p(:,2);
a=polyfit(xpref,xp,1);
a0=a(2)
a1=a(1)
b=polyfit(ypref,yp,1);
b0=b(2)
b1=b(1)
[maxrow,maxcol]=size(oldp);
rows=coords(1); %Number of rows in the corrected image
cols=coords(2); %Number of columns in the corrected image
for xref=1:rows,
for yref=1:cols,
xnear=round(a0+a1*xref);%search for nearest x-coordinate
ynear=round(b0+b1*yref);%search for nearest y-coordinate
if ((xnear<=maxrow) & (ynear<=maxcol) & (ynear>0) &
(xnear>0))
%If nearest pixel within old image use GL(xnear,ynear)
newp(xref,yref)=oldp(xnear,ynear);
else
%If not, pixel in corrected image is set to black
newp(xref,yref)=0;
end;
end;
end;

You might also like