您的位置:首页 > 运维架构

OpenCV手机摄像头方向的问题

2014-08-27 12:35 267 查看
来intel中国研究院实习也有一段时间了,做了挺多东西,主要还是利用OpenCV检测人脸、识别人脸和一些其它的拓展的相关的东西,应该抽时间把自己遇到的问题简单的写出来,一来自己也可总结一下,二来希望可以帮助遇到类似问题的朋友。
使用OpenCV的Android接口,要想显示从摄像头输入的视频,可以使用CameraBridgeViewBase类,然后在public Mat onCameraFrame(CvCameraViewFrame inputFrame) 函数中使用代码:
mRgba = inputFrame.rgba();//color frame

mGray = inputFrame.gray();// gray frame
来得到摄像头视频的每一个frame,在这个函数中进行各种处理。最后return mRgba后就可以在界面上显示了,当然xml中的视频显示部分如下:
<org.opencv.android.JavaCameraView

android:layout_width="200dp"

android:layout_height="150dp"

android:id="@+id/fd_activity_surface_view" />
具体代码可以参考Sample中的代码。
但是显示的过程是有一些问题的,比方说最简单的人脸检测的时候,我们一般用前置摄像头,然后检测人脸,使用OpenCV自带的显示的过程中和平常是不一样的,也就是说和自带相机的那种显示是不一样的,相机的显示就类似镜子的显示,而OpenCV则接近真实显示,即一个一模一样的你和你对立站立。这样的话,当你旋转手机的时候问题就出现了,当旋转90度的时候,显示的视频和你真实的方向就正好差了180度,也就成反的了,这在需要检测人脸的应用中很不方便,因为反转的图片OpenCV是检测不到人脸的。
其实解决方法非常简单,大家想想从反面看洗出来的纸照片是什么效果?明白了吧,我们只需要把得到的frame绕竖方向,即Y轴旋转180即可,这样就可以达到Mirror效果,你打开相机用前置摄像头看看,相机都是mirror效果的。想实现图翻转OpenCV中的flip函数就可以了。

flip

public static void flip(Mat src,
Mat dst,
int flipCode)


Flips a 2D array around vertical, horizontal, or both axes.

The function
flip
flips the array in one of three different ways (row and column indices are 0-based):

dst _(ij) =<BR> <= ft(<BR> ltBR gtsrc _(src.rows-i-1,j) if flipCode = 0 ltBR gtsrc _(i, src.cols -j-1) if flipCode gt 0 ltBR gtsrc _(src.rows -i-1, src.cols -j-1) if flipCode lt 0 ltBR gt<BR>right.

The example scenarios of using the function are the following:

Vertical flipping of the image (
flipCode == 0
) to switch between top-left and bottom-left image origin. This is a typical operation in video processing on Microsoft Windows* OS.
Horizontal flipping of the image with the subsequent horizontal shift and absolute difference calculation to check for a vertical-axis symmetry (
flipCode > 0
).
Simultaneous horizontal and vertical flipping of the image with the subsequent shift and absolute difference calculation to check for a central symmetry (
flipCode < 0
).
Reversing the order of point arrays (
flipCode > 0
or
flipCode == 0
).

Parameters:
src
- input array.
dst
- output array of the same size and type as
src
.
flipCode
- a flag to specify how to flip the array; 0 means flipping around the x-axis and positive value (for example, 1) means flipping around y-axis. Negative value (for example, -1) means flipping around both axes (see the discussion below
for the formulas).

函数很简单,一个输入Mat,一个输出Mat,filpCode有三种情况,0为绕X轴翻转,大于的0代表绕Y轴翻转,-1代表即桡X轴也绕Y轴翻转。因此如下代码就可以实现mirror效果了:mRgba = inputFrame.rgba();//color frame

mGray = inputFrame.gray();// gray frameCore.flip(mRgba, mRgba, 1);//flip aroud Y-axis

Core.flip(mGray, mGray, 1);
现在再试试,是不是跟自带的相机显示效果一样了?


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: