在android中使用OpenCv从图像中检测方形对象

时间:2015-12-14 13:05:39

标签: android opencv image-processing

Detecting Square Image from the picture

以下是我使用的代码

import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.io.InputStream;

public class MainActivity extends Activity {
    ImageView iv;
    protected BaseLoaderCallback OpenCVCallBack = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    System.out.println("tester");
                    onOpenCVReady();
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);




}
private void OutputGrayMatToFile(Mat mGaryMat, String Filename)
{
    ImageView img = (ImageView) findViewById(R.id.imgBelow);
    Mat mRgba = new Mat();
    Imgproc.cvtColor(mGaryMat, mRgba, Imgproc.COLOR_GRAY2BGRA, 4);
    Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(mRgba, bmp);
    img.setImageBitmap(bmp);

}

@Override
protected void onResume() {
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0,this,OpenCVCallBack);
}

protected void onOpenCVReady(){
    //this should crash if opencv is not loaded
    Mat img = new Mat();
    Toast.makeText(getApplicationContext(), "opencv ready", Toast.LENGTH_LONG).show();
    Bitmap footbm = BitmapFactory.decodeResource(getResources(), R.drawable.img);
  //  Bitmap footbm = BitmapFactory.decodeStream(is);

    Mat footMat = new Mat();
    //convert bitmap to opencv Mat
    Utils.bitmapToMat(footbm, footMat);

    //Convert to Gray image
    Mat footGrayMat = new Mat();
    Imgproc.cvtColor(footMat, footGrayMat, Imgproc.COLOR_BGR2GRAY, 1);

    //Do canny
    Mat outCannyMat = new Mat();
    Imgproc.Canny(footGrayMat, outCannyMat, 80, 100, 3, false);

    //output to file
    OutputGrayMatToFile(outCannyMat, "Canny");
}

}

问题 如何从中获取方形图像我不知道这是我第一次使用OpenCV和图像处理。 到目前为止,我达到了这一点并对它感到非常沮丧或任何人请告诉我有关图像中对象检测的任何其他更好的方法

0 个答案:

没有答案