Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

since grayscale images in opencv usually are CV_8U, you need to do this in 2 seperate steps:

// #1 load and convert to gray:
Mat img = imread("C:/OpenCV4_0_1/sources/samples/data/lena.jpg");
Mat gray;

cvtColor(img, gray, COLOR_BGR2GRAY); // BGR, not RGB !

// #2 convert to 16U:
unsigned short i[48400];
Mat gimg(220, 220, CV_16UC1, i); 
// but be careful, your gimg is only valid, as long as the i array stays in scope !

gray.convertTo(gimg, CV_16UC1, 255); // also upscale to [0..0xffff]

since grayscale images in opencv usually are CV_8U, you need to do this in 2 seperate steps:

// #1 load and convert to gray:
Mat img = imread("C:/OpenCV4_0_1/sources/samples/data/lena.jpg");
imread("C:/OpenCV4_0_1/sources/samples/data/lena.jpg"); // CV_8UC3
Mat gray;
 cvtColor(img, gray, COLOR_BGR2GRAY); // BGR, not RGB !

// #2 convert to 16U:
unsigned short i[48400];
Mat gimg(220, 220, CV_16UC1, i); 
// but be careful, your gimg is only valid, as long as the i array stays in scope !

gray.convertTo(gimg, CV_16UC1, 255); // also upscale to [0..0xffff]