在OpenCV android中,可以应用双边过滤吗?我知道我可以像Imgproc.GaussianBlur(gray, gray, new Size(15,15), 0);一样进行高斯模糊处理,但似乎找不到用于双边过滤的代码。

最佳答案

似乎有可能像:

Imgproc.bilateralFilter(mat, dstMat, 10, 50, 0);

来自herehere

更新

这个:
E/AndroidRuntime: FATAL EXCEPTION: Thread-1376 Process: PID: 30368 CvException [org.opencv.core.CvException: cv::Exception: /Volumes/build-storage/build/2_4_pack-android/opencv/modules‌​/imgproc/src/smooth.‌​cpp:1925: error: (-215) (src.type() == CV_8UC1 || src.type() == CV_8UC3) && src.type() == dst.type() && src.size() == dst.size() && src.data != dst.data in function void cv::bilateralFilter_8u(const cv::Mat&, cv::Mat&, int, double, double, int)

是因为处理Mat的颜色格式错误。您应该将4通道RGBA格式转换为3通道RGB才能应用bilateralFilter()(就像bilateralFilterTutorial()方法here一样)。因此,您的代码应如下所示:
// load Mat from somewhere (e.g. from assets)
mSourceImageMat = Utils.loadResource(this, R.drawable.<your_image>);
// convert 4 channel Mat to 3 channel Mat
Imgproc.cvtColor(mSourceImageMat, mSourceImageMat, Imgproc.COLOR_BGRA2BGR);

// create dest Mat
Mat dstMat = mSourceImageMat.clone();

// apply bilateral filter
Imgproc.bilateralFilter(mSourceImageMat, dstMat, 10, 250, 50);

// convert to 4 channels Mat back
Imgproc.cvtColor(dstMat, dstMat, Imgproc.COLOR_RGB2RGBA);

// create result bitmap and convert Mat to it
Bitmap bm = Bitmap.createBitmap(mSourceImageMat.cols(), mSourceImageMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(dstMat, bm);

// show bitmap on ImageView
mImageView.setImageBitmap(bm);

09-10 14:17