Ask Your Question
0

unsigned short 1D array of gray image

asked 2019-02-21 12:27:05 -0600

ymw gravatar image

updated 2019-02-22 03:51:09 -0600

hi, I know this sound very small problem, But I have been searching google answer for this, basically what I need is convert 2d array of gray image into 1d unsigned short array. I tried following code, but I could not get what I need.

unsigned short pixellist[48400];//image size 220x220

img= imread("lena.png");

cvtColor(img,gimg,COLOR_RGB2GRAY);

unsigned char* dataMat = gimg.data;

how can I get dataMat into pixellist ? without for loops??? or are there any other method to convert Mat to unsigned short array???

edit retag flag offensive close merge delete

Comments

LBerger gravatar imageLBerger ( 2019-02-21 12:32:08 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2019-02-22 01:15:31 -0600

berak gravatar image

updated 2019-02-22 01:16:32 -0600

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"); // 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]
edit flag offensive delete link more

Comments

hi berak how can I use this method without upscaling?

ymw gravatar imageymw ( 2019-02-22 10:17:41 -0600 )edit

just make it gray.convertTo(gimg, CV_16UC1); to keep it in the original [0..255] range

berak gravatar imageberak ( 2019-02-22 10:20:21 -0600 )edit

thank you for the reply, now it works as I expected, can you please tell me how to do the opposite way? unsiged short array to Mat?

ymw gravatar imageymw ( 2019-02-22 14:23:19 -0600 )edit
0

answered 2019-02-21 14:20:54 -0600

Maybe use stl::vector instead of array since different images will be different sizes. Also, data in grayscale images are normally unsigned char (uchar). Something lke:

Mat img = imread("C:/OpenCV4_0_1/sources/samples/data/lena.jpg");
Mat gimg;

cvtColor(img, gimg, COLOR_RGB2GRAY);

vector<unsigned short> pixelList(gimg.size().area());
pixelList.assign(gimg.begin<uchar>(), gimg.end<uchar>());
edit flag offensive delete link more

Comments

hi Chris, Thanks for the reply, your answer works as I needed.But I cannot use vector unsigned short for my situation. the image I use here is size of 220x220. is it impossible to use following function for this case? CV::Mat gimg(220, 220, CV_16UC1, i);

ymw gravatar imageymw ( 2019-02-21 22:23:52 -0600 )edit

You can change from vector to array with something like this. Notice the lena image in the opencv samples is 512 x 512 so adjust the array size for your image.

Mat img = imread("C:/OpenCV4_0_1/sources/samples/data/lena.jpg");
Mat gimg;

cvtColor(img, gimg, COLOR_RGB2GRAY);

unsigned short pixelArray[512 * 512];
std::copy(gimg.begin<uchar>(), gimg.end<uchar>(), std::begin(pixelArray));
Chris gravatar imageChris ( 2019-02-22 05:53:40 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-02-21 12:27:05 -0600

Seen: 1,430 times

Last updated: Feb 22 '19