Reputation: 111
I'm writing a program use both OpenCV and Kinect v2 SDK, where I met an awkward problem. I obtained vectors of Point2i(class consist of int X,Y) as indices of pixels, and I need to convert it into depthSpacePoint(a struct consisting of float32 X,Y), which is simply depthSpacePoints.X = Point2i.X; or the other way around.
There are so many elements, writing a for loop would be too slow.
So I wonder if there any method like memcpy or other to do it faster?
Here is a brief of my code:
cv::findnonZeros(Mat A, vector<cv::Point2i> B);
vector<depthSpacePoint> C(B.size());
for(int i= 0; i < B.size(); ++i){
C[i].X = b[i].x;
C[i].Y = B[i].y;
}
icoordinateMapper(C.size(),C,...,...);
<- this is the reason why I convert it, because this library function only takes its own data type depthSpacePoint;
Upvotes: 2
Views: 156
Reputation: 41775
You can implement your own version of cv::findNonZero
that directly creates a std::vector<depthSpacePoint>
.
It's just a matter of scanning (efficiently) the matrix and checking a condition.
void findNonZeroSpacePoints( InputArray _src, std::vector<depthSpacePoint>& pts )
{
// Get the src matrix, be sure it's of correct type
Mat src = _src.getMat();
CV_Assert( src.type() == CV_8UC1 );
// If src is all zero, return
int n = countNonZero(src);
if(n == 0) {
pts.clear();
return;
}
// Allocate pts
pts.resize(n);
// Efficiently scan matrix and append points
depthSpacePoint* ppts = pts.data();
for( int i = 0; i < src.rows; i++ )
{
const uchar* bin_ptr = src.ptr(i);
for( int j = 0; j < src.cols; j++ )
if( bin_ptr[j] )
*ppts ++ = depthSpacePoint(float(j), float(i));
}
}
Upvotes: 1