Ask Your Question

Android camera image format is not NV21

asked 2013-02-05 07:39:53 -0600

arsalank2 gravatar image

updated 2013-02-05 09:22:58 -0600

I managed to capture the camera frames on the native side by calling:

cv::VideoCapture videoCapture(CV_CAP_ANDROID + 0);
videoCapture.set(CV_CAP_PROP_FRAME_WIDTH, 320);
videoCapture.set(CV_CAP_PROP_FRAME_HEIGHT, 240);

cv::Mat image;
videoCapture >> image;

However, the captured image has 3-bytes per pixel, as compared to Android native capture which is 1.5 bytes per pixel. For example, in case of 320x240, the number of bytes in the image captured through OpenCV is 230400, while they are 115200 when captured by Android API.

How can I capture images with NV21 format (i.e. 1.5 bytes per pixel).

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-02-05 07:49:32 -0600

  1. You cannot change color conversion in cv::VideoCapture without code changes.

  2. As I know all production devices and all Android emulators since Android 2.3 support NV21 frame format. It is one of the format, that all devices must support. The is only problem with some old Android emulators, i.e. Android 2.2, that retries frames in some strange non standard format. It is emulator bug and I do not recommend you to use it.

edit flag offensive delete link more


I have tested it with various devices (e.g. Samsung Galaxy S2 and S3), not on an emulator. I do not want to change to another color format, I just need NV21 which is the standard.

Why cv::Mat has 3 bytes per pixel instead of 1.5? Am I reading it incorrectly?

arsalank2 gravatar imagearsalank2 ( 2013-02-05 08:19:31 -0600 )edit

cv::VideoCapture does not support output in raw camera format, i.e. NV21. It supports CV_CAP_ANDROID_COLOR_FRAME_BGR, CV_CAP_ANDROID_COLOR_FRAME_RGB, CV_CAP_ANDROID_COLOR_FRAME_BGRA, CV_CAP_ANDROID_COLOR_FRAME_RGBA and CV_CAP_ANDROID_GREY_FRAME. This formats are the most popular and abstract from hardware implementation.

Alexander Smorkalov gravatar imageAlexander Smorkalov ( 2013-02-05 12:00:46 -0600 )edit

Thanks for the information. Is CV_CAP_ANDROID_COLOR_FRAME_RGB default format?

Next question, how to change the camera format? I could not find any suitable method in VideoCapture. I tried setting the properties (VideoCapture::set)CV_CAP_PROP_FOURCC,CV_CAP_PROP_FORMATandCV_CAP_PROP_CONVERT_RGB` but the application crashes with following error:

OpenCV Error: One of arguments' values is out of range.
arsalank2 gravatar imagearsalank2 ( 2013-02-07 06:05:05 -0600 )edit

You set frame format in retrieve method: camera->retrieve(image, format)

Alexander Smorkalov gravatar imageAlexander Smorkalov ( 2013-02-08 01:27:20 -0600 )edit

Question Tools


Asked: 2013-02-05 07:39:53 -0600

Seen: 3,360 times

Last updated: Feb 05 '13