C++ &引用;。exe已触发一个断点";将cv::Mat转换为TensorFlow张量时
我试图将C++ &引用;。exe已触发一个断点";将cv::Mat转换为TensorFlow张量时,c++,opencv,tensorflow,C++,Opencv,Tensorflow,我试图将cv::Mat转换为Tensor,因此我构建了一个函数,它显示了问题:“.exe触发了断点”,在调试时,我发现析构函数~my_buffer()和基类~TensorBuffer()可能不正确。它显示了一个名为ref\uu.load()=2的函数。下面是my_buffer类。 命令行输出: 检查失败:参考加载()==0(2对0) class my_buffer:public TensorBuffer{ 公众: 浮动*数据; 尺寸、长度; ~my_buffer()覆盖{ //无事可做 } v
cv::Mat
转换为Tensor
,因此我构建了一个函数,它显示了问题:“.exe触发了断点”,在调试时,我发现析构函数~my_buffer()
和基类~TensorBuffer()
可能不正确。它显示了一个名为ref\uu.load()=2
的函数。下面是my_buffer
类。
命令行输出:
检查失败:参考加载()==0(2对0)
class my_buffer:public TensorBuffer{
公众:
浮动*数据;
尺寸、长度;
~my_buffer()覆盖{
//无事可做
}
void*data()常量重写{返回数据}
size\u t size()常量重写{return len\u;}
bool OwnsMemory()常量重写{return false;}
TensorBuffer*根缓冲区()重写{返回此;}
void FillAllocationDescription(AllocationDescription*proto)常量重写{
tensorflow::int64 rb=size();
proto->设置请求字节(rb);
proto->set_分配器_name(tensorflow::cpu_分配器()->name());
}
以防万一,代码的其余部分:
Tensor cv2tensor(const cv::Mat & img )
{
int height = 1069;
int width = 500;
int chanel = 3;
int bacth = 1;
int input_size = height*width*chanel;
std::vector<float> input_buffer(input_size);
/*float * input_data = input_buffer.data();*/
const int64_t dim[4] = { bacth, width, height, chanel };
my_buffer tensor_buf;
//////////////////////////////////////////////////////
/*img.reshape(1);*/
int count = 0;
/*for (int i=0;i<)*/
for (int i = 0; i < 3; i++)
{
for (int row = 0; row < img.rows; row++)
{
for (int col = 0; col < img.cols; col++)
{
input_buffer[count] = img.at<cv::Vec3b>(row, col)[0];
count += 1;
}
}
}
////////////////////////////////////////////////////////
tensor_buf.data_ = (float *)input_buffer.data();
tensor_buf.len_ = input_size;
std::vector<tensorflow::int64> tensor_dim;
for (int i = 0; i < 4; i++)
tensor_dim.push_back(dim[i]);
Tensor input_tensor = tensorflow::TensorCApi::create_tensor(DT_FLOAT, TensorShape(tensor_dim), &tensor_buf);
//cv::resize()
/*Tensor input_tensor;*/
return input_tensor;
}
张量CV2传感器(常数cv::Mat和img)
{
整数高度=1069;
整数宽度=500;
int香奈儿=3;
int-bacth=1;
int input_size=高度*宽度*通道;
std::矢量输入_缓冲区(输入_大小);
/*float*input_data=input_buffer.data()*/
const int64_t dim[4]={bacth,width,height,chanel};
我的缓冲张量;
//////////////////////////////////////////////////////
/*重塑(1)*/
整数计数=0;
/*对于(int i=0;i而言,将OpenCV矩阵转换为张量实际上更直接(from):
// just for this answer:
using namespace tensorflow;
cv::Mat image;
cv::Mat image_float;
image.convertTo(image_float, CV_32FC3);
float *image_float_data = (float*)image_float.data;
TensorShape my_shape = TensorShape{1, image.rows, image.cols, image.channels()};
Tensor my_tensor = Tensor(DT_FLOAT, my_shape);
std::copy_n((char*) image_float_data,
my_shape.num_elements() * sizeof(float),
const_cast<char*>(my_tensor.tensor_data().data()));
//就为了这个答案:
使用名称空间tensorflow;
cv::Mat图像;
cv::Mat image_float;
convertTo(image_float,CV_32FC3);
float*image\u float\u data=(float*)image\u float.data;
TensorShape my_shape=TensorShape{1,image.rows,image.cols,image.channels()};
张量my_张量=张量(DT_FLOAT,my_shape);
标准::复制((字符*)图像浮点数数据,
my_shape.num_elements()*sizeof(float),
const_cast(my_tensor.tensor_data().data());
为什么要迭代数据?
为什么要创建自己的缓冲区类