Creating OpenCV Mat from user data results in image with circular shifted columns

此生再无相见时 提交于 2019-12-31 03:04:16

问题


I have an image that I load from a file. Is it a .png. I convert this to a 1D array for use in a function via a pointer to the array. When I create a Mat from the 1D pointer, the resulting image looks like it takes the right-most dozen or so columns, and puts them on the left side of the image, almost like a circular shift of columns.

// SAMPLE CODE
Mat img  = imread(argv[1], CV_LOAD_IMAGE_ANYDEPTH);     // 16U1 png
int ncols   = img.cols;
int nrows   = img.rows;

//--Create input array and pointer--
uint16_t rawImage[nrows*ncols];
uint16_t *rawImage_ptr = rawImage;

//Assign value to array
for (int i=0;i<(ncols*nrows);i++){
 *(rawImage_ptr+i) = img.at<uint16_t>(i);
}

// Create Mat from pointer
Mat image(nrows, ncols, CV_16UC1, &rawImage_ptr);

The result 'image' has some of the right columns wrapped around to the left. Any idea what is going on here?


回答1:


Images are stored in opencv with each new row starting at a 32bit boundary.
If the number of cols * pixel size isn't a multiple of 4 then each row if the image will be padded.

You should use cv::mat ptr(row) to get a pointer to the start of each row and then loop along a row.



来源:https://stackoverflow.com/questions/22368312/creating-opencv-mat-from-user-data-results-in-image-with-circular-shifted-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!