Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

While implementing deblur tutorial in java I got the following error

Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: OpenCV(4.2.0) C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\core\src\merge.dispatch.cpp:129: error: (-215:Assertion failed) mv[i].size == mv[0].size && mv[i].depth() == depth in function 'cv::merge'
]
    at org.opencv.core.Core.merge_0(Native Method)
    at org.opencv.core.Core.merge(Core.java:4045)
    at DeblurFilter.calcWnrFilter(OutOfFocusDeblurFilter.java:95)
    at DeblurFilter.run(OutOfFocusDeblurFilter.java:24)
    at OutOfFocusDeblurFilter.main(OutOfFocusDeblurFilter.java:111)

This is the code that I have written

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

class DeblurFilter{
    public void run(String[] args){
        String strInFileName = "../data/lena.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(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){

        List<Mat> planes = new ArrayList<Mat>();
        inputImg.convertTo(inputImg, CvType.CV_32F);
        planes.add(inputImg);
        planes.add(Mat.zeros(inputImg.size(), CvType.CV_32F));
        Mat complexI = new Mat();
        Core.merge(planes, complexI);
        Core.dft(complexI, complexI, Core.DFT_SCALE);
       // System.out.println(planes.size());

        List<Mat>planesH = new ArrayList<Mat>();
        H.convertTo(H, CvType.CV_32F);
        planesH.add(H);
        planesH.add(Mat.zeros(H.size(), CvType.CV_32F));
        Mat complexH = new Mat(), complexIH = new Mat();
        Core.merge(planesH, complexH);
        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);
        //System.out.println(h_PSF_shifted.size().height);
        ArrayList<Mat>planes = new ArrayList<Mat>();
        h_PSF_shifted.convertTo(h_PSF_shifted, CvType.CV_32F);
        planes.add(h_PSF_shifted);
        planes.add( Mat.zeros(h_PSF_shifted.size(), CvType.CV_32F));
        Mat complexI = new Mat();
        complexI.convertTo(complexI, CvType.CV_32F);
        Core.merge(planes, complexI);
        Core.dft(complexI, complexI);
        Core.split(complexI, planes);
        Mat denom = new Mat();
        Mat p0mat = planes.get(0);
        Core.absdiff(p0mat, Mat.zeros(p0mat.size(), CvType.CV_32F), p0mat);
        Core.pow(p0mat,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);
    }
}

It shows error in Core.merge(planes, complexI); of calcWnrFilter method of DeblurFilter class