Search code examples
opencvmultidimensional-arraymat

OpenCV How to initialize Mat with 2D array in JAVA


Suppose, I have a 2D array initialized with values, how do I put this value in a Mat object in OpenCV?


Solution

  • Sorry don't know about Java but can suggest the general logic. In C++ openCV we do it by 2 for loops as following:

    matObject.create( array.rows, array.cols, CV_8UC1 ); // 8-bit single channel image
    
    for (int i=0; i<array.rows; i++)
    {
        for(int j=0; j<array.cols; j++)
        {
             matObject.at<uchar>(i,j) = array[i][j];
        }
    }
    

    Let me know if it was your query..