本文介绍了cv :: warpPerspective只显示部分的翘曲图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 通过使用getHomography和warpPerspective,将前景视图更改为出价视图。 它的作用在于图像扭曲到所需的视角,已关闭。它将变形的图像大部分移动到图像框之外。我假设原因是因为操作结果为负坐标。 我已经计算了用于计算平移矩阵的点,而不是通过使用任何opencv:s函数因为棋盘功能未能检测到正确的点。 我想这可以通过对转换矩阵进行额外的更改来修复。但是怎么做呢?此外,有没有办法确保变换的图像沿x轴居中,然后让y轴调整到所需的位置? 代码段现在执行的工作: cv :: Mat image; // image加载了原始图像 cv :: Mat warpPers; //生成的图像的容器 cv :: Mat H; std :: vector< cv :: Points2f> src; std :: vector< cv :: Point2f> dst; //在现实中还有几个点。 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); 解决方案 Opencv提供了非常方便的方式进行透视变换。你唯一要做的是照顾单数回归findHomography。 的确,你提供的图像的一些点可以在x轴或y轴的负部分。 所以你必须在扭曲图像之前做一些检查。 步骤1:用findHomography 找到单应性H你会得到一个经典结构对于单应性 H = [h00,h01,h02; h10,h11,h12; h20,h21,1]; 步骤2:在变形后搜索图像角落的位置 因此,让我定义角的顺序: (0,0)________(0,w) | | | ________ | (h,0)(h,w) 矩阵如下: P = [0,w,w,0; 0,0,h,h; 1,1,1,1] 使用H制作产品,坐标: P'= H * P 步骤3:用这4个新的点检查x和y的最小值,得到变形图像的大小之后,你已经完成了产品,你会收到类似的东西: P'= [s1 * x1,s2 * x2,s3 * x3,s4 * x4; s1 * y1,s2 * y2,s3 * y3,s4 * y4; s1,s2,s3,s4] 行1和2的第3行 之后,检查第一行的列的最小值,第二行的行的最小值(使用cvReduce) 找到将包含图像的边界框(即warpPerspective函数的dst矩阵的维度),使用cvReduce查找每行的最大值 let minx是第一行(即列)的最小值,maxx(第一行的最大值) miny和maxy的第二行。 p> 因此,变形图像的大小应为cvSize(maxx-minx,maxy-miny) 步骤4:添加对单应性的校正检查minx和/或miny是否为负,如果minx ,因此H应为: h10,h11,h12-miny; // if miny< 0 h20,h21,1]; 步骤5:扭曲图像 Im changing an image from front perspective to a bids eye view by using getHomography and warpPerspective.It works in that the image warps to the desired perspective but the crop is off. It moves the warped image largely outside the image box. I assume the reason is because the operation results in negative coordinates.I have calculated the points for calculation of the translation matrix manually and not by using any of opencv:s functions for doing that since i.e. the chessboard functions failed to detect the proper points.I guess this can be fixed by doing additional changes to the transformation matrix. But how is that done? Also, is there a way to make sure the transformed image is centered along the x-axis and then let the y-axis be adjusted to a desired position?Code snippet that does the job now:cv::Mat image; // image is loaded with the original imagecv::Mat warpPers; // The container for the resulting imagecv::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); 解决方案 Opencv gives very convenient way to do perpective transform. The only thing you have to do is take care of the homography return by findHomography.Indeed, maybe some points of the image you provide go in the negative part of the x or y axis.So you have to do some check before warp the image.step 1: find the homography H with findHomographyyou will get a classic structure for homographyH = [ h00, h01, h02; h10, h11, h12; h20, h21, 1];step 2: search the position of image's corners after warpingSo let me define the order for the corner:(0,0) ________ (0, w) | | |________|(h,0) (h,w)To do that, just create a matrix like that:P = [0, w, w, 0; 0, 0, h, h; 1, 1, 1, 1]Make the product with H and get the warped coordinates: P' = H * Pstep 3: check the minimum in x and y with these new 4 points and get the size of warped imageAfter, you have done the product you will receive something like that:P' = [s1*x1, s2*x2, s3*x3, s4*x4; s1*y1, s2*y2, s3*y3, s4*y4; s1 , s2 , s3 , s4]So to obtain, new valid coordinate just divide line 1 and 2 by the line 3After that check the minimum for the column on the first line, and the minimum for the row on the second line (use cvReduce)to find the bounding box that will contains the image (ie the dimension of the dst matrix for the warpPerspective function) just find with cvReduce the maximum over each linelet minx be the minimum on the first row (ie for column), maxx (the maximum for the 1 row)miny and maxy for the second row.So the size of the warped image should be cvSize(maxx-minx, maxy-miny)step 4: add a correction to the homographyCheck if minx and/or miny is/are negative, if minx < 0 then add -minx to h02 and if miny < 0, then add -miny to h12so H should be:H = [ h00, h01, h02-minx; //if minx <0 h10, h11, h12-miny; //if miny <0 h20, h21, 1];step 5: warp the image 这篇关于cv :: warpPerspective只显示部分的翘曲图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-01 17:10