0

In my App, i open the native android camera and i render the preview on a TextureView, and what i want to do next is, to take each frame from the camera and perform some processing and operations using OpenCV library.

I also referred to some posts here Android decode Bitmap from Camera Preview , how to get RGB values of bitmap in android , How to capture preview image frames from Camera Application in Android Programming?

but i donot know to which format i should convert the retrieved frames especially that i want to use it in opeCV as i said earlier, also which format yields good performance, should i convert them into YUV, JPEG or NV21?

update

the reason why i want to use native android camera and then convert the frames retrieved from the camera to another format applicable to openCV is,when i used CvCameraViewListener2, there are always a black area surrounds the camera preview as shown below in the picture and i could not get rid of it so i decided to use the native android camera instead

image:

enter image description here

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • For best performance you should use Camera2 API and YUV_420_888 image format (It's NV21 equal). As far as I know OpenCV can work with such format or can easliy convert to RGB. – Maxim Metelskiy Nov 12 '16 at 06:29
  • @MaximMetelskiy actually i cant work with camera2 API because the sdk version of my device is 18...do u mean that opencv can work with "YUV_420_888" and "NV21 " directly without any perior conversions?? – Amrmsmb Nov 12 '16 at 12:22
  • @MaximMetelskiy how can i convert NV21 to RGB??please advice – Amrmsmb Nov 14 '16 at 06:35
  • Look here http://stackoverflow.com/questions/23122957/opencv-c-create-mat-object-from-android-nv21-image-data-buffer . Are you sure you need RGB image? May be only grayscale part would be enough? Then you could avoid any conversations. – Maxim Metelskiy Nov 14 '16 at 08:41

1 Answers1

0

If your activity implements OpenCV's CameraBridgeViewBase.CvCameraViewListener2, it will also implement the onCameraFrame method which comes with a CvCameraViewFrame parameter.

You can get your image in grayscale or RGBA format from that. This guy does it in this tutorial. This tutorial is about integrating native C++ OpenCV code, but if you do not need to do that, just pay attention to the part where he works on his MainActivity.

EDIT: If you have access to your preview frame's data as a byte array, have you tried simply putting that data into a mat?

Mat foo = new Mat(height, width, type); //type is probably CvType.CV_8UC1
foo.put(0, 0, data);
Reeven
  • 35
  • 1
  • 3
  • yes i know how to implement OpenCV's CameraBridgeViewBase.CvCameraViewListener2 and i did a project with it and it works fine. the reason why i want to use native android camera and then convert the frames retrieved from the camera to another format applicable to openCV is,when i used CvCameraViewListener2, there are always a black area surrounds the camera preview "i will post a picture" and i could not get rid of it so i decided to use the native android camera instead.but what is the format i should convert me frames to so that i cant perform some processing on it using opencv.please advice – Amrmsmb Nov 11 '16 at 13:35
  • tried to mess my comment, obviously. Edited my answer instead. – Reeven Nov 11 '16 at 13:46
  • but the problem is that the balck area surrounds the preview is never accessible. the pixel at position (0,0) refers to the very top left of the preview area and not the black area – Amrmsmb Nov 11 '16 at 13:58