Ask Your Question
0

how to write Out-of-focus Deblur Filter with java

asked 2020-06-22 03:02:09 -0600

forestjj gravatar image

updated 2020-06-22 03:16:17 -0600

berak gravatar image

I am working on a java project where I have to use Out-of-focus Deblur Filter . This is what I have done so far :

 private Mat calcPSF(Mat outputImg, Size filterSize, int R) {
        Mat dst = new Mat();
        Mat h = new Mat(filterSize, CvType.CV_32F, new Scalar(0));
        Point point = new Point(filterSize.width / 2, filterSize.height / 2);
        Imgproc.circle(h, point, R, new Scalar(255), -1, 8);
        Scalar summa = Core.sumElems(h);

        Core.divide(h, summa, dst);

        return dst;
    }
private Mat calcWnrFilter(Mat inputhPSF, double nsr) {
        Mat outputG = new Mat();
        Mat hPSFshifted = fftshift(inputhPSF);
        //Mat_<float>(hPSFshifted.clone()), Mat::zeros(h_PSF_shifted.size(), CV_32F)
        List<Mat> planes = new ArrayList<>();
        planes.add(hPSFshifted.clone());
        planes.add(Mat.zeros(hPSFshifted.size(), CvType.CV_32F));
        Mat complexI = new Mat();
        Core.merge(planes, complexI);
        Core.dft(complexI, complexI);
        Core.split(complexI, planes);
        Mat denom = new Mat();
        Core.pow(new Mat(Math.abs(planes.get(0).nativeObj)), 2, denom);
        long nativeObj = denom.nativeObj;
        nativeObj += nsr;
        Core.divide(planes.get(0), new Mat(nativeObj), outputG);

        return outputG;
    }

    private Mat getFloat(Mat mat) {
        Mat mat1 = new Mat();
        mat.convertTo(mat1, CvType.CV_32FC1);

        return mat1;
    }
   private Mat filter2DFreq(Mat inputImg, Mat H) {

        Mat outputImg = new Mat();
        List<Mat> planes = new ArrayList<>();
        List<Mat> planesH = new ArrayList<>();

        planes.add(inputImg.clone());
        planes.add(Mat.zeros(inputImg.size(), CvType.CV_32F));
        Mat complexI = new Mat();
        Core.merge(planes, complexI);
        Core.dft(complexI, complexI, Core.DFT_SCALE);
        planesH.add(H.clone());
        planesH.add(Mat.zeros(H.size(), CvType.CV_32F));
        Mat complexH = new Mat();
        Core.merge(planesH, complexH);
        Mat complexIH = new Mat();
        Core.mulSpectrums(complexI, complexH, complexIH, 0);
        Core.idft(complexIH, complexIH);
        Core.split(complexIH, planes);
        outputImg = planes.get(0);

        return outputImg;
    }
    private Mat fftshift(Mat inputImg) {
        Mat outputImg = new Mat();
        outputImg = inputImg.clone();
        int cx = outputImg.cols() / 2;
        int cy = outputImg.rows() / 2;
        Mat q0 = new Mat(outputImg, new Rect(0, 0, cx, cy));
        Mat q1 = new Mat(outputImg, new Rect(cx, 0, cx, cy));
        Mat q2 = new Mat(outputImg, new Rect(0, cy, cx, cy));
        Mat q3 = new Mat(outputImg, new Rect(cx, cy, cx, cy));
        Mat tmp = new Mat();
        q0.copyTo(tmp);
        q3.copyTo(q0);
        tmp.copyTo(q3);
        q1.copyTo(tmp);
        q2.copyTo(q1);
        tmp.copyTo(q2);

        return outputImg;
    }
 Mat imgOut = new Mat();

        Rect roi = new Rect(0, 0, src.cols() & -2, src.rows() & -2);

        Mat Hw = new Mat();
        Mat h = new Mat();
        h = calcPSF(h, roi.size(), 53);
        Hw = calcWnrFilter(h, 1.0 / 5200);

        imgOut = filter2DFreq(src.submat(roi), Hw);

        imgOut.convertTo(imgOut, CV_8U);
        Core.normalize(imgOut, imgOut, 0, 255, NORM_MINMAX);

        HighGui.imshow("deblur_result", imgOut);
        HighGui.waitKey();
edit retag flag offensive close merge delete

Comments

and the problem is ?

berak gravatar imageberak ( 2020-06-22 03:16:55 -0600 )edit

merge.dispatch.cpp:129: error: (-215:Assertion failed) mv[i].size == mv[0].size && mv[i].depth() == depth in function 'cv::merge

forestjj gravatar imageforestjj ( 2020-06-22 20:57:41 -0600 )edit

so either inputImg or H is not float (i guess, your src img is the problem)

berak gravatar imageberak ( 2020-06-23 02:12:15 -0600 )edit

thankyou,I'll try again

forestjj gravatar imageforestjj ( 2020-06-27 20:17:47 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2020-06-30 03:38:44 -0600

forestjj gravatar image
  Mat mat = Imgcodecs.imread("image",Imgcodecs.IMREAD_GRAYSCALE);
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-06-22 03:02:09 -0600

Seen: 368 times

Last updated: Jun 30 '20