change imref2d so that image is in center

5 views (last 30 days)
Katharina Heitger
Katharina Heitger on 5 Dec 2021
I have asked in a previous question how I can change the origin of a transformationmatrix for application. The initial situation was that it seemed that the transformationmatrix was apllied onto a simulated image with the wrong origin. For information the simulated image contains a plane as an object. So the image after tranfomration should be a square beginning at matrix indeces approx. (26;26). And the image is the size of 2049x2049px. The previous question can be found here: https://de.mathworks.com/matlabcentral/answers/1598324-transformation-matrix-projective2d-change-of-origin-at-application?s_tid=srchtitle
Now with the help of these answers and additional research I applied different parameters of imwarp() and got this far:
With
pixelExtendInWorldX=(203.53*2)/2049; %I know that the image plane in real world is in x-direction 203.53*2 mm long.
pixelExtendInWorldY=(1390-984.41)/2049; %the distancce in y-direction is 1390-984.41
outputimref2d=imref2d(size(origimage), pixelExtendInWorldX, pixelExtendInWorldY);
realdectcorroutputview2 = imwarp(origimage,tmatrix, 'nearest','OutputView', outputimref2d);
With this I get an result which begins at matrix indeces (93,92) and is then cut off. So it appears that the center is too much in the lower right direction. But the result seems to be a square (the desired shape) as the visible edges are straight lines. As the image is cut off only two of four edges are visible.
I tried the answers from my first question as well, which I wrote as
xWorldLimits2=[-203.54 203.54];
yWorldLimits2=[984.41 1390];
outputimref2dwl=imref2d(size(origimage) ,xWorldLimits2,yWorldLimits2);
realdectcorroutputview2wl = imwarp(origimage,tmatrix, 'nearest', 'OutputView', outputimref2dwl);
This resulted in an image, beginning at indeces of (1117/1118), going straight downwards, but having a protruding edge at the end in the right direction from this image.
I also tried
xWorldLimits2=[-203.54 203.54];
xWorldLimits2=xWorldLimits2-mean(xWorldLimits2);
yWorldLimits2=[984.41 1390];
yWorldLimits2=yWorldLimits2-mean(yWorldLimits2);
outputimref2dwl=imref2d(size(origimage) ,xWorldLimits2,yWorldLimits2);
realdectcorroutputview2wl = imwarp(origimage,tmatrix, 'nearest', 'OutputView', outputimref2dwl);
which did not really change the result.
And
Rin=imref2d(size(origimage));
Rin.XWorldLimits=Rin.XWorldLimits-2*mean(Rin.XWorldLimits);
Rin.YWorldLimits=Rin.YWorldLimits-2*mean(Rin.YWorldLimits);
realdectcorrRin = imwarp(origimage,Rin, tmatrix, 'nearest');
did not work at all.
So the first attempt seems to yield the best results yet, but how can I additionally change (supposably) the imref2d-object, that the final image is not cut off anymore, but more centered?
Any help is appreciated.
  5 Comments
Katharina Heitger
Katharina Heitger on 6 Dec 2021
Here are some simple sketches, how the matrices look like. idealdect200 is the desired result and realdect200 is the image to be tranformed. The other are named like in the code. Ending at (2049/2049) indicates that there is a cut off. realdectcorroutputview2v2 is not sketched, as it only contains 0.

Sign in to comment.

Answers (2)

yanqi liu
yanqi liu on 6 Dec 2021
yes,sir,which tmatrix used in your code,may be upload the file and do some analysis,such as
clear all;clc;close all;
origimage = imread('football.jpg');
Rin=imref2d([size(origimage,1) size(origimage,2)]);
Rin.XWorldLimits=Rin.XWorldLimits-2*mean(Rin.XWorldLimits);
Rin.YWorldLimits=Rin.YWorldLimits-2*mean(Rin.YWorldLimits);
tmatrix = affine2d([1 0 0; .5 1 0; 0 0 1]);
realdectcorrRin = imwarp(origimage,Rin, tmatrix, 'nearest');
figure; montage({origimage,realdectcorrRin}, 'Size', [1 2], 'BackgroundColor', 'w', 'BorderSize', [2 2]);

Matt J
Matt J on 6 Dec 2021
Edited: Matt J on 6 Dec 2021
The reason the output doesn't look like a square is because your tmatrix is wrong, not because of anything else:
load 'SimulationDataMathworks0612.mat';
origimage=realdect200; %or realdet1802028
mp=fliplr( [19,19; 19 2031; 2034 2034; 2034 16] );
fp=fliplr( [26,26; 26 2024; 2024 2024; 2024 26] );
tform=fitgeotrans(mp,fp,'projective');
realdectcorrRin = imwarp(origimage, tform, 'nearest');
imshow(realdectcorrRin,[])
  3 Comments
Katharina Heitger
Katharina Heitger on 8 Dec 2021
I read that, but I completly forgot about that. But my input data for fitgeotrans() are world coordinates as you described. So at least that is correct.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by