0

I have a pointer to an image buffer (byte array) and I know the number of rows and columns. What is the fastest way of displaying this image data in opencv. See details below.

int               rows, cols;
unsigned char     *ImageBuffer;

if (err = WFS_GetSpotfieldImage(instr.handle, &ImageBuffer, &rows, &cols))
    handle_errors(err);

// what goes here to construct "MAT image" using the "ImageBuffer" which is a pointer?
Mat image(rows, cols, CV_8UC3, Scalar(255, 0, 0));

cv::namedWindow("Spot Field", cv::WINDOW_AUTOSIZE);
cv::imshow("Spot Field", image); 
Amir T
  • 1
  • What do you mean with "the fastest way"? if you impose the function; there is nothing to change... – Chris Maes Apr 25 '15 at 21:16
  • I mean how to convert the byte array pointed to by ImageBuffer into the Mat format. Would it be fast enough to use for loops and set the pixels of the Mat image based on the data in the byte array? – Amir T Apr 25 '15 at 21:23
  • Try this answer: http://stackoverflow.com/questions/6924790/opencv-create-mat-from-camera-data – CodyF Apr 25 '15 at 22:09
  • You can just `cout << image;` to print all its data. – herohuyongtao Apr 26 '15 at 02:22

1 Answers1

1

Use this constructor:

Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP)

data – Pointer to the user data. Matrix constructors that take data and step parameters do not allocate matrix data. Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied. This operation is very efficient and can be used to process external data using OpenCV functions. The external data is not automatically deallocated, so you should take care of it.

Konstantin S.
  • 1,307
  • 14
  • 19
  • Thank you Stukov! I found the following method slightly faster than the above constructor you mentioned: Mat image(rows, cols, CV_8UC1, Scalar(0, 0, 0)); for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { Byte temp = ImageBuffer[x + y*cols]; image.at(y, x) = temp; } } – Amir T Apr 25 '15 at 23:41
  • 1
    Your method first allocates new data during the creation of an empty matrix, and then the data to rewrite the data transferred from the buffer. The above method does not allocate all the new data. – Konstantin S. Apr 26 '15 at 03:24