【问题标题】:How to convert floating point image to 32bit single channel in OpenCv?如何在 OpenCv 中将浮点图像转换为 32 位单通道?
【发布时间】:2012-07-04 16:31:23
【问题描述】:

我正在进行分水岭分割,标记图像来自经过距离变换的源图像。距离变换返回一个浮点图像(我不知道位深度),我无法将其通过分水岭方法,因为它需要 32 位单通道图像。

我可以使用 mat 的 convertTo 方法将位深度设置为 32 吗? 我也很难显示浮点图像,因为 matToBitmap() 方法似乎不接受它们。 (在 Android 中)

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);

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
mDist.convertTo(...);

Imgproc.watershed(mThresh, mDist); //

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


Utils.matToBitmap(mThresh, bmpOut);
imgView.setImageBitmap(bmpOut);

【问题讨论】:

  • OpenCV 中 32 位、单通道、浮点图像的类型代码是 CV_32FC1。转换为该类型有效吗?

标签: android image-processing opencv computer-vision


【解决方案1】:

是的,您可以使用 convertTo 函数将任何 opencv 矩阵转换为另一种类型。要转换的类型应设置在具有相同大小的目标矩阵中。 convertTo 具有可选参数 scale 和 shift,因此您可以在转换为定点深度时避免裁剪和量化错误。所以对于你的代码:

Mat mDist32 = Mat(mDist.rows,mDist.cols,CV_32SC1); // 32 bit signed 1 channel, use CV_32UC1 for unsigned
mDist.convertTo(mDist32,CV_32SC1,1,0);
Imgproc.watershed(mThresh,mDist32);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    相关资源
    最近更新 更多