Ask Your Question
0

convert Mat image into 2d Matrix double

asked 2016-01-16 14:05:07 -0600

Rami gravatar image

Is there any way ,in which I can convert a Mat object into an double[][] array?I have some image proccesing algoritms that I don't know how to use them on Mat objects, but I can use them if I can find a way of transforming , on double[][]'s ! I'm a novice in Opencv! If the Mat's are converted into double[][]'s , and the Mat's are on 3 channels, do I lose some information, do I have to convert the Mat image in gray? What approach can I use?

edit retag flag offensive close merge delete

Comments

You can access to pixel data using method at or Ptr. you can find some examples here.

I think you should think in the other way transform your algorithm to be compatible with opencv Mat.

About double and 3 channels : a real number is not vector.

It's dfficult to answer to your question without knowing your problem

LBerger gravatar imageLBerger ( 2016-01-16 14:46:04 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2019-09-06 17:42:13 -0600

//code for passing 1 channel image pixel values to a 2D array

//declare ,read,convert image to 32float

    Mat img,dst;
    img=imread("IMG.JPG");
    img.convertTo(dst, CV_32F);
    float arrOut[dst.rows][dst.cols];

//pass data to 2D array

    for(int i = 0 ;i < dst.rows; ++i)
        for(int j = 0; j < dst.cols; ++j)
            arrOut[i][j]=dst.at<float>(i,j);

//show the array

    for(int i = 0 ;i < dst.rows; ++i)
        for(int j = 0; j < dst.cols; ++j)
        {  cout<<arrOut[i][j]<<" ";
           if(j==(dst.cols-1))
                  cout<<endl;
        }
edit flag offensive delete link more
0

answered 2016-01-16 14:51:08 -0600

matman gravatar image

updated 2016-01-19 14:11:24 -0600

When Mat is continous (bool isContinous = src.isContinous()) you can use a simple pointer. If you have a submatrix of a Mat (src(Rect(x, y, w, h)) normaly a row is continous, so you can set a pointer to the begining of a row.

cv::Mat src; // some input image
cv::Mat dst;

// Convert to double (much faster than a simple for loop)
src.convertTo(dst, CV_64F, 1, 0);

 if(dst.isContinous()) {
    double *ptrDst = dst.ptr<double>();   
    for(int i = 0; i < dst.total(); ++i) {
            double value = ptrDst[i]; // Or do some other stuff
    }
} else
    for(int i = 0 i < dst.rows; ++i) {
        double *ptrDst = dst.ptr<double>(i);

        for(int j = 0; j < dst.cols; ++j) {
            double value = ptrDst[j];
        }
    }
}

Or for 2D matrix access:

cv::Mat src; // some input image
cv::Mat dst;

// Convert to double (much faster than a simple for loop)
src.convertTo(dst, CV_64F, 1, 0);

double *ptrDst[dst.rows];
for(int i = 0 i < dst.rows; ++i) {
    ptrDst[i] = dst.ptr<double>(i);

    for(int j = 0; j < dst.cols; ++j) {
        double value = ptrDst[i][j];
    }
}

Edit: You use a 3 channel image, above example is for 1 channel image. So storage order is explained here. The data access is similar to example above with respect to the 3 channel storage.

Edit2: code corrected. Thanks to LBerger.

edit flag offensive delete link more

Comments

Although this is almost a 4-year old question,it is worth to note that for a 2D matrix access,there is something strange: the ptrDst is declared for a 1D array ,and then it is later used as a referenced 2D array: ptrDst[i][j].

Mohamed Shehabeldin gravatar imageMohamed Shehabeldin ( 2019-09-06 16:12:53 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-16 14:05:07 -0600

Seen: 13,286 times

Last updated: Sep 06 '19