简体   繁体   中英

Distance transform in OpenCV output error

I'm using Android binaries and this is the first time I'm performing a distance transform in OpenCV. The OpenCV specification says that the output image of the distanceTransform is a 32-bit floating-point, single-channel image. I get this into a mat (mDist), but on creating a bitmap out of the mat it throws an IllegalStateException. Is this due to incompatibility of the output and the mat object? Do I have to specify the color channel details for the mat or anything? The following is my code portion.

在此处输入图片说明

        Mat mImg = new Mat();
        Mat mThresh = new Mat();
        Mat mDist = new Mat();

        ImageView imgView = (ImageView) findViewById(R.id.imageView);
        Bitmap bmpIn = BitmapFactory.decodeResource(getResources(),
                R.drawable.w1);    
        Utils.bitmapToMat(bmpIn, mImg); //Load image to mat

        Imgproc.cvtColor(mImg, mImg, Imgproc.COLOR_BGR2GRAY);    
        Imgproc.threshold(mImg, mThresh, 0, 255, Imgproc.THRESH_BINARY
                | Imgproc.THRESH_OTSU); //Grayscale and thresholding        

        Imgproc.distanceTransform(mThresh, mDist, Imgproc.CV_DIST_L2, Imgproc.CV_DIST_MASK_PRECISE);

        Bitmap bmpOut = Bitmap.createBitmap(mDist.cols(), mDist.rows(),
                Bitmap.Config.ARGB_8888);   

        Utils.matToBitmap(mDist, bmpOut); //Error in creating bitmap
        imgView.setImageBitmap(bmpOut);

The fault was in the following line of code;

Bitmap bmpOut = Bitmap.createBitmap(mDist.cols(), mDist.rows(),
                Bitmap.Config.ARGB_8888); 

The Bitmap.Config that should be applied was not ARGB_8888. The function creates an 8-bit single channel mat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM