Rella
Rella

Reputation: 66935

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

So having buffer with RGBRGBRGB... size of w*3 how to fill OpenCV Image with such data?

Upvotes: 0

Views: 2524

Answers (2)

Michael Repucci
Michael Repucci

Reputation: 1663

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.

Upvotes: 4

Andrew
Andrew

Reputation: 24846

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.

Upvotes: 2

Related Questions