Convert RGB array to Mat (OpenCv)

raphaelluchini picture raphaelluchini · Nov 24, 2013 · Viewed 10.4k times · Source

I've been trying to convert an array [R,G,B,..] in Mat object with opencv. But is returning wrong data, someone knows why?

double data[12] = {0,0,255,0,0,255,0,0,255,0,0,255};
Mat src =  Mat(2,2, CV_16UC3, data);

and returns:

M = 
 [0, 0, 0, 0, 0, 0;
  0, 0, 0, 0, 57344, 16495]

EDIT:

Solved! use uchar instead double, and CV_8UC3

Answer

berak picture berak · Nov 24, 2013

i think, you wanted:

uchar data[12] = {0,0,255,0,0,255,0,0,255,0,0,255};
Mat src =  Mat(2,2, CV_8UC3, data);

(all red, 2x2 rbg image)