Ask Your Question
0

Convert pixels to Mat

asked 2013-07-23 01:24:31 -0600

koraxis gravatar image

Hello I am using Vuforia for creating an Augmented Reality Android app. And i want to do image processing on the images i get from Vuforia's camera frames. I am thinking of using OpenCV natively for that only purpose.

Vuforia captures images with its own Image class. but you can get pixels from that image as " short*". So I guess i have to turn those pixels somehow to Mat class so that i can use the image processing methods on it. But i couldnt find how to do that?

Can someone help me about it? Some suggestions

Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-07-23 01:55:19 -0600

The easiest way is to use the constructor that take a pointer to data to initialize a mat.

cv::Mat myImg = cv::Mat( ROWS, COLS, CV_16UC3, ptrToVuforia );

Where ROWS and COLS are the height and width of your image respectively, and ptrToVuforia the pointer of data (aka pixels) from Vuforia. I assume here, that your image is in color (C3) and short is 16 bits, adapt if needed.

edit flag offensive delete link more

Comments

Hi Mathieu, Thank you for the quick answer. Yeah the image is in color. I will try that.

koraxis gravatar imagekoraxis ( 2013-07-23 01:57:33 -0600 )edit

Hi again Mathieu, the app crashes when i try to convert the RGB to GRAYSCALe like this:

Mat mt= Mat(width, height, CV_16UC3, pixels);
Mat graymat;
LOG("Before convert , image type %d",mt.type());
cvtColor(mt,graymat,CV_BGR2GRAY);

It crashes during the cvtColor method. And the LOG gives the image type as 18. Is it a normal value? What do you think the problem can be

koraxis gravatar imagekoraxis ( 2013-07-23 03:03:34 -0600 )edit

height must be the first parameter:

Mat mt= Mat(height, width, CV_16UC3, pixels);
Vladislav Vinogradov gravatar imageVladislav Vinogradov ( 2013-07-23 03:14:29 -0600 )edit

I changed it like that, but still it crashes at the same point.

koraxis gravatar imagekoraxis ( 2013-07-23 03:20:03 -0600 )edit

Could you see your picture before the convert? Are you sure your short are 16 bits? By the way, if you have short, not unsigned short, it probably should be CV_16SC3.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2013-07-23 03:37:30 -0600 )edit

Its normal short, so i changed it to CV_16SC3 , but this time it crashed with an error that says : " Assertion failed (depth == CV_8U || depth == CV_16U || depth == CV_32F)" in cvColor()

Also i can see the picture. When i use mt.data() and send it to the Java part as byte[] and create bitmap, i can see the picture in color.

This is how i get the pixels from vuforia:

short* input= (short*) imageRGB565->getPixels();
int width = imageRGB565->getWidth();
int height = imageRGB565->getHeight();
koraxis gravatar imagekoraxis ( 2013-07-23 03:52:30 -0600 )edit
1

cvtColor is suppose to work with unsigned short, not signed one, so I suggest you convert your image to unsigned short. Add 32768 to all your pixels to have only positive values... Ugly, but I don't have better solution yet... After that, convert to gray scale.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2013-07-23 04:20:05 -0600 )edit

Your image has RGB565 format:

Mat mt565 = Mat(width, height, CV_16UC1, pixels);
Mat graymat;
cvtColor(mt565, graymat, COLOR_BGR5652GRAY);
Vladislav Vinogradov gravatar imageVladislav Vinogradov ( 2013-07-23 07:17:37 -0600 )edit

How can we find out the format of a Mat object? So that i can choose the proper converting selection for it.

Vladislav, when i tried the way you described i am getting this error: Assertion failed (scn == 2 && depth == CV_8U).

koraxis gravatar imagekoraxis ( 2013-07-23 09:34:51 -0600 )edit

This is the type() function of a Mat. It gives you an int not a string, so you have to make the conversion yourself.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2013-07-23 16:43:42 -0600 )edit

Question Tools

Stats

Asked: 2013-07-23 01:24:31 -0600

Seen: 2,817 times

Last updated: Jul 23 '13