![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/824155/image.png)
change imref2d so that image is in center
6 次查看(过去 30 天)
显示 更早的评论
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.
回答(2 个)
yanqi liu
2021-12-6
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]);
0 个评论
Matt J
2021-12-6
编辑:Matt J
2021-12-6
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 个评论
Matt J
2021-12-7
编辑:Matt J
2021-12-7
But why do you use fliplr()? I mean I know what it does, but why do you use it here?
Because, the data for the vertices of your quadrilaterals were in matrix coordinates, but fitgeotrans and imwarp expect things in the coordinate conventions of the Image Processing Toolbox. In that system, the x-axis goes horizontally from left to right across the image and the y-axis runs vertically from top to bottom.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!