Thread Subject: imtransform question

Subject: imtransform question

From: kees de Kapper

Date: 14 Oct, 2011 12:18:29

Message: 1 of 7

Dear all,

probably an easy question for the diehards but I'm struggling a while already.

I'm trying to transform an image using imtransform. The main idea is that I've got a large image, which I will register/match to another image. Therefore I just select a small ROI to perform the registration (using an optimization). Subsequently, this found transformation is applied to the entire (large) image. However, the resulting image is not matched, even if rotations were not taken into account.

To demonstrate my problem, I simplified my code to the following:
I. first cropping, then imtransform,
II. first imtransform (use the same transformation), then cropping.

Why doesn't give the two (apparently similar) routes the same result? And how can I solve this?

Many thanks in advance,
Kees.

%***************************************************************
function main()
Img = zeros(400,500); Img(100:200,200:400) = 1; % test image

ClipBox = [50,size(Img,1)-50;50,size(Img,2)-50]; %[vert0,vert1,hor0.hor1]
dx = 100;
dy = -100;
dr = 0;
Ax1 = 1:size(Img,1); % coordinates of the axes
Ax2 = 1:size(Img,2);

% Transform image and then do clipbox
ImgT0 = TransformImage(Img, dx,dy,dr, Ax1,Ax2);
ImgT0 = ImgT0(ClipBox(1,1):ClipBox(1,2),ClipBox(2,1):ClipBox(2,2));

% Do clipbox and then transform image
Img = Img(ClipBox(1,1):ClipBox(1,2),ClipBox(2,1):ClipBox(2,2));
Ax1 = Ax1(ClipBox(1,1):ClipBox(1,2));
Ax2 = Ax2(ClipBox(2,1):ClipBox(2,2));
ImgT1 = TransformImage(Img, dx,dy,dr, Ax1,Ax2);

figure(2);
subplot(3,2,1); imagesc(Img); colormap(gray); title('original image cropped')
subplot(3,2,3); imagesc(ImgT0); colormap(gray); title('transformed image then cropped')
subplot(3,2,5); imagesc(ImgT1); colormap(gray); title('cropped original image then transformed')
subplot(3,2,6); imagesc(ImgT0-ImgT1); colormap(gray); title('difference two transf. images');


function ImgT = TransformImage(Img, dx,dy,dr, Ax1, Ax2)
udata = [Ax1(1), Ax1(end)];
vdata = [Ax2(1), Ax2(end)];

tform = maketform('affine',[cos(dr/180*pi) -sin(dr/180*pi) 0; ...
                            sin(dr/180*pi) cos(dr/180*pi) 0; ...
                            dx dy 1]);
[ImgT] = imtransform(Img, tform, 'linear', ...
                     'udata', udata,'vdata', vdata,...
                     'xdata', udata,'ydata', vdata,...
                     'size', size(Img), 'fill', NaN);

%***************************************************************

Subject: imtransform question

From: HB

Date: 14 Oct, 2011 13:31:16

Message: 2 of 7

On 14 Okt., 14:18, "kees de Kapper" <kees_de_kap...@hotmail.com>
wrote:
> Dear all,
>
> probably an easy question for the diehards but I'm struggling a while already.
>
> I'm trying to transform an image using imtransform. The main idea is that I've got a large image, which I will register/match to another image. Therefore I just select a small ROI to perform the registration (using an optimization). Subsequently, this found transformation is applied to the entire (large) image. However, the resulting image is not matched, even if rotations were not taken into account.
>
> To demonstrate my problem, I simplified my code to the following:
> I. first cropping, then imtransform,
> II. first imtransform (use the same transformation), then cropping.
>
> Why doesn't give the two (apparently similar) routes the same result? And how can I solve this?
>
> Many thanks in advance,
> Kees.
>
> %***************************************************************
> function main()
> Img = zeros(400,500); Img(100:200,200:400) = 1; % test image
>
> ClipBox = [50,size(Img,1)-50;50,size(Img,2)-50]; %[vert0,vert1,hor0.hor1]
> dx = 100;
> dy = -100;
> dr = 0;
> Ax1 = 1:size(Img,1); % coordinates of the axes
> Ax2 = 1:size(Img,2);
>
> % Transform image and then do clipbox
> ImgT0 = TransformImage(Img, dx,dy,dr, Ax1,Ax2);
> ImgT0 = ImgT0(ClipBox(1,1):ClipBox(1,2),ClipBox(2,1):ClipBox(2,2));
>
> % Do clipbox and then transform image
> Img = Img(ClipBox(1,1):ClipBox(1,2),ClipBox(2,1):ClipBox(2,2));
> Ax1 = Ax1(ClipBox(1,1):ClipBox(1,2));
> Ax2 = Ax2(ClipBox(2,1):ClipBox(2,2));
> ImgT1 = TransformImage(Img, dx,dy,dr, Ax1,Ax2);
>
> figure(2);
> subplot(3,2,1); imagesc(Img); colormap(gray); title('original image cropped')
> subplot(3,2,3); imagesc(ImgT0); colormap(gray); title('transformed image then cropped')
> subplot(3,2,5); imagesc(ImgT1); colormap(gray); title('cropped original image then transformed')
> subplot(3,2,6); imagesc(ImgT0-ImgT1); colormap(gray); title('difference two transf. images');
>
> function ImgT = TransformImage(Img, dx,dy,dr, Ax1, Ax2)
> udata = [Ax1(1), Ax1(end)];
> vdata = [Ax2(1), Ax2(end)];
>
> tform = maketform('affine',[cos(dr/180*pi) -sin(dr/180*pi) 0; ...
>                             sin(dr/180*pi) cos(dr/180*pi) 0; ...
>                             dx dy 1]);
> [ImgT] = imtransform(Img, tform, 'linear', ...
>                      'udata', udata,'vdata', vdata,...
>                      'xdata', udata,'ydata', vdata,...
>                      'size', size(Img), 'fill', NaN);
>
> %***************************************************************

Have you tried the cpselect tool? It's really good for finding
Transformation points.

Subject: imtransform question

From: kees de Kapper

Date: 14 Oct, 2011 14:21:13

Message: 3 of 7

thanks for replying. CP select has a different purpose then greyscale pixel registration that I want to do. I don't want to (can't) detect clear features. Moreover it won't be to accurate enough.


HB <mister.hb@gmail.com> wrote in message <01ecfe7b-aedd-4428-a4b4-904d2f06d80c@i19g2000yqa.googlegroups.com>...
> Have you tried the cpselect tool? It's really good for finding
> Transformation points.

Subject: imtransform question

From: kees de Kapper

Date: 16 Oct, 2011 09:17:10

Message: 4 of 7

kick

Subject: imtransform question

From: Matt J

Date: 16 Oct, 2011 14:05:11

Message: 5 of 7

"kees de Kapper" <kees_de_kapper@hotmail.com> wrote in message <j799al$qd6$1@newscl01ah.mathworks.com>...
>
> Why doesn't give the two (apparently similar) routes the same result? And how can I solve this?
==========

Because you have the roles of Ax1 and Ax2 reversed. Ax1 corresponds to the y axis while Ax2 corresponds to the x axis. Simply change the signature of Transform Image to

function ImgT = TransformImage(Img, dx,dy,dr, Ax2, Ax1)

and you will see that the problem is fixed.

Subject: imtransform question

From: <address_is@invalid.invalid>

Date: 16 Oct, 2011 19:08:54

Message: 6 of 7

Kick

Subject: imtransform question

From: kees de Kapper

Date: 17 Oct, 2011 12:20:15

Message: 7 of 7

Matt J, thanks! that was the trick!
Although I'm reasonably experienced with Matlab, I have been struggling with this issue for a while. Now, it is finally solved. So easy!

/Kees

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
xy versus ij Matt J 16 Oct, 2011 10:13:48
imtransform kees de Kapper 14 Oct, 2011 08:19:33
rssFeed for this Thread

Contact us at files@mathworks.com