marcelosalloum
marcelosalloum

Reputation: 3531

Anyone knows how to convert a cv::Mat into a NSString?

I am using OpenCV in a iPhone project. In this application, I receive a 3x3 matrix from the Opencv function an need to transform it into a NSString. For example:

cv::Mat myMatrix = opencvClass.getMatrix();
NSString *matrixString = myMatrix.toNSString(); // this function does not exist actually but it is what I need

Any suggestions?

Cheers,

Upvotes: 0

Views: 642

Answers (1)

Guillaume Algis
Guillaume Algis

Reputation: 11016

I guess you could convert it to a std::stringstream, extract the C array of bytes from it and use this to contruct your NSString :

NSString NSStringFromCvMat(cv::Mat mat)
{
    std::stringstream ss;
    ss << mat;
    return [NSString stringWithCString:ss.str().c_str() encoding:NSASCIIStringEncoding];
}

Untested but should work. The encoding could be a problem. Maybe try different values.

I don't think it is the best solution though, and you could find a beter and cleaner one by diving into OpenCV source code.

Upvotes: 3

Related Questions