Why is imwarp not getting the birdsEyeView image I'm expecting?

조회 수: 10(최근 30일)
cui 2022년 7월 21일
편집: cui 2022년 9월 21일 12:17
I want to do a birds eye image conversion on a monocular car camera image, I have made some adaptations based on the official "birdsEyeView" function documentation example and used 3 ways to transform the image, but why is the imwarp used in method 1 not the same as the other methods, why? your answer is greatly appreciate.
%% 测试图像只针对部分区域做birdsEyeView图
I = imread("road.png");
focalLength = [309.4362 344.2161];
principalPoint = [318.9034 257.5352];
imageSize = [480 640];
camIntrinsics = cameraIntrinsics(focalLength,principalPoint,imageSize);
height = 2.1798;
pitch = 14;
sensor = monoCamera(camIntrinsics,height,'Pitch',pitch);
distAhead = 30;
spaceToOneSide = 6;
bottomOffset = 3;
outView = [bottomOffset,distAhead,-spaceToOneSide,spaceToOneSide];
outImageSize = [NaN,250];
birdsEye = birdsEyeView(sensor,outView,outImageSize);
BEV = transformImage(birdsEye,I);
%% 实验
worldPts = [outView(1),outView(4);% 左下
outView(1),outView(3);% 右下
outView(2),outView(3);% 右上
outView(2),outView(4)];% 左上
% 推测目标图像大小
targetImgWidth = 250;
xLims = outView(2)-outView(1);
yLims = outView(4)-outView(3);
outImageSize = [xLims/yLims*targetImgWidth,targetImgWidth];
h = outImageSize(1);
w = outImageSize(2);
fixedPts = [1,h;
1,1];% 顺序与worldPts必须对应
% 单目摄像头图像上对应点坐标绘图
monoImgPts = vehicleToImage(birdsEye.Sensor,worldPts);
I = insertMarker(I,monoImgPts);
I = insertText(I,monoImgPts,1:4);
src = insertShape(I,"FilledPolygon",monoImgPts,Opacity=0.3);
title("source image")
%% transform image
% method1: use imwarp
tform = fitgeotrans(monoImgPts,fixedPts,"projective");
dstImg = imwarp(src,tform);
title("method1: use imwarp")
% method2: interpolation
[bdsX,bdsY] = meshgrid(1:w,1:h);
[u,v] = transformPointsInverse(tform,bdsX(:),bdsY(:));
mapX = reshape(u,size(bdsX));
mapY = reshape(v,size(bdsY));
src = im2double(src);
birdsEyeImgR = interp2(src(:,:,1),mapX,mapY,"linear",0);
birdsEyeImgG = interp2(src(:,:,2),mapX,mapY,"linear",0);
birdsEyeImgB = interp2(src(:,:,3),mapX,mapY,"linear",0);
birdsEyeImg = cat(3,birdsEyeImgR,birdsEyeImgG,birdsEyeImgB);
figure; imshow(birdsEyeImg)
title("method2: interpolation")
% method3: use transformImage
BEV = transformImage(birdsEye,src);
title("method3: use transformImage")

채택된 답변

cui 2022년 7월 22일
편집: cui 2022년 9월 21일 12:17
After my personal experiments, imwarp by default transforms the whole image, if you need to transform a local ROI image, you need to specify the "OutputView" parameter option, which is used to specify the size and location of the output image.
load param.mat % required parameters
outputView = imref2d(floor(outImageSize));
dstImg = imwarp(src,tform,OutputView=outputView);
title("imwarp method")

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by