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

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

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);
                System.out.println("ERROR : Image cannot be loaded..!!");
            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);

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

        void filter2DFreq(Mat inputImg, Mat outputImg,  Mat H){
            ArrayList<Mat>planes = new ArrayList<>();
            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(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();
            ArrayList<Mat>planes = new ArrayList<>();
            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) {
            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 ...
edit retag flag offensive close merge delete


yes but I have done same as the tutorial

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

Solve it? I guess,mat_float is the problem

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