如何使用OpenCv从图像中检测多个轮廓?

时间:2019-02-11 10:48:53

标签: java android opencv image-processing opencv4android

我正在开发一个android应用程序。目前,我只能从输入图像中检测轮廓。

我认为问题出在图像预处理上。

我尝试了多种解决方案,但似乎不太起作用的一种解决方案是应用拉普拉斯滤波器,然后应用Canny边缘检测器,但问题是轮廓不完整,这会阻止程序检测形状。

这是我目前正在使用的代码:

    Mat mRGBA = new Mat();

    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.coffret1);
    //compress bitmap
    bmp = getResizedBitmap(bmp, 500);
    Utils.bitmapToMat(bmp, mRGBA);

    Mat mGray = new Mat();



    // Change the background from white to black, since that will help later to
    // extract
    // better results during the use of Distance Transform
    Mat src = mRGBA.clone();
    byte[] srcData = new byte[(int) (src.total() * src.channels())];
    src.get(0, 0, srcData);
    for (int i = 0; i < src.rows(); i++) {
        for (int j = 0; j < src.cols(); j++) {
            if (srcData[(i * src.cols() + j) * 3] == (byte) 255 && srcData[(i * src.cols() + j) * 3 + 1] == (byte) 255
                    && srcData[(i * src.cols() + j) * 3 + 2] == (byte) 255) {
                srcData[(i * src.cols() + j) * 3] = 0;
                srcData[(i * src.cols() + j) * 3 + 1] = 0;
                srcData[(i * src.cols() + j) * 3 + 2] = 0;
            }
        }
    }
    src.put(0, 0, srcData);

    // Create a kernel that we will use to sharpen our image
    Mat kernel = new Mat(3, 3, CvType.CV_32F);
    // an approximation of second derivative, a quite strong kernel
    float[] kernelData = new float[(int) (kernel.total() * kernel.channels())];
    kernelData[0] = 1; kernelData[1] = 1; kernelData[2] = 1;
    kernelData[3] = 1; kernelData[4] = -8; kernelData[5] = 1;
    kernelData[6] = 1; kernelData[7] = 1; kernelData[8] = 1;
    kernel.put(0, 0, kernelData);
    // do the laplacian filtering as it is
    // well, we need to convert everything in something more deeper then CV_8U
    // because the kernel has some negative values,
    // and we can expect in general to have a Laplacian image with negative values
    // BUT a 8bits unsigned int (the one we are working with) can contain values
    // from 0 to 255
    // so the possible negative number will be truncated
    Mat imgLaplacian = new Mat();
    Imgproc.filter2D(src, imgLaplacian, CvType.CV_32F, kernel);
    Mat sharp = new Mat();
    src.convertTo(sharp, CvType.CV_32F);
    Mat imgResult = new Mat();
    Core.subtract(sharp, imgLaplacian, imgResult);
    // convert back to 8bits gray scale
    imgResult.convertTo(imgResult, CvType.CV_8UC3);
    imgLaplacian.convertTo(imgLaplacian, CvType.CV_8UC3);
    // Create binary image from source image
    Mat bw = new Mat();
    Imgproc.cvtColor(imgResult, bw, Imgproc.COLOR_BGR2GRAY);
   //Imgproc.threshold(bw, bw, 40, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);
    Imgproc.adaptiveThreshold(bw, bw, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 40);
    Imgproc.GaussianBlur(bw, bw, new Size(5, 5), 5);
    Imgproc.Canny(bw, bw, 80, 300, 3, false);
    Mat kernell = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
    Imgproc.morphologyEx(bw, bw, Imgproc.MORPH_CLOSE, kernell);

     Bitmap bitm = Bitmap.createBitmap(bw.cols(), bw.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(bw, bitm);

    if(bitm!=null){
        Toast.makeText(getApplicationContext(), "Bitmap not null", Toast.LENGTH_SHORT).show();
        imgView.setImageBitmap(bitm);
    }else{
        Toast.makeText(getApplicationContext(), "Bitmap null", Toast.LENGTH_SHORT).show();
    }

这是原始图片

https://i.imgur.com/JbGoeLl.jpg

这是结果图像

https://i.imgur.com/Y4bNEZg.png

如何解决此问题?谁能修复我的代码?

0 个答案:

没有答案
相关问题