user7360712
user7360712

Reputation: 21

OpenCV 2.4 Bilateral filtering

In OpenCV android, is it possible to apply bilateral filtering? I know that I can do gaussian blurring like Imgproc.GaussianBlur(gray, gray, new Size(15,15), 0); but I cannot seem to find the code for bilateral filtering.

Upvotes: 2

Views: 3465

Answers (2)

martenq
martenq

Reputation: 13

The problem could be, that you use PNG image, which has a 4th channel for transparency. Convert it from 4 channel to 3 channel, before use.

Imgproc.cvtColor(src,dst,Imgproc.COLOR_BGRA2BGR);

Upvotes: 0

Andrii Omelchenko
Andrii Omelchenko

Reputation: 13343

Seems it's possible like:

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

from here and here.

Update

This:

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)

is because wrong color format of processing Mat. You should convert 4 channels RGBA format to 3 channels RGB for bilateralFilter() apply (like in bilateralFilterTutorial() method here). So, You code should be like that:

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

Upvotes: 4

Related Questions