如何在OpenCV中对图像应用逼真模糊?

时间:2018-09-23 08:20:41

标签: java android opencv gaussianblur

我是openCV的初学者,所以如果我的问题很愚蠢,请原谅。基本上,我正在尝试在openCv中应用gussianBlur,但是它显示了一个奇怪的错误,我不明白为什么?

代码:

Mat tmp = new Mat (bitmap.getWidth(), bitmap.getHeight(), CvType.CV_8UC4);
Utils.bitmapToMat(bitmap, tmp);
//Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2HSV_FULL);
Imgproc.GaussianBlur(tmp,tmp,new org.opencv.core.Size(2,2),0,0);
Utils.matToBitmap(tmp,bitmap);
imgView.setImageBitmap(null);
imgView.setImageBitmap(bitmap);

错误:

CvException [org.opencv.core.CvException: cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/imgproc/src/smooth.cpp:816: error: (-215) ksize.width > 0 && ksize.width % 2 == 1 && ksize.height > 0 && ksize.height % 2 == 1 in function cv::Ptr<cv::FilterEngine> cv::createGaussianFilter(int, cv::Size, double, double, int)]
    at org.opencv.imgproc.Imgproc.GaussianBlur_1(Native Method)
    at org.opencv.imgproc.Imgproc.GaussianBlur(Imgproc.java:533)
    at opengl.community.myopencvexample.MainActivity$3.onClick(MainActivity.java:82)
    at android.view.View.performClick(View.java:4478)
    at android.view.View$PerformClick.run(View.java:18698)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:149)
    at android.app.ActivityThread.main(ActivityThread.java:5257)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

方法GaussianBlurhttps://docs.opencv.org/java/2.4.9/org/opencv/imgproc/Imgproc.html#GaussianBlur(org.opencv.core.Mat,%20org.opencv.core.Mat,%20org.opencv.core.Size,%20double,%20double)

的文档
  

公共静态无效高斯模糊(Mat src,                   Mat dst,                   大小ksize,                   双sigmaX                   双西格玛)

     

ksize-高斯内核大小。 ksize.width和ksize.height可以不同,但​​都必须为正数,且奇数。或者,它们可以为零,然后根据sigma *计算得出。

您传递的内核大小为2、2,这并不奇怪。

使用类似这样的内容:

Imgproc.GaussianBlur(tmp, tmp, new Size(3, 3), 0, 0);