Opencv 如何在.cc文件中存储大矩阵?
我目前正在大学从事计算机视觉/机器学习项目。遗憾的是,它们只允许我们上传一个文件,并且限制了太多的计算时间。因此,我需要在我的计算机上计算矩阵,并将它们存储在与代码相同的文件中(22500行、1列和100行+22500列和100行+1列)。我已经找到了导出数据()的方法,但我不确定如何初始化矩阵 我试过的 第二次尝试Opencv 如何在.cc文件中存储大矩阵?,opencv,matrix,Opencv,Matrix,我目前正在大学从事计算机视觉/机器学习项目。遗憾的是,它们只允许我们上传一个文件,并且限制了太多的计算时间。因此,我需要在我的计算机上计算矩阵,并将它们存储在与代码相同的文件中(22500行、1列和100行+22500列和100行+1列)。我已经找到了导出数据()的方法,但我不确定如何初始化矩阵 我试过的 第二次尝试 #include <opencv/cv.h> #include <opencv/highgui.h> int main(int argc, char co
#include <opencv/cv.h>
#include <opencv/highgui.h>
int main(int argc, char const *argv[])
{
float dataHeaderMat1[10] = {1,2,3,4,5,7,8,9,10,11};
cv::Mat matrix1;
// Something is wrong with this line
cv::cvInitMatHeader( &matrix1, 10, 1, CV_64FC1, dataHeaderMat1);
return 0;
}
以下步骤用于声明和初始化矩阵:
#include <opencv/cv.h>
#include <opencv/highgui.h>
int main(int argc, char const *argv[])
{
float data[10] = {1,2,3,4,5,7,8,9,10,11};
cv::Mat A;
// Something is wrong with this line
A = cv::Mat(1, 10, CV_32FC1, data);
return 0;
}
#包括
#包括
int main(int argc,char const*argv[]
{
浮点数据[10]={1,2,3,4,5,7,8,9,10,11};
材料A;
//这条线有毛病
A=cv::Mat(1,10,cv_32FC1,数据);
返回0;
}
但是,我不太确定这是否是大型阵列的最佳方式。您可以尝试将图像保存到头文件,如下所示:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
// uncomment for test
//#include "image.h"
int main(int argc, char **argv)
{
// This part creates header file from image.
Mat img=imread("D:\\ImagesForTest\\lena.jpg");
int w=img.cols;
int h=img.rows;
int channels=img.channels();
ofstream os("image.h");
os << "int rows=" << h << ";" << endl;
os << "int cols=" << w << ";" << endl;
os << "unsigned char d[]={" << endl;
for(int i=0;i<h;++i)
{
for(int j=0;j<w;++j)
{
if(i!=(w-1) || j!=(h-1))
{
Vec3b b=img.at<Vec3b>(i,j);
os << format("0x%02x,",b[0]);
os << format("0x%02x,",b[1]);
os << format("0x%02x,",b[2]);
}
}
}
Vec3b b=img.at<Vec3b>(w-1,h-1);
os << format("0x%02x,",b[0]);
os << format("0x%02x,",b[1]);
os << format("0x%02x",b[2]);
os << endl << "};" << endl;
os << "Mat I=Mat(rows,cols,CV_8UC3,d);" << endl;
os.close();
// To test uncomment commented part of code and comment uncommented.
// uncomment for test
/*
namedWindow("I");
imshow("I",I);
waitKey();
return 0;
*/
}
#包括
#包括
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
//取消对测试的注释
//#包括“image.h”
int main(int argc,字符**argv)
{
//此部分从图像创建头文件。
Mat img=imread(“D:\\ImagesForTest\\lena.jpg”);
int w=img.cols;
int h=img.rows;
int channels=img.channels();
流操作系统(“image.h”);
操作系统包含正确的头文件,或者只使用:#Include。此外,您可以查看保存/加载大矩阵。我无法上载头文件。只有cc文件。正如我在问题标题中所述。您可以将其另存为.cc。
main.cc:10:5: error: ‘cvInitMatHeader’ is not a member of ‘cv’
cv::cvInitMatHeader( &matrix1, 10, 1, CV_64FC1, dataHeaderMat1);
^
#include <opencv/cv.h>
#include <opencv/highgui.h>
int main(int argc, char const *argv[])
{
float data[10] = {1,2,3,4,5,7,8,9,10,11};
cv::Mat A;
// Something is wrong with this line
A = cv::Mat(1, 10, CV_32FC1, data);
return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
// uncomment for test
//#include "image.h"
int main(int argc, char **argv)
{
// This part creates header file from image.
Mat img=imread("D:\\ImagesForTest\\lena.jpg");
int w=img.cols;
int h=img.rows;
int channels=img.channels();
ofstream os("image.h");
os << "int rows=" << h << ";" << endl;
os << "int cols=" << w << ";" << endl;
os << "unsigned char d[]={" << endl;
for(int i=0;i<h;++i)
{
for(int j=0;j<w;++j)
{
if(i!=(w-1) || j!=(h-1))
{
Vec3b b=img.at<Vec3b>(i,j);
os << format("0x%02x,",b[0]);
os << format("0x%02x,",b[1]);
os << format("0x%02x,",b[2]);
}
}
}
Vec3b b=img.at<Vec3b>(w-1,h-1);
os << format("0x%02x,",b[0]);
os << format("0x%02x,",b[1]);
os << format("0x%02x",b[2]);
os << endl << "};" << endl;
os << "Mat I=Mat(rows,cols,CV_8UC3,d);" << endl;
os.close();
// To test uncomment commented part of code and comment uncommented.
// uncomment for test
/*
namedWindow("I");
imshow("I",I);
waitKey();
return 0;
*/
}