在Android平台上利用opencv进行图像处理之边缘检测、灰度变换、缩小
来源:互联网 发布:stussy靠谱的淘宝店 编辑:程序博客网 时间:2024/06/10 11:48
转载请注明本文出自这么远_那么近的博客(http://blog.csdn.net/u010585964),谢谢支持!
在网上找资料学习利用opencv开发Android还算挺顺利的,以下是今天在Android平台上利用opencv对图片进行边缘检测、灰度变换、缩小的实现。
运行结果如下:
代码如下:
MainActivity:
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.android.Utils;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.os.Environment;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;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{String sdcard = Environment.getExternalStorageDirectory().getPath();private ImageView iv = null;private Button btn1 = null;private Button btn2 = null;private Button btn3 = null;private Button btn4 = null;private Bitmap btmp=null;String tag = "MainActivity";// OpenCV类库加载并初始化成功后的回调函数private BaseLoaderCallback mLoader = new BaseLoaderCallback(this){@Overridepublic void onManagerConnected(int status){switch (status){case LoaderCallbackInterface.SUCCESS:{}break;default:{super.onManagerConnected(status);}break;}}};@Overrideprotected void onResume(){super.onResume();// 通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 OpenCV ManagerOpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoader);}@Overrideprotected 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);btn3 = (Button) findViewById(R.id.btn3);btn4 = (Button) findViewById(R.id.btn4);btmp = BitmapFactory.decodeFile("/sdcard/face8.jpg");iv.setImageBitmap(btmp);btn1.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0){bianyuanjiance();}});btn2.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0){huidubianhuan();}});btn3.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0){suoxiao();}});btn4.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0){iv.setImageBitmap(btmp);}});}protected void suoxiao(){double scale=0.5;Mat img1 = Highgui.imread("/sdcard/face8.jpg");Size size=new Size(img1.width()*scale,img1.height()*scale);Mat img2=new Mat(size,CvType.CV_16S);Imgproc.resize(img1,img2,size);Boolean flag=Highgui.imwrite("/sdcard/suoxiao.jpg", img2);if (flag){Bitmap bitmap=BitmapFactory.decodeFile("/sdcard/suoxiao.jpg");iv.setImageBitmap(bitmap);}else {Toast.makeText(getApplicationContext(), "图片写入失败", Toast.LENGTH_SHORT).show();}}protected void huidubianhuan(){Bitmap btmp2=Bitmap.createBitmap(btmp.getWidth(), btmp.getHeight(), Config.RGB_565);Mat rgbImg = Highgui.imread("/sdcard/face8.jpg");Size size=new Size((double)rgbImg.width(),(double)rgbImg.height());Mat grayImg=new Mat(size,CvType.CV_8UC1);Imgproc.cvtColor(rgbImg, grayImg, Imgproc.COLOR_RGB2GRAY);Utils.matToBitmap(grayImg, btmp2);iv.setImageBitmap(btmp2);}private void bianyuanjiance(){Mat img = Highgui.imread("/sdcard/face8.jpg");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/huidubianhuan.jpg", img3);Log.i(tag, "onClick");if (flag){Log.i(tag, "flag");File file = new File("/sdcard/huidubianhuan.jpg");if (file.exists()){Bitmap bitmap2 = BitmapFactory.decodeFile(sdcard + "/huidubianhuan.jpg");iv.setImageBitmap(bitmap2);}}else{Toast.makeText(getApplicationContext(), "图片写入失败", Toast.LENGTH_SHORT).show();}}@Overridepublic 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;}}activity_main.xml:
<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" > <TableRow android:id="@+id/tableRow3" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9" android:gravity="center" > <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableRow> <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"/><Button android:id="@+id/btn3" android:layout_width="0dp" android:layout_height="wrap_content" android:text="缩小一半" android:layout_weight="1"/> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > </TableRow> <Button android:id="@+id/btn4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="恢复" /> </TableLayout></LinearLayout>Manifest.xml:
<?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>
0 0
- 在Android平台上利用opencv进行图像处理之边缘检测、灰度变换、缩小
- 在android平台上利用opencv进行图像处理之边沿检测
- 利用Opencv进行图像灰度变换处理
- Android平台上利用opencv进行图像的边沿检测
- Android平台上利用opencv进行图像的边沿检测
- opencv图像边缘检测使用,先做平滑处理在进行边缘检测
- 【OpenCV图像处理】二十二、图像边缘检测(上)
- Android平台上利用opencv进行图像的缩放
- OpenCV简单图像处理 灰度变换
- OpenCV图像处理篇之边缘检测算子
- 利用图像在水平和垂直方向上的差分运算对图像进行边缘检测
- OpenCV-将图像转化为灰度图,然后边缘检测
- Qt 图像处理之 灰度变换
- Matlab图像处理之灰度变换函数
- 【OpenCV图像处理】九、常见的图像灰度变换
- 【OpenCV】图像的变换(三)-Canny边缘检测
- 图像边缘检测--OpenCV之cvCanny函数
- 图像边缘检测--OpenCV之cvCanny函数
- LINUX总结第16篇:Linux中的lo回环接口详细介绍
- .NET(C#): 多种输出字节数组的方法
- Matlab混合编程中编译器设置问题
- SQLite Select 语句(http://www.w3cschool.cc/sqlite/sqlite-select.html)
- SQLite 运算符(http://www.w3cschool.cc/sqlite/sqlite-operators.html)
- 在Android平台上利用opencv进行图像处理之边缘检测、灰度变换、缩小
- Sass进阶
- Ksvd算法
- cocos2d-x项目中含CocoStudio移植Android时Android.mk的内容
- SQLite 表达式(http://www.w3cschool.cc/sqlite/sqlite-expressions.html)
- 5Foundation Kit 字典NSDictionary
- 用C++11的std::async代替线程的创建
- 《unix高级环境编程》信号——信号基本概述
- SQLite Where 子句(http://www.w3cschool.cc/sqlite/sqlite-where-clause.html)