Ask Your Question
0

Mat convert to int array to byte array

asked 2016-05-30 12:22:27 -0600

VCS_DEV gravatar image

I need to convert a Mat to int array and byte array in C ++. How can I do this?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-05-30 14:36:52 -0600

matman gravatar image

updated 2016-05-30 15:08:44 -0600

It is useful to deal with OpenCV's documentation before asking such basic questions. What you need is cv::Mat::convertTo with CV_32S and CV_8U as rtype and if you want to convert 2D matrix to 1D array use cv::Mat::reshape. You can handle Mat as C array with cv::Mat::ptr

// example for 1 channel Mat
cv::Mat img, imgconv;
img.convertTo(imgconv, CV_32S);

if(imgconv.isContinuous() {
    imgconv = imgconv.reshape(1, 1);
}

for(int i = 0; i < imgconv.rows; ++i) {
    int *ptrarray = imgconv.ptr<int>(i);
    for(int ii = 0; ii < imgconv.cols; ++ii) {
         int x = ptrarray[ii];
    }
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-05-30 12:22:27 -0600

Seen: 3,247 times

Last updated: May 30 '16