简体   繁体   English

根据用户数据创建OpenCV Mat会导致图像出现列移位

[英]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. 是.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. 当我从1D指针创建Mat时,生成的图像看起来像是占据了最右端的大约12列,然后将它们放在图像的左侧,几乎就像是列的圆形移位。

// 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. 图像存储在opencv中,每个新行都从32位边界开始。
If the number of cols * pixel size isn't a multiple of 4 then each row if the image will be padded. 如果cols *像素大小的数量不是4的倍数,则将填充图像的每一行。

You should use cv::mat ptr(row) to get a pointer to the start of each row and then loop along a row. 您应该使用cv::mat ptr(row)获取指向每一行开头的指针,然后沿着一行循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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