Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to display an array using OpenCV

📅 2014-Feb-11 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ opencv ⬩ 📚 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::imshow("foobar", img);

Tried with: Ubuntu 12.04 LTS

© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘📧