在 Javacv 中传递 Point2f [] 作为 getAffineTransform() 的参数
Passing Point2f [] as arguments for getAffineTransform() in Javacv
我正在尝试使用 javacv 在 java 中找到 here 的面部对齐实现 C++ 位,但我不知道如何通过
point2f[] 作为 getAffineTransform() 中的 point2f。
这是我想出的办法;
//face alignment begin
Point2f[] srcTri = new Point2f[3];
Point2f[] dstTri = new Point2f[3];
Mat src_mat= new Mat(orig);
Mat dst_mat= new Mat(input);
Mat warp_mat = new Mat( 2, 3, CV_32FC1);
//Set the dst image the same type and size as src
Mat warp_dst = new Mat(src_mat.rows(), src_mat.cols(), src_mat.type());
//Set your 3 points to calculate the Affine Transform
srcTri[0] = new Point2f((float)landmarks[5*2], (float)landmarks[5*2+1]);
srcTri[1] = new Point2f((float)landmarks[6*2], (float)landmarks[6*2+1]);
srcTri[2] = new Point2f((float)landmarks[0*2], (float)landmarks[0*2+1]);
dstTri[0] = new Point2f((float)landmarks[5*2], (float)landmarks[5*2+1]);
dstTri[1] = new Point2f((float)landmarks[6*2], (float)landmarks[6*2+1]);
dstTri[2] = new Point2f((float)landmarks[0*2], (float)landmarks[0*2+1]);
//Get the Affine Transform
warp_mat = getAffineTransform( srcTri, dstTri ); //This is where I have problems. getAffineTransform() expects Point2f but I have Point2f[]
//Apply the Affine Transform just found to the src image
warpAffine(src_mat, warp_dst, warp_mat, warp_dst.size());
//show images
imshow("src", src_mat);
imshow("dst", dst_mat);
imshow("warp_dst", warp_dst);
waitKey(0);
//face alignment end
有人请教我如何将 srcTri 和 dstTri 作为 Point2f 传递给 getAffineTransform() 或如何将 Point2f[] 转换为 Point2f。甚至有可能这样做吗?
如何将地标放入可以接受为 getAfflineTransform() 中的参数的 Point2f 中?
非常感谢您在此处导航方面提供的帮助。
我相信这就是答案:https://groups.google.com/d/msg/javacv/dbWTNCHFyeg/Q6k59GPavgsJ:
Point2f
is a Pointer
, which works like a native array:
http://bytedeco.org/javacpp-presets/opencv/apidocs/org/bytedeco/javacpp/opencv_core.Point2f.html
And we can do things like that with a Pointer
to access the elements
of a native array:
http://bytedeco.org/javacpp/apidocs/org/bytedeco/javacpp/Pointer.html#position-int-
Check the README file for an example with CvPoint
:
https://github.com/bytedeco/javacv#sample-usage Now do the same thing,
but with Point2f
.
所以你的代码应该这样修改:
Point2f srcTri = new Point2f(3);
Point2f dstTri = new Point2f(3);
// ...
srcTri.position(0).x((float)landmarks[5*2]).y((float)landmarks[5*2+1]);
srcTri.position(1).x((float)landmarks[6*2]).y((float)landmarks[6*2+1]);
srcTri.position(2).x((float)landmarks[0*2]).y((float)landmarks[0*2+1]);
// and do the same for dstTri
warp_mat = getAffineTransform( srcTri.position(0), dstTri.position(0) );
我正在尝试使用 javacv 在 java 中找到 here 的面部对齐实现 C++ 位,但我不知道如何通过
point2f[] 作为 getAffineTransform() 中的 point2f。
这是我想出的办法;
//face alignment begin
Point2f[] srcTri = new Point2f[3];
Point2f[] dstTri = new Point2f[3];
Mat src_mat= new Mat(orig);
Mat dst_mat= new Mat(input);
Mat warp_mat = new Mat( 2, 3, CV_32FC1);
//Set the dst image the same type and size as src
Mat warp_dst = new Mat(src_mat.rows(), src_mat.cols(), src_mat.type());
//Set your 3 points to calculate the Affine Transform
srcTri[0] = new Point2f((float)landmarks[5*2], (float)landmarks[5*2+1]);
srcTri[1] = new Point2f((float)landmarks[6*2], (float)landmarks[6*2+1]);
srcTri[2] = new Point2f((float)landmarks[0*2], (float)landmarks[0*2+1]);
dstTri[0] = new Point2f((float)landmarks[5*2], (float)landmarks[5*2+1]);
dstTri[1] = new Point2f((float)landmarks[6*2], (float)landmarks[6*2+1]);
dstTri[2] = new Point2f((float)landmarks[0*2], (float)landmarks[0*2+1]);
//Get the Affine Transform
warp_mat = getAffineTransform( srcTri, dstTri ); //This is where I have problems. getAffineTransform() expects Point2f but I have Point2f[]
//Apply the Affine Transform just found to the src image
warpAffine(src_mat, warp_dst, warp_mat, warp_dst.size());
//show images
imshow("src", src_mat);
imshow("dst", dst_mat);
imshow("warp_dst", warp_dst);
waitKey(0);
//face alignment end
有人请教我如何将 srcTri 和 dstTri 作为 Point2f 传递给 getAffineTransform() 或如何将 Point2f[] 转换为 Point2f。甚至有可能这样做吗? 如何将地标放入可以接受为 getAfflineTransform() 中的参数的 Point2f 中?
非常感谢您在此处导航方面提供的帮助。
我相信这就是答案:https://groups.google.com/d/msg/javacv/dbWTNCHFyeg/Q6k59GPavgsJ:
Point2f
is aPointer
, which works like a native array: http://bytedeco.org/javacpp-presets/opencv/apidocs/org/bytedeco/javacpp/opencv_core.Point2f.htmlAnd we can do things like that with a
Pointer
to access the elements of a native array: http://bytedeco.org/javacpp/apidocs/org/bytedeco/javacpp/Pointer.html#position-int-Check the README file for an example with
CvPoint
: https://github.com/bytedeco/javacv#sample-usage Now do the same thing, but withPoint2f
.
所以你的代码应该这样修改:
Point2f srcTri = new Point2f(3);
Point2f dstTri = new Point2f(3);
// ...
srcTri.position(0).x((float)landmarks[5*2]).y((float)landmarks[5*2+1]);
srcTri.position(1).x((float)landmarks[6*2]).y((float)landmarks[6*2+1]);
srcTri.position(2).x((float)landmarks[0*2]).y((float)landmarks[0*2+1]);
// and do the same for dstTri
warp_mat = getAffineTransform( srcTri.position(0), dstTri.position(0) );