1

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

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

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

1 回答 1

3

是的,您可以使用 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);
于 2012-07-05T19:08:44.793 回答