Mat convert to int array to byte array
I need to convert a Mat to int array and byte array in C ++. How can I do this?
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];
}
}
Asked: 2016-05-30 12:22:27 -0600
Seen: 3,076 times
Last updated: May 30 '16