Android opencv tutorial1相關問題

一: 最近嘗試把Opencv配置在安卓上,具體的配置網上有很多。配置成功後直接用android opencv sdk中的sample作了測試,我用的是裏面的tutorial1。
適當修改tutorial1使得demo能從前置攝像頭提取視頻幀。但是修改後發現從前置攝像頭得到的圖像的方向不對,若想旋轉90則可以通過矩陣轉置變換transpose(src, dst)來實現,若想水平或者垂直旋轉可以通過opencv的flip函數來實現圖像方向的翻轉。flip函數的官方api介紹如下

C++: void ocl::flip(const oclMat& src, oclMat& dst, int flipCode)
Parameters:
src – source image.
dst – destination image.
flipCode – specifies how to flip the array: 0 means flipping around the x-axis, positive (e.g., 1) means flipping around y-axis, and negative (e.g., -1) means flipping around both axes.
The function flip flips the array in one of three different ways (row and column indices are 0-based). Supports all data types.

由於此函數屬於Core包,所以還需要import org.opencv.core.Core;

最後在onCameraFrame函數中就可以對幀進行翻轉了。代碼如下

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        Mat srcImg = inputFrame.rgba();
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        { // 豎屏
            // doSomrthing
            Core.flip(srcImg, srcImg, 0);//flip aroud Y-axis
        } else
        {
            // 橫屏時dosomething
            Core.flip(srcImg, srcImg, 1);//flip aroud Y-axis
        }
        return srcImg;
    }

二:此外,若只想讓Android屏幕橫屏顯示,可以在Manifest文件增加如下配置

android:screenOrientation="landscape"       android:configChanges="keyboardHidden|orientation"

增加後的完整Manifest文件爲:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="**.**.**.**">

    <uses-sdk tools:overrideLibrary="org.opencv"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Tutorial1Activity"
            android:screenOrientation="landscape"
            android:configChanges="keyboardHidden|orientation" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <supports-screens android:resizeable="true"
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:anyDensity="true" />

    <uses-permission android:name="android.permission.CAMERA"/>

    <uses-feature android:name="android.hardware.camera" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>

</manifest>

三: 若在AndroidManifest中沒有強制設置app運行的屏幕方向,並想在代碼中對橫屏和豎屏分別進行處理。可以通過下面的代碼來控制

if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        { // 豎屏
            // doSomrthing
        } else
        {
            // 橫屏時dosomething
        }

Configuration在android.content.res.Configuration包中。

四: 從攝像頭拿到幀後把Mat轉化爲byte[]並對圖像數據進行操作,操作後回顯。可通過如下方式實現。

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

        Mat srcImg = inputFrame.rgba();
        int t = srcImg.channels();
        int width = srcImg.width();
        int height = srcImg.height();
        int rows = srcImg.rows();
        int cols = srcImg.cols();
        int imgSize = width*height*srcImg.channels();
        byte buff[] = new byte[imgSize];
        byte dstbuff[] = new byte[imgSize];

        srcImg.get(0, 0, buff); //獲取srcImg中所有的像素
        //do whatever you want using buff
        srcImg.put(0, 0, dstbuff);//將buff放回原mat,完成對mat的修改操作
        return srcImg;
    }

Reference:
http://blog.csdn.net/wunghao8/article/details/38868281
http://blog.csdn.net/watkinsong/article/details/9189649
http://blog.csdn.net/yangtrees/article/details/38279351
http://my.oschina.net/u/1246663/blog/197626

發佈了95 篇原創文章 · 獲贊 127 · 訪問量 44萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章