在android平臺上利用opencv進行影象處理之邊沿檢測
阿新 • • 發佈:2019-02-03
由於專案的需要,這幾天開始接觸opencv,要在android平臺上利用opencv進行影象處理。以下是這幾天的成果:
執行結果如下:
原始圖片:
處理後的圖片:
Java程式碼:
AndroidMenifest.xml檔案:package com.cdq.opencvtest; import java.io.File; import org.opencv.android.BaseLoaderCallback; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.Size; import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc; import android.os.Bundle; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private ImageView iv = null; private Button btn1 = null; private Button btn2 = null; String tag="MainActivity"; //OpenCV類庫載入並初始化成功後的回撥函式 private BaseLoaderCallback mLoader = new BaseLoaderCallback(this) { @Override public void onManagerConnected(int status) { switch (status) { case LoaderCallbackInterface.SUCCESS: { }break; default: { super.onManagerConnected(status); }break; } } }; @Override protected void onResume() { super.onResume(); //通過OpenCV引擎服務載入並初始化OpenCV類庫,所謂OpenCV引擎服務即是 OpenCV Manager OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this,mLoader); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/face8.jpg"); iv.setImageBitmap(bitmap); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Mat img = Highgui.imread("/sdcard/face8.jpg", 0); Size dSize = new Size((double) img.width(), (double) img.height()); Mat img2 = new Mat(dSize, CvType.CV_8SC1); Mat img3 = new Mat(); img.convertTo(img2, CvType.CV_8SC1); Imgproc.Canny(img, img3, 123, 250); boolean flag = Highgui.imwrite("/sdcard/new.jpg", img3); Log.i(tag, "onClick"); if (flag) { Log.i(tag, "flag"); File file = new File("/sdcard/new.jpg"); if (file.exists()) { Bitmap bitmap2 = BitmapFactory.decodeFile("/sdcard/new.jpg"); iv.setImageBitmap(bitmap2); } } else { Toast.makeText(getApplicationContext(), "圖片寫入失敗", Toast.LENGTH_SHORT).show(); } } }); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/face8.jpg"); iv.setImageBitmap(bitmap); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cdq.opencvtest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.cdq.opencvtest.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
佈局檔案:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity" > <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" > <ImageView android:id="@+id/iv" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="9" /> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="處理" android:layout_weight="1"/> <Button android:id="@+id/btn2" android:layout_width="0dp" android:layout_height="wrap_content" android:text="恢復" android:layout_weight="1"/> </TableRow> </TableLayout> </LinearLayout>
實踐過程中,一直有個問題糾結了我很久——java.lang.UnsatisfiedLinkError: Native method not found: org.opencv.highgui.Highgui.imread_0:(Ljava/lang/String;I)。在網上看了很多資料後,才明白其實是程式沒有載入opencv類庫。於是我加入了以下程式碼後,問題得到了解決:
//OpenCV類庫載入並初始化成功後的回撥函式
private BaseLoaderCallback mLoader = new BaseLoaderCallback(this)
{
@Override
public void onManagerConnected(int status)
{
switch (status)
{
case LoaderCallbackInterface.SUCCESS:
{
}break;
default:
{
super.onManagerConnected(status);
}break;
}
}
};
@Override
protected void onResume()
{
super.onResume();
//通過OpenCV引擎服務載入並初始化OpenCV類庫,所謂OpenCV引擎服務即是 OpenCV Manager
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this,mLoader);
}