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函数就可以了。
Flips a 2D array around vertical, horizontal, or both axes.
The function
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 (
Horizontal flipping of the image with the subsequent horizontal shift and absolute difference calculation to check for a vertical-axis symmetry (
Simultaneous horizontal and vertical flipping of the image with the subsequent shift and absolute difference calculation to check for a central symmetry (
Reversing the order of point arrays (
Parameters:
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);
现在再试试,是不是跟自带的相机显示效果一样了?
使用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
flipflips 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 > 0or
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);
现在再试试,是不是跟自带的相机显示效果一样了?
相关文章推荐
- OpenCV手机摄像头方向的问题
- OpenCV 2.2.0 摄像头捕获视频问题的解决
- win7 旗舰 vs2008 OpenCV2.3.1 解决了 OpenCV旧版本在win7下不能打开摄像头的问题
- 关于opencv摄像头图像显示 经常出现select timeout的问题
- OpenCV打开摄像头问题
- java解决手机等移动设备中照片上传至服务器方向不正确的问题
- OpenCV摄像头采集分辨率设置问题
- opencv VideoCapture调用摄像头无响应问题
- 解决OpenCV在使用摄像头捕捉视频流时出现的一个奇怪问题
- 【VS2010学习笔记】【函数学习】一(MFC+OpenCV2.4.7读取摄像头之WM_TIMER消息处理函数的添加问题)
- 使用OpenCV 实现多摄像头图像处理笔记(1):DirectShow的配置过程可能出现的问题
- opencv 用摄像头提取图片后图片命名问题
- OpenCV摄像头采集图像问题及DirectShow的使用
- Zedboard摄像头获取视频设计问题篇(四)-OpenCV+QT配置显示图片
- QT+opencv+摄像头调试过程中遇到的问题
- opencv调用摄像头在mfc控件中显示黑屏问题的解决
- ubuntu11.10下安装opencv2.3.1(已经解决读取摄像头问题)
- ubuntu 下用 opencv驱动笔记本摄像头问题解决方法
- 关于opencv2.4.9和VS2013在windows7下无法打开摄像头的问题的扯淡解决办法
- Android 打开相机获得路径:注意三星手机图片方向问题。