手势动态识别(基于opencv的简单实现)
http://blog.csdn.net/anqing715/article/details/17653709
基于OpenCV的实时手势识别算法研究_许艳旭
http://wenku.baidu.com/view/c7a2c97e998fcc22bdd10d11.html
基于OpenCV动态手势识别的实现
http://wenku.baidu.com/view/e0c708efad51f01dc281f1dd.html
MainActivity.java
package com.duanjiwei.takephoto;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
//public class MainActivity extends AppCompatActivity {
public class MainActivity extends Activity {
public static final int TAKE_PHOTO = 1;
//public static final int CROP_PHOTO = 2;
private Button takephoto;
private ImageView picture;
private Uri imageUri;
Mat imageMat;
private static final String TAG = "OCVSample::Activity";
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
imageMat=new Mat();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
@Override
public void onResume()
{
super.onResume();
if (!OpenCVLoader.initDebug()) {
Log.d("OpenCV", "Internal OpenCV library not found. Using OpenCV Manager for initialization");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_10, this, mLoaderCallback);
} else {
Log.d("OpenCV", "OpenCV library found inside package. Using it!");
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takephoto = (Button)findViewById(R.id.take_photo);
picture = (ImageView)findViewById(R.id.picture);
takephoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File outputImage = new File(Environment.getExternalStorageDirectory(), "output_image.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
switch (requestCode){
case TAKE_PHOTO:
//if (resultCode == RESULT_OK){
//Intent intent = new Intent("com.android.camera.action.CROP");
//intent.setDataAndType(imageUri, "image/*");
// intent.putExtra("scale", true);
//intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
//startActivityForResult(intent, CROP_PHOTO);
//}
//break;
//case CROP_PHOTO:
if (resultCode == RESULT_OK){
try{
//获取原图
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
picture.setImageBitmap(bitmap);
//图像处理
Mat mat_src = new Mat(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC4);
Utils.bitmapToMat(bitmap, mat_src);
Mat mat_gray = new Mat(mat_src.cols(), mat_src.rows(), CvType.CV_8UC1);
Imgproc.cvtColor(mat_src, mat_gray, Imgproc.COLOR_BGRA2GRAY, 1);
//-------------------------------------------------------------------
Mat frame = new Mat(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC4);
mat_src.copyTo(frame);
Mat frameHSV = new Mat(mat_src.cols(), mat_src.rows(), CvType.CV_8UC3);; // hsv空间
Mat mask = new Mat(mat_src.cols(), mat_src.rows(), CvType.CV_8UC1);
Mat dst = new Mat(mat_src.cols(), mat_src.rows(), CvType.CV_8UC3); // 输出图像
//dst.copyTo(frame);
// 中值滤波,去除椒盐噪声
Imgproc.medianBlur(frame, frame, 5);
Imgproc.cvtColor(frame, frameHSV, Imgproc.COLOR_RGB2HSV, 3);
Mat dstTemp1 = new Mat(mat_src.cols(), mat_src.rows(), CvType.CV_8UC1);
Mat dstTemp2 = new Mat(mat_src.cols(), mat_src.rows(), CvType.CV_8UC1);
// 对HSV空间进行量化,得到二值图像,亮的部分为手的形状
Core.inRange(frameHSV, new Scalar(0, 30, 30), new Scalar(40, 170, 256), dstTemp1);
Core.inRange(frameHSV, new Scalar(156, 30, 30), new Scalar(180, 170, 256), dstTemp2);
Core.bitwise_or(dstTemp1, dstTemp2, mask);
// 形态学操作,去除噪声,并使手的边界更加清晰
Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
Imgproc.erode(mask, mask, element);
Imgproc.morphologyEx(mask, mask, Imgproc.MORPH_OPEN, element);
Imgproc.dilate(mask, mask, element);
Imgproc.morphologyEx(mask, mask, Imgproc.MORPH_CLOSE, element);
frame.copyTo(dst, mask);
// List contourList = new ArrayList();
// Mat hierarchy = new Mat();
//
// // 得到手的轮廓
// Imgproc.findContours(mask, contourList, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
//
// Random r = new Random();
// int nContoursNum = contourList.size();
// for (int i = 0; i < nContoursNum; i++)
// {
// Imgproc.drawContours(dst, contourList, i, new Scalar(r.nextInt(255), r.nextInt(255), r.nextInt(255)), -1);
// }
//-------------------------------------------------------------------
//图像显示
Bitmap bmp_gray = Bitmap.createBitmap(mat_gray.cols(), mat_gray.rows(), Bitmap.Config.ARGB_8888 );
Utils.matToBitmap(dst, bmp_gray);
picture.setImageBitmap(bmp_gray);
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
break;
default:
break;
}
}
}
activity_main.xml