Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to display an array using OpenCV

📅 2014-Feb-11 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

One dimensional arrays are commonly used to hold 2D data by storing it row-by-row. To check the contents of the array, it is sometimes useful to display it as if it were a 2D image.

This can be done easily in OpenCV by creating a cv::Mat object from the array and displaying that:

#include <opencv2/highgui.hpp>

// Assume arr has 1-channel 2D uchar data
int width;
int height;
unsigned char* arr;

// Create Mat object from array
const cv::Mat img(cv::Size(width, height), CV_8U, arr);

// Display in window and wait for key press
cv::namedWindow("foobar");
cv::imshow("foobar", img);
cv::waitKey(0);

Tried with: Ubuntu 12.04 LTS