简体   繁体   English

OpenCV:如何为每个像素填充具有每个RGB颜色值的rgb图像?

[英]OpenCV: how to fill rgb image having values for each of RGB colors for each pixel?

因此,具有RGBRGBRGB ... w * 3大小的缓冲区如何用此类数据填充OpenCV图像?

The easiest way is just to loop over the elements of the buffer using the at templated method . 最简单的方法就是使用at模板化方法遍历缓冲区的元素。

unsigned char buffer[] = {1, 2, 3, ..., 18}; // RGBRGB...
cv::Mat image(2, 3);
for (int i = 0; i < 18; ++i) {
  int row = i/9;
  int col = (i/3)%3;
  int rgb = i%3; // An index 
  image.at<unsigned char>(row,col+rgb) = buffer[i];
}

Of course, you need to initialize your matrix with the correct type, and set the color format, which I didn't do above. 当然,您需要使用正确的类型初始化矩阵,并设置颜色格式,而我上面没有做过。 See more about the OpenCV matrix object here . 在此处查看有关OpenCV矩阵对象的更多信息。

IplImage has a variable imageData. IplImage具有可变的imageData。 It is just a buffer. 它只是一个缓冲区。 So you can simply copy your array if it have the same format as imageData buffer. 因此,如果数组具有与imageData缓冲区相同的格式,则可以简单地复制它。 If format differs then you can copy, but you will need to fill other variables of your IplImage properly. 如果格式不同,则可以复制,但是您需要正确填充IplImage的其他变量。

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

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