OpenCV деформируется от одного треугольника к другому
Я хотел бы отобразить один треугольник внутри мата OpenCV на другой, почти так же, как это делает warpAffine (проверьте здесь), но для треугольников вместо четырехугольников, чтобы использовать его в триангуляции Делоне.
Я знаю, что можно использовать маску, но я хотел бы знать, есть ли лучшее решение.
С наилучшими пожеланиями, Даниэль
2 ответа
Вы должны использовать getAffineTransform, чтобы найти преобразование, и использовать warpAffine, чтобы применить его
Я скопировал вышеупомянутое изображение и следующий код C++ из моего треугольника деформации после другого, используя OpenCV (C++ / Python). Комментарии в коде ниже должны дать хорошее представление о том, что происходит. Для получения более подробной информации и кода Python вы можете перейти по ссылке выше. Все пиксели внутри треугольника tri1 в img1 преобразуются в треугольник tri2 в img2. Надеюсь это поможет.
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;
}