我正在使用C ++中OpenCV 3.4.2中的ThinPlateSplineShapeTransformer
扭曲图像。另外,使用同一对象,我想使目标图像中的初始图像扭曲3个点。为了可视化转换,我使用了三个三角形:
扭曲的图像+单独的点变换,红色三角形应该与绿色三角形重叠。蓝色是最初的颜色。
代码:
void transform()
{
Mat img = imread("test.jpg"); // the posted original image
auto tps = cv::createThinPlateSplineShapeTransformer();
std::vector<cv::Point2f> sourcePoints, targetPoints, myPoints;
sourcePoints.push_back(cv::Point2f(0, 0));
targetPoints.push_back(cv::Point2f(100, 0));
sourcePoints.push_back(cv::Point2f(650, 40));
targetPoints.push_back(cv::Point2f(500, 0));
sourcePoints.push_back(cv::Point2f(0, 599));
targetPoints.push_back(cv::Point2f(0, 450));
sourcePoints.push_back(cv::Point2f(799, 599));
targetPoints.push_back(cv::Point2f(600, 599));
std::vector<cv::DMatch> matches;
for (unsigned int i = 0; i < sourcePoints.size(); i++)
matches.push_back(cv::DMatch(i, i, 0));
tps->estimateTransformation(sourcePoints, targetPoints, matches);
std::vector<cv::Point2f> transPoints, transPoints2;
//======== draw test points
myPoints.push_back(Point2f(100, 100));
myPoints.push_back(Point2f(200, 200));
myPoints.push_back(Point2f(100, 400));
line(img, myPoints[0], myPoints[1], Scalar(0, 255, 0), 3);
line(img, myPoints[1], myPoints[2], Scalar(0, 255, 0), 3);
line(img, myPoints[2], myPoints[0], Scalar(0, 255, 0), 3);
//========= warp image
Mat img2 = img.clone();
tps->warpImage(img, img2);
//========= warp points
tps->applyTransformation(myPoints, transPoints);
//tps->applyTransformation(transPoints2, transPoints);
line(img2, transPoints[0], transPoints[1], Scalar(0, 0, 255), 3);
line(img2, transPoints[1], transPoints[2], Scalar(0, 0, 255), 3);
line(img2, transPoints[2], transPoints[0], Scalar(0, 0, 255), 3);
//========== draw reference points
line(img2, myPoints[0], myPoints[1], Scalar(255, 0, 0), 3);
line(img2, myPoints[1], myPoints[2], Scalar(255, 0, 0), 3);
line(img2, myPoints[2], myPoints[0], Scalar(255, 0, 0), 3);
imshow("img", img);
imshow("img2", img2);
get_test_contur();
waitKey(0);
}
我不明白为什么结果(红色三角形)不与绿色三角形重叠。我想念什么?