How to convert cv::Mat to std::string and hash it using MD5 ?
I need to find the repeated images (exact same, this is not a image similarity problem) by hashing. So I tried something like this, but this seems not right.
Mat src = imread (inputfile, -1) ;
char* rawPtr = new char[src.step[0] * src.rows] ;
memcpy(rawPtr, (char*)src.data, src.step[0] * src.rows) ; // is this all the data ??
string rawdata(rawPtr) ;
cout << str2md5(rawPtr, strlen(rawPtr)) << endl ;
My file is CV_16UC3, 1624 by 1236 , 3 channels, src.step[0] * src.rows is the bytes number of the whole image 12043584 bytes, however strlen(rawPtr) returns 5739852 chars, and these don't match.
So the md5 value I got is not right. I would like to know how to efficiently and correctly convert cv::Mat data into a string.
rrrr, you can't use strlen (or , any string related function) on binary data ! ( as it will stop at the 1st 0, which is the string terminator, but a total valid black pixel )
use the same length calculation (src.step[0] * src.rows) you had before instead.
making a string of it won't work as expected, too, for the same reason.
if your str2md5 function expects a const char * as input, it might work, if it wants a std::string, it won't.
@berak, thanks so much, I didn't realize it. But I am still not sure if I'm doing it right to hash the cv::Mat. I got another example here . FileStorage fs (".xml", FileStorage::WRITE + FileStorage::MEMORY) ; fs <<"data"<< src ; string cvinternal = fs.releaseAndGetString() ; //cout << cvinternal.length() << endl ; cout << str2md5(cvinternal.c_str(), cvinternal.length()) << endl ;
How do you think use this to hash ?