unsigned char ** to opencv mat
Asked Answered
F

2

8

I'm almost there but I can't quite understand how to convert

unsigned char ** to a cv::Mat

I know that the .data part of a cv::Mat is uchar*

I'm using a function that returns and image in the form of...

unsigned char ** output;

But the rest of my code uses cv::Mat's. I don't have the source for the lib I'm using either so don't really know what it's doing.

Edit Thanks for the help guys, I've done this...

cv::Mat TempMat = cv::Mat(h, w, CV_8UC1, *output);
imshow("this is a test",TempMat);

but the image is black so I now need to find out if there's actually anything there or not.

Sorry for lack of research i'm on a tight deadline, no it's not homework, trying to get something ready to show results to a Professor!

Fridafriday answered 6/11, 2012 at 16:40 Comment(1)
Do you know the width and height of the double pointer you are returning?Animism
M
11

You have to use Mat constructor with a pointer to data:

 // constructor for matrix headers pointing to user-allocated data
    Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP);
    Mat(Size _size, int _type, void* _data, size_t _step=AUTO_STEP);

You have to convert void** to void* and after this use it.

Meredeth answered 6/11, 2012 at 17:3 Comment(4)
Thanks, I've had a go at doing that. See edit for the next problemFridafriday
@Oliver9523 what is the format of input data (void**)? If it's a pointer to two-dimensional array of image data than you should convert it to one-dimensional.Meredeth
I'm assuming it's a 2d array, thanks. I'll have a look at converting it.Fridafriday
+1 for Astor comment: if your data is a 2D array, you must, say, preallocate your cv::Mat and copy the data row by row.Auston
R
1

maybe you should remove * at

cv::Mat TempMat = cv::Mat(h, w, CV_8UC1, *output);

make it just like this:

cv::Mat TempMat = cv::Mat(h, w, CV_8UC1, output);
Reathareave answered 22/9, 2015 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.