Error while implementing the "out of focus deblur tutorial" in java

asked 2020-01-02 08:06:43 -0600

Gourav gravatar image

I was implementing this tutorial in and java I wrote this code
```

    import org.opencv.core.*;  
    import org.opencv.highgui.HighGui;  
    import org.opencv.imgcodecs.Imgcodecs;  
    import org.opencv.imgproc.Imgproc;  
    import java.util.ArrayList;  

    class DeblurFilter{
        public void run(String[] args){
            String strInFileName = "../data/original.jpg";
            int snr = 5200;
            int R = 53;
            Mat imgIn = new Mat();
            imgIn = Imgcodecs.imread(strInFileName);
            if(imgIn.empty()){
                System.out.println("ERROR : Image cannot be loaded..!!");
                System.exit(-1);
            }
            Mat imgOut = new Mat();
            Rect roi = new Rect(0,0,imgIn.cols()&-2,imgIn.rows()&-2);

            Mat Hw = new Mat(), h = new Mat();
            calcPSF(h, roi.size(), R);
            calcWnrFilter(h, Hw, 1.0/snr);
            filter2DFreq(new Mat(imgIn, roi), imgOut, Hw);
            imgOut.convertTo(imgOut, CvType.CV_8U);
            Core.normalize(imgOut, imgOut, 0, 255,Core.NORM_MINMAX);
            HighGui.imshow("Original",imgIn);
            HighGui.imshow("Deblurred",imgOut);

        }
        void calcPSF(Mat outputImg, Size filterSize, int R){
            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(0, 0, 255), -1, 8);
            Scalar summa = Core.sumElems(h);
            Core.divide(h, summa, outputImg);
        }

        void fftshift( Mat inputImg, Mat outputImg){
            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);
        }

        void filter2DFreq(Mat inputImg, Mat outputImg,  Mat H){
            ArrayList<Mat>planes = 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);

            ArrayList<Mat>planesH = new ArrayList<>(2);
            planesH.add(H.clone());
            planesH.add(Mat.zeros(H.size(), CvType.CV_32F));
            Mat complexH =new Mat(), complexIH = new Mat();
            Core.mulSpectrums(complexI, complexH, complexIH, 0);

            Core.idft(complexIH, complexIH);
            Core.split(complexIH, planes);
            outputImg = planes.get(0);
        }

        void calcWnrFilter( Mat input_h_PSF, Mat output_G, double nsr){
            Mat h_PSF_shifted = new Mat();
            fftshift(input_h_PSF,h_PSF_shifted);
            ArrayList<Mat>planes = new ArrayList<>();
            planes.add(h_PSF_shifted.clone());
            planes.add( Mat.zeros(h_PSF_shifted.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(planes.get(0),2, denom);
            Core.add(denom, new Scalar(nsr), denom);
            Core.divide(planes.get(0), denom, output_G);
        }

    }

    public class OutOfFocusDeblurFilter {
        public static void main(String[] args) {
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
            new DeblurFilter().run(args);
        }
    }

and got the following error
```

C:\Program Files\Java\jdk-13.0.1\bin\java.exe" -Djava.library.path=D:\opencv\build\java\x64 "-javaagent:D:\IntelliJ IDEA Community Edition 2019.3.1\lib\idea_rt.jar=60673:D:\IntelliJ IDEA Community Edition 2019.3.1\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\goura\IdeaProjects\first_project\out\production\first_project;D:\opencv\build ...
(more)
edit retag flag offensive close merge delete

Comments

yes but I have done same as the tutorial

Gourav gravatar imageGourav ( 2020-01-02 08:43:56 -0600 )edit

Solve it? I guess,mat_float is the problem

forestjj gravatar imageforestjj ( 2020-06-29 03:48:53 -0600 )edit