Android open cv 使用记录

发现Android自带的图像处理还是不够强大,最近项目中遇到的图像处理相关难题,参照了下C++写的opencv,发现很强大,还提供了android sdk.赞!
下面我们来使用它:
首先,进入官网(http://opencv.org/)下载OpenCV4Android并解压。
具体Android Studio 配置请看http://www.cnblogs.com/tail/p/4618476.html
效果一:
Android open cv 使用记录_第1张图片 —->Android open cv 使用记录_第2张图片
效果二:
Android open cv 使用记录_第3张图片 —->Android open cv 使用记录_第4张图片

public class Tutorial1Activity extends Activity {
    private static final String TAG = "OCVSample::Activity";
    private ImageView drawView;
    private Bitmap srcBitmap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  

        OpenCVLoader.initDebug();

        setContentView(R.layout.tutorial1_surface_view);
        drawView = (ImageView) findViewById(R.id.tutorial1_activity_java_surface_view);
        Button button = (Button) findViewById(R.id.gray_button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                srcBitmap = ((BitmapDrawable)  drawView.getDrawable()).getBitmap();
                drawView.setImageBitmap(procSrc2Gray());
            }
        });
    }
    //效果一:
    public Bitmap procSrcGray(Bitmap srcBitmap){
        Mat rgbMat = new Mat();
        Mat grayMat = new Mat();
        Bitmap grayBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.RGB_565);
        //convert original bitmap to Mat, R G B.
        Utils.bitmapToMat(srcBitmap, rgbMat);
        //rgbMat to gray grayMat
        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY);
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Imgproc.findContours(grayMat, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        Imgproc.drawContours(rgbMat, contours, -1, new Scalar(255,255,255),-1);
        Utils.matToBitmap(rgbMat, grayBitmap); //convert mat to bitmap
        return  grayBitmap;
    }
        //效果二:
    public Bitmap procSrc2Gray() {
        Bitmap srcBitmap88 = Bitmap.createBitmap(srcBitmap).copy(Bitmap.Config.RGB_565, true);
        Bitmap grayBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.RGB_565);

        Mat gray = new Mat();
        Mat bw = new Mat();
        Mat src = new Mat();

        //convert original bitmap to Mat, R G B.
        Utils.bitmapToMat(srcBitmap88, src);
        //rgbMat to gray grayMat
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_RGB2GRAY);
        Imgproc.threshold(gray, bw, 150, 255, Imgproc.THRESH_BINARY_INV);
        Mat temp = bw.clone();

        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Imgproc.findContours(temp, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        Imgproc.drawContours(bw, contours, -1, new Scalar(255, 255, 255), -1);
        Mat rgb = new Mat();

        Imgproc.cvtColor(bw, rgb, Imgproc.COLOR_GRAY2RGB);
        //convert mat to bitmap
        Utils.matToBitmap(rgb, grayBitmap);         
        return grayBitmap;
    }
}

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >

    <ImageView  android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tutorial1_activity_java_surface_view" android:src="@drawable/bg1" />

    <Button  android:id="@+id/gray_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="变灰"/>

</FrameLayout>

调用opencv的代码是在C++高手指导下完成的,感谢opencv,感谢C++.

你可能感兴趣的:(android,opencv)