两个垫子的 OpenCV 数据相同,但使用 Mat::at 检索时的值已损坏 [英] OpenCV data of two mats are the same but values when retrieved with Mat::at are corrupted
问题描述
我使用自定义函数序列化然后反序列化 Mat.当您比较两个函数的 uchar* Mat.data
时,它们是相同的,但是当我尝试在第三个元素 element 之后直接使用 Mat.at<>
检查值时值停留在一些损坏的浮点值上.我尝试将元素更改为其他所有值,但没有任何改变.当我序列化-反序列化图像垫和直方图垫时,问题也不会发生.下面是代码,下面是输出.
I serialize and then deserialize a Mat using custom functions. When you compare the uchar* Mat.data
of two functions they are the same but when I try to check values directly using Mat.at<>
after third element, element values are stuck at some corrupted float values. I tried changing elements to every other value, nothing changed. The problem also doesn't happen when I serialize-deserialize image Mats and histogram Mats. Below is the code and below that is the output.
cv::Mat kernelx = (cv::Mat_<float>(3, 3) << 1, 0, -1, 1, 0, -1, 1, 0, -1);
std::string stroper = dbop::serializeMat(kernelx);
cv::Mat matoper = dbop::deserializeMat(stroper);
for (int i = 0; i < matoper.rows; i++) {
for (int j = 0; j < matoper.cols; j++) {
float f1 = (float)kernelx.data[i + j];
float f2 = (float)matoper.data[i + j];
std::cout << kernelx.at<float>(i, j) << " " << matoper.at<float>(i, j) << " " << f1 << " " << f2 << " " << std::endl;
}
}
这是输出.第一个值是原始 Mat 的元素,第二个值是序列化-反序列化 Mat 的元素,第三个值是原始 Mat 转换为浮点的数据值,第四个值是 s-d Mat 的数据值.
Here is the output. First values are elements of the original Mat, second values are elements of serialized-deserialized Mat, third values are the original Mat's data values casted to float, fourth values are s-d Mat's data values.
1 1 0 0
0 0 0 0
-1 -4.31596e+08 128 128
1 -4.31602e+08 0 0
0 -4.31602e+08 128 128
-1 -4.31602e+08 63 63
1 -4.31602e+08 128 128
0 -4.31602e+08 63 63
-1 -4.31602e+08 0 0
我猜 serializeMat 和 deserializeMat 函数在这里是相关的,所以这里都是.
I guess serializeMat and deserializeMat functions are relevant here so here is both.
std::string dbop::serializeMat(cv::Mat operand) {
std::ostringstream srlzstrstream;
uchar* pixelPtr = (uchar*)operand.data;
int cn = operand.channels();
cv::Scalar_<uchar> cnPixel;
srlzstrstream << operand.dims << " ";
for (int i = 0; i < operand.dims; i++) {
srlzstrstream << operand.size[i] << " ";
}
srlzstrstream << operand.type() << " ";
for (int i = 0; i < operand.total(); i++) {
for (int k = 0; k < cn; k++) {
srlzstrstream << (float)pixelPtr[i * cn + k] << " ";
}
}
std::string srlzdstr = srlzstrstream.str();
return srlzdstr;
}
cv::Mat dbop::deserializeMat(std::string operand){
std::istringstream desrlzstrstream(operand);
int mdims, mtype;
desrlzstrstream >> mdims;
int* msize = new int[mdims];
for (int i = 0; i < mdims; i++)
desrlzstrstream >> msize[i];
desrlzstrstream >> mtype;
cv::Mat matoper(mdims, msize, mtype);
delete [] msize;
uchar* pixelPtr = (uchar*)matoper.data;
float cnpixoper;
for (int i = 0; i < matoper.total(); i++) {
for (int k = 0; k < matoper.channels(); k++) {
desrlzstrstream >> cnpixoper;
pixelPtr[i * matoper.channels() + k] = cnpixoper;
}
}
return matoper;
}
推荐答案
+ 编辑
由于您想为泛型类型保存字节,因此您可以像这样迭代每个字节.
+ Edit
Since you want to save byte for generic types, you can just iterate each byte like this.
在serializeMat
for (int i = 0; i < operand.total() * operand.elemSize(); i++) {
srlzstrstream << pixelPtr[i] << " ";
}
在deserializeMat
for (int i = 0; i < matoper.total() * matoper.elemSize(); i++) {
desrlzstrstream >> cnpixoper;
pixelPtr[i] = cnpixoper;
}
operand.total() * operation.elemSize()
会给你 operand
在这两种情况下,输出都是
In both case, the output is
1 1 0 0
0 0 0 0
-1 -1 128 128
1 1 0 0
0 0 128 128
-1 -1 63 63
1 1 128 128
0 0 63 63
-1 -1 0 0
这篇关于两个垫子的 OpenCV 数据相同,但使用 Mat::at 检索时的值已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!