OpenCV从一个三角形变形到另一个三角形

时间:2012-04-11 06:05:14

标签: opencv triangulation affinetransform warp

我想将OpenCV Mat中的一个三角形映射到另一个三角形,非常类似于warpAffine(检查它here),但是对于三角形而不是四边形,以便在delaunay三角剖分中使用它。

我知道有人可以使用面具,但我想知道是否有更好的解决方案。

祝你好运, 丹尼尔

2 个答案:

答案 0 :(得分:5)

enter image description here 我已从帖子Warp one triangle to another using OpenCV ( C++ / Python )复制了上面的图片和以下C ++代码。下面代码中的注释应该可以很好地了解发生了什么。有关更多详细信息和python代码,您可以访问上面的链接。 img1中三角形tri1内的所有像素都转换为img2中的三角形tri2。希望这会有所帮助。

void warpTriangle(Mat &img1, Mat &img2, vector<Point2f> tri1, vector<Point2f> tri2)
{
    // Find bounding rectangle for each triangle
    Rect r1 = boundingRect(tri1);
    Rect r2 = boundingRect(tri2);

   // Offset points by left top corner of the respective rectangles
   vector<Point2f> tri1Cropped, tri2Cropped;
   vector<Point> tri2CroppedInt;
   for(int i = 0; i < 3; i++)
   {
      tri1Cropped.push_back( Point2f( tri1[i].x - r1.x, tri1[i].y -  r1.y) );
      tri2Cropped.push_back( Point2f( tri2[i].x - r2.x, tri2[i].y - r2.y) );

      // fillConvexPoly needs a vector of Point and not Point2f
      tri2CroppedInt.push_back( Point((int)(tri2[i].x - r2.x), (int)(tri2[i].y - r2.y)) );

   }

   // Apply warpImage to small rectangular patches
   Mat img1Cropped;
   img1(r1).copyTo(img1Cropped);

   // Given a pair of triangles, find the affine transform.
   Mat warpMat = getAffineTransform( tri1Cropped, tri2Cropped );

   // Apply the Affine Transform just found to the src image
   Mat img2Cropped = Mat::zeros(r2.height, r2.width, img1Cropped.type());
   warpAffine( img1Cropped, img2Cropped, warpMat, img2Cropped.size(), INTER_LINEAR, BORDER_REFLECT_101);

   // Get mask by filling triangle
   Mat mask = Mat::zeros(r2.height, r2.width, CV_32FC3);
   fillConvexPoly(mask, tri2CroppedInt, Scalar(1.0, 1.0, 1.0), 16, 0);

  // Copy triangular region of the rectangular patch to the output image
  multiply(img2Cropped,mask, img2Cropped);
  multiply(img2(r2), Scalar(1.0,1.0,1.0) - mask, img2(r2));
  img2(r2) = img2(r2) + img2Cropped;

}

答案 1 :(得分:0)

您应该使用getAffineTransform来查找转换,并使用warpAffine来应用它