Professional Documents
Culture Documents
Anita Enmark
R7011R 2014
Assignment 1
GEOMETRIC CORRCETION
Control Points
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
% 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.
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;