简体   繁体   中英

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 .

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 .

IplImage has a variable imageData. It is just a buffer. So you can simply copy your array if it have the same format as imageData buffer. If format differs then you can copy, but you will need to fill other variables of your IplImage properly.

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