2

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 Answers1

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.

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • Interesting. Thanks for that. However, my image is a 512(rows)x 640(cols) with element_size()=2. So, it should be on a 32-bit boundary I believe. Am I missing something? – user3413491 Mar 14 '14 at 13:18