0

I am currently working on a Computer Vision / Machine Learning project for university. Sadly, they only allow us to upload one single file and restrict the computation time too much. Hence I need to compute the matrices on my machine and store them in the same file as the code (22500 rows, 1 col and 100 rows + 22500 col and 100 rows + 1 col). I already found a way to export the data (link), but I'm not sure how to initialize the matrix.

What I've tried

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

When I compile it, I get:

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);
                            ^

Second try

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

gives

main.cc:10:5: error: ‘cvInitMatHeader’ is not a member of ‘cv’
     cv::cvInitMatHeader( &matrix1, 10, 1, CV_64FC1, dataHeaderMat1);
     ^
Community
  • 1
  • 1
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • Include the proper headers, or just use: #include . Also, you can have a look [here](http://stackoverflow.com/a/32357875/5008845) to save/load a big matrix – Miki Jan 01 '17 at 17:21

2 Answers2

0

The following works to declare and initialize a matrix:

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

However, I'm not too sure if this is the best way for big arrays.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
0

You can try to save image to header file, like this:

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

But be careful, not all IDEs likes such large files.

Andrey Smorodov
  • 10,649
  • 2
  • 35
  • 42