[OPENCV GPU] How can I convert GpuMat and Vector<Point2f>
Hello,
I am trying to implement a tracking solution in OpenCV 2.4.10 with GPU support.
So first I am extracting GFTT points :
gpu::GoodFeaturesToTrackDetector_GPU detector(MAX_COUNT, 0.01, 10);
detector(d_frame, d_points);
Then I want to perform some computation on d_points before putting them in the PyrLKOpticalFlow tracker. So there is two solutions: Download in a vector<points2f> or directly work with d_points.
1) Download in a vector<point2f> I can easily download data in a vector of Points2f. For that I use the method:
static void download(const GpuMat& d_mat, vector<Point2f>& vec) {
vec.resize(d_mat.cols);
Mat mat(1, d_mat.cols, CV_32FC2, (void*)&vec[0]);
d_mat.download(mat); }
but I am not able to re-upload the modified data in d_points with the same structure (a GpuMat of 1 row, N columns, and Points2f data type). At best, I can obtain a structure like : GpuMat, 2 rows, N columns, and float data type; which is not readable by the PyrLKOpticalFlow tracker. I tried several things :
//Mat mat(points, false);
//d_points.upload(mat);
//Mat mat = Mat(points, CV_32FC2);
//Mat mat(1, d_points.cols, CV_32FC2, (void*)&points);
//GpuMat d_mat = GpuMat(mat); d_mat.copyTo(d_points);
//Mat mat = Mat(points, CV_32FC2).reshape(1,points.size());
//d_points.create(1, points.size(),CV_32FC2);
//d_points.upload(Mat(points, false));
//d_mat.copyTo(d_points);
2) Directly modify the GpuMat d_points I found several way to access d_points element, but every time I try to cout them I have a segmentation fault.
I know that d_points.cols = N and d_points.rows = 1. I tried this way :
Point2f* row_y = (Point2f*)((char*)d_points.data + n * step);
Point2f val = row_y[0];
But if I cout I get a segmentation fault.
And I also tried :
cv::Point2f* pixel_ptr = d_points.ptr<cv::Point2f >(n);
But when I want to access pixel_ptr, I also get a segmentation fault.
Is there a way to modify d_points (GpuMat with a special structure) or to access the data, modify it and re-upload it ?
Thank you for help !
Any luck with this issue? I am also trying to using PyrLKOpticalFlow and I can not pass the gpuMat that i get from FAST_GPU to sparse call for PyrLKOpticalFlow.
I'm trying to do the exactly same thing as you, @kjoshi. Have you solved it?