Ask Your Question

How to convert Floating point image to 32-bit single-channel?

asked 2012-07-04 11:22:37 -0500

Tru gravatar image

updated 2012-07-05 01:46:14 -0500

Kirill Kornyakov gravatar image

I'm doing a watershed segmentation and the marker image is derived from the source image put through a distance transform. The distance transform returns a floating point image (I have no idea about the bit-depth) and I have trouble putting it through the watershed method since it requires a 32 bit single channel image.

Can I use the mat's convertTo method to set the bit depth to 32? I also have trouble trying to display the floating point image since the matToBitmap() method doesn't seem to accept them.

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

    ImageView imgView = (ImageView) findViewById(;
    Bitmap bmpIn = BitmapFactory.decodeResource(getResources(),

    Utils.bitmapToMat(bmpIn, mImg);

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

    //Marker image for watershed
    Imgproc.distanceTransform(mThresh, mDist, Imgproc.CV_DIST_L2, Imgproc.CV_DIST_MASK_PRECISE);

    //Conversions for watershed
    Imgproc.cvtColor(mThresh, mThresh, Imgproc.COLOR_GRAY2BGR, 3);

    //Floating-point image -> 32-bit single-channel

    Imgproc.watershed(mThresh, mDist); //

    Bitmap bmpOut = Bitmap.createBitmap(mThresh.cols(), mThresh.rows(),

    Utils.matToBitmap(mThresh, bmpOut);
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-07-04 14:14:19 -0500

Andrey Pavlenko gravatar image

Use Mat::convertTo():

Mat mDist32S = new Mat();
mDist.convertTo(mDist32S, CvType.CV_32S);

As for the matToBitmap() - it can convert 8UC1 and 8UC4 Mat-s only. So to display a float point image you need convertTo(CvType.CV_8U), then to Bitmap.

edit flag offensive delete link more

Question Tools


Asked: 2012-07-04 11:22:37 -0500

Seen: 8,681 times

Last updated: Jul 04 '12