在我的应用程序中,我想创建一个具有一些值的OpenCV Mat A(2-Dimensions),然后使用A作为输入将其传递给另一个OpenCV函数.
目前,我正在尝试:
// float data[2][5] = {{1,2,3,4,5},{7,8,9,10,11}};
// OR
float data[10] = {1,2,3,4,5,7,8,9,10,11};
// and then
// A = Mat(1, 5, CV_32FC1, &data, 2); // init from float 1D - array
// OR
A = Mat(2, 5, CV_32FC1, &data, 2);
Run Code Online (Sandbox Code Playgroud)
在1D数组的情况下,传递的值是OK.但这对2D阵列不起作用,这更常见.我怎样才能在OpenCV中解决这个问题?
我正在创建一个多维MAT对象,并希望得到对象的大小 - 例如,
const int sz[] = {10,10,9};
Mat temp(3,sz,CV_64F);
std::cout << "temp.dims = " << temp.dims << " temp.size = " << temp.size() << " temp.channels = " << temp.channels() << std::endl;
Run Code Online (Sandbox Code Playgroud)
我相信得到的MAT是10x10x9,我想确认一下,但是COUT声明给出了:
temp.dims = 3 temp.size = [10 x 10] temp.channels = 1
我希望看到:
temp.dims = 3 temp.size = [10 x 10 x 9] temp.channels = 1
要么:
temp.dims = 3 temp.size = [10 x 10] temp.channels = 9
如何获得此Mat对象的维度?我在Mat :: Mat或MatND中没有看到任何方法
我正在使用openCV,我有一个类型为CV_32F的95,1 mat对象,我想写一个二进制文件.我正在使用下面的代码但是我不能将32F转换为char类型.有什么建议吗?我还想执行读取二进制文件并将值存储到相同类型的mat对象的相反过程.
try{
ofstream posBinary;
posBinary.open("C:/Users/Dr.Mollica/Documents/TSR Datasets/signDatabasePublicFramesOnly/posSamps.bin", ios::out | ios::binary);
posBinary.write((char *)featureVector, sizeof(featureVector);
}
catch (exception X){ cout << "Error! Could not write Binary file" << endl; }
Run Code Online (Sandbox Code Playgroud)
另外需要注意的是,我想在二进制文件中执行此操作的原因是我将向文件中写入大量这些向量,这些向量将被读入机器学习算法.从我的理解,读取和写入二进制文件是最快的方法.
我想在Opencv中将Mat转换为vector并将Vector转换为mat.
我的代码:
void mat_to_vector(Mat in,vector<float> &out){
for (int i=0; i < in.rows; i++) {
for (int j =0; j < in.cols; j++){
//unsigned char temp;
//file << Dst.at<float>(i,j) << endl;
out.push_back(in.at<float>(i,j));
}
}
}
void vector_to_mat(vector<float> in, Mat out,int cols , int rows){
for (int i=rows-1; i >=0; i--) {
for (int j =cols -1; j >=0; j--){
out.at<float>(i,j) = in.back();
in.pop_back();
//file << Dst.at<float>(i,j) << endl;
// dst_temp.push_back(Dst.at<float>(i,j));
}
}
}
Run Code Online (Sandbox Code Playgroud)
以上代码很慢.有更快的解决方案吗?