實作在姿態辨識之後
利用opencv的透視轉換函式把圖片放到二維碼上
---
程式碼:
void putPicture(Mat intrinsic, Mat distortion, Vec3d rvec, Vec3d tvec, Mat _frame)
{
Mat frame = _frame.clone();
Mat img_input = imread(".//data//squirrel.jpg");
Mat img_perspective = Mat::zeros(frame.size(), frame.type());
float unit = 0.05;
Point3f pointO = Point3f(-unit, -unit, unit);
Point3f pointX = Point3f(-unit, unit, unit);
Point3f pointXY = Point3f(unit, unit, unit);
Point3f pointY = Point3f(unit, -unit, unit);
vector<Point3f> Point3D;
vector<Point2f> Point2D;
Point3D.push_back(pointO);
Point3D.push_back(pointX);
Point3D.push_back(pointXY);
Point3D.push_back(pointY);
projectPoints(Point3D, rvec, tvec, intrinsic, distortion, Point2D);
//求圖片的透視矩陣
Point2f srcTri[4];
srcTri[0] = Point2f(0, 0);
srcTri[1] = Point2f(img_input.cols - 1, 0);
srcTri[2] = Point2f(img_input.cols - 1, img_input.rows - 1);
srcTri[3] = Point2f(0, img_input.rows - 1);
Point2f dstTri[4];
dstTri[0] = Point2D[0];
dstTri[1] = Point2D[1];
dstTri[2] = Point2D[2];
dstTri[3] = Point2D[3];
//做圖片的透視轉換
Mat perspective_matrix = getPerspectiveTransform(srcTri, dstTri);
warpPerspective(img_input, img_perspective, perspective_matrix, img_perspective.size());
//把透視轉換後的圖片貼到視訊影像上
for (int r = 0; r < frame.rows; r++)
{
for (int c = 0; c < frame.cols; c++)
{
if ((img_perspective.at<Vec3b>(r,c)[0]!=0)&& (img_perspective.at<Vec3b>(r, c)[1] != 0)&& (img_perspective.at<Vec3b>(r, c)[2] != 0))
{
frame.at<Vec3b>(r, c)[0] = img_perspective.at<Vec3b>(r, c)[0];
frame.at<Vec3b>(r, c)[1] = img_perspective.at<Vec3b>(r, c)[1];
frame.at<Vec3b>(r, c)[2] = img_perspective.at<Vec3b>(r, c)[2];
}
}
}
imshow("putPicture", frame);
}
---
運行結果:
No comments:
Post a Comment