我通过使用getHomography和warpPerspective将图像从前视角更改为出价视图.
它的工作原理是图像扭曲到所需的视角,但裁剪关闭.它将扭曲的图像移动到图像框之外.我假设原因是因为操作导致负坐标.
我已经手动计算了翻译矩阵的计算点,而不是使用任何opencv:s函数来计算,因为棋盘函数未能检测到正确的点.
我想这可以通过对转换矩阵进行其他更改来解决.但那怎么办?另外,有没有办法确保变换后的图像沿x轴居中,然后将y轴调整到所需位置?
现在完成工作的代码段:
cv::Mat image; // image is loaded with the original image
cv::Mat warpPers; // The container for the resulting image
cv::Mat H;
std::vector<cv::Point2f> src;
std::vector<cv::Point2f> dst;
// In reality several more points.
src.push_back(cv::Point2f(264,301));
src.push_back(cv::Point2f(434,301));
src.push_back(cv::Point2f(243,356));
src.push_back(cv::Point2f(476,356));
dst.push_back(cv::Point2f(243,123));
dst.push_back(cv::Point2f(476,123));
dst.push_back(cv::Point2f(243,356));
dst.push_back(cv::Point2f(476,356));
H = cv::findHomography(src, dst, CV_RANSAC);
cv::warpPerspective(image,
newPers,
H,
cv::Size(3000,3000),
cv::INTER_NEAREST | CV_WARP_FILL_OUTLIERS
);
cv::namedWindow("Warped persp", cv::WINDOW_AUTOSIZE );
cv::imshow( "Warped persp", newPers);
Run Code Online (Sandbox Code Playgroud) 我使用了四组点来获得一个PerspectiveTransform矩阵t.然后使用warpPerspective将Mat A转换为Mat B.从Mat A中指出一个点.我想让新的Point在Mat B.中获得一个位置但是warpPerspective()不能在透视透射变换时可以做到这一点.在这里,我想通过使用warpPerspective来了解perspectiveTransform中的位置是否与Mat B中的位置相同.那么,warpPerspective和perspectiveTransform之间的区别是什么?
Mat trans = getPerspectiveTransform(dst, gpsPoints);
warpPerspective(A, B, trans, image.size());
Point2f a = Point2f(..., ...); //were known
vector<Point2f> obj(1);
obj[0] = a;
vector<Point2f> b;
perspectiveTransform(obj, b, trans);//if the new point in B is c, is c equals to b?
Run Code Online (Sandbox Code Playgroud) 我尝试为warpPerspective()函数指定一个与基本(0,0)不同的原点,以便独立于支持图像大小应用变换.我在原始代码中添加了一个CvPoint参数,但是我找不到使用这些坐标的位置.我试图在X0,Y0和W0的计算中使用它们,但它不起作用,这只是在生成的图像中移动变换后的图像.任何的想法?
这里的代码:
void warpPerspective( const Mat& src, Mat& dst, const Mat& M0, Size dsize,
int flags, int borderType, const Scalar& borderValue, CvPoint origin )
{
dst.create( dsize, src.type() );
const int BLOCK_SZ = 32;
short XY[BLOCK_SZ*BLOCK_SZ*2], A[BLOCK_SZ*BLOCK_SZ];
double M[9];
Mat _M(3, 3, CV_64F, M);
int interpolation = flags & INTER_MAX;
if( interpolation == INTER_AREA )
interpolation = INTER_LINEAR;
CV_Assert( (M0.type() == CV_32F || M0.type() == CV_64F) && M0.rows == 3 && M0.cols == 3 );
M0.convertTo(_M, _M.type());
if( !(flags & …Run Code Online (Sandbox Code Playgroud)