简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM