【问题标题】:How can I store a big matrix within the .cc file?如何在 .cc 文件中存储一个大矩阵?
【发布时间】:2017-01-01 16:25:10
【问题描述】:

我目前正在为大学从事计算机视觉/机器学习项目。遗憾的是,它们只允许我们上传一个文件并且过多地限制了计算时间。因此,我需要在我的机器上计算矩阵并将它们存储在与代码相同的文件中(22500 行,1 列和 100 行 + 22500 列和 100 行 + 1 列)。我已经找到了导出数据的方法(link),但我不确定如何初始化矩阵。

我尝试过的

#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::CV_32FC1, data);
    return 0;
}

当我编译它时,我得到:

main.cc: In function ‘int main(int, const char**)’:
main.cc:10:16: error: expected primary-expression before ‘(’ token
     A = cv::Mat(1, 10, cv::CV_32FC1, data);
                ^
In file included from /usr/include/opencv2/core/core_c.h:47:0,
                 from /usr/include/opencv/cv.h:63,
                 from main.cc:1:
main.cc:10:28: error: expected unqualified-id before ‘(’ token
     A = cv::Mat(1, 10, cv::CV_32FC1, data);
                            ^

第二次尝试

#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;
}

给予

main.cc:10:5: error: ‘cvInitMatHeader’ is not a member of ‘cv’
     cv::cvInitMatHeader( &matrix1, 10, 1, CV_64FC1, dataHeaderMat1);
     ^

【问题讨论】:

  • 包括正确的标题,或者只使用:#include 。另外,你可以看看here 来保存/加载一个大矩阵

标签: opencv matrix


【解决方案1】:

声明和初始化矩阵的工作如下:

#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;
}

但是,我不太确定这是否是大型数组的最佳方式。

【讨论】:

    【解决方案2】:

    您可以尝试将图像保存到头文件,如下所示:

    #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;
        */
    }
    

    但是要小心,并不是所有的 IDE 都喜欢这么大的文件。

    【讨论】:

    • 我无法上传头文件。只有cc文件。正如我在问题标题中所写。
    • 可以另存为.cc。
    猜你喜欢
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 2020-04-18
    • 2019-11-04
    • 1970-01-01
    相关资源
    最近更新 更多