openCV for Android的搭建并把普通圖像轉(zhuǎn)換為灰度圖

1.下載openCV for Android 庫 鏈接 https://github.com/opencv/opencv/releases/tag/3.3.0
2.導(dǎo)入添加依賴,

QQ截圖20171113233914.jpg

QQ截圖20171113234127.jpg

QQ截圖20171113234538.jpg

openCV SDK結(jié)構(gòu)


QQ截圖20171113234710.jpg

3.導(dǎo)入libs


QQ截圖20171113234929.jpg

QQ截圖20171113235259.jpg

so mips就不導(dǎo)了
最終是這個樣子的


QQ截圖20171113235528.jpg

4.openCVLibrary的build.gradle和你項目的build.gradle 的 需要保持一致
自己項目的:

  compileSdkVersion 26
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.doudoubird.opencv_one"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
}

openCV的

    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
 //       applicationId "com.doudoubird.myopencv"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

5.最后需要構(gòu)建自己的Native_Libs, build.gradle中需要這樣寫

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "com.doudoubird.opencv_one"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile project(':openCVLibrary330')
}
task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {
    destinationDir file("$projectDir/libs")
    baseName "Native_Libs2"
    extension "jar"
    from fileTree(dir: "libs", include: "**/*.so")
    into "lib"
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn(nativeLibsToJar)
}

以上也可以參考這篇博客 http://blog.csdn.net/qq_18870023/article/details/58203990
現(xiàn)在可以編寫代碼把一張彩色圖變成灰度圖啦

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private static String  TAG = "TAG";
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initLoadOpenCVLibs();
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    private void initLoadOpenCVLibs() {
        boolean success = OpenCVLoader.initDebug();
        if (success){
            Log.e(TAG, "加載完了 ");
        }
    }

    @Override
    public void onClick(View view) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher,options);
        Mat src = new Mat();
        Mat dst = new Mat();
        Utils.bitmapToMat(bitmap,src);
        Imgproc.cvtColor(src,dst,Imgproc.COLOR_BGRA2GRAY);
        Utils.matToBitmap(dst,bitmap);
        ImageView imagview = (ImageView) this.findViewById(R.id.imageView);
        imagview.setImageBitmap(bitmap);
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    tools:context="com.doudoubird.opencv_one.MainActivity">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="環(huán)境測試-灰度"
         />

    <ImageView
        android:layout_below="@+id/button"
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@mipmap/ic_launcher"
        />

</RelativeLayout>

效果如下

沒變前
Screenshot_2017-11-14-00-14-24-139_com.doudoubird.png

變后
Screenshot_2017-11-14-00-14-28-472_com.doudoubird.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末驹闰,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子准浴,更是在濱河造成了極大的恐慌汽纤,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件剪侮,死亡現(xiàn)場離奇詭異金赦,居然都是意外死亡音瓷,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門夹抗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绳慎,“玉大人,你說我怎么就攤上這事⊥迪撸” “怎么了磨确?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長声邦。 經(jīng)常有香客問我乏奥,道長,這世上最難降的妖魔是什么亥曹? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任邓了,我火速辦了婚禮,結(jié)果婚禮上媳瞪,老公的妹妹穿的比我還像新娘骗炉。我一直安慰自己,他們只是感情好蛇受,可當我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布句葵。 她就那樣靜靜地躺著,像睡著了一般兢仰。 火紅的嫁衣襯著肌膚如雪乍丈。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天把将,我揣著相機與錄音轻专,去河邊找鬼。 笑死察蹲,一個胖子當著我的面吹牛请垛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播洽议,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼宗收,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了绞铃?” 一聲冷哼從身側(cè)響起镜雨,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎儿捧,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體挑宠,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡菲盾,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了各淀。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片懒鉴。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出临谱,到底是詐尸還是另有隱情璃俗,我是刑警寧澤,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布悉默,位于F島的核電站城豁,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏抄课。R本人自食惡果不足惜唱星,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望跟磨。 院中可真熱鬧间聊,春花似錦、人聲如沸抵拘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽僵蛛。三九已至尚蝌,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間墩瞳,已是汗流浹背驼壶。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留喉酌,地道東北人热凹。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像泪电,于是被迫代替她去往敵國和親般妙。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 聲明:由于簡書寫作不便相速,后續(xù)將在掘金上更新和發(fā)布文章碟渺,包括本文。 掘金賬號:徒步青云[https://juejin...
    徒步青云閱讀 10,230評論 2 34
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,167評論 25 707
  • 本文翻譯自O(shè)penCV in Android Studio - stackoverflow[1]的第二個回答突诬,略有...
    menglj閱讀 4,961評論 4 10
  • 1. 正月里去舅媽家拜年苫拍,恰巧碰到了回娘家的姐姐,雖然年過三十旺隙,但姐姐繼承了舅媽的美貌绒极,皮膚白皙透亮,小臉微圓蔬捷,眸...
    花子魚閱讀 1,315評論 0 0
  • rn頁面之間跳轉(zhuǎn)時默認一直顯示tabbar垄提,然而在開發(fā)過程中必須要切換tabbar的顯示和隱藏榔袋。在嘗試過很多次之后...
    金絲楠閱讀 1,859評論 1 3