Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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();
click to hide/show revision 2
None

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

berak gravatar image

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

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

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

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