Ask Your Question
0

java.lang.UnsupportedOperationException: Mat data type is not compatible: 6[SOLVED]

asked 2020-01-02 21:28:10 -0600

Gourav gravatar image

updated 2020-01-03 07:05:22 -0600

supra56 gravatar image

I was implementing the tutorial of anisotropic image segmentation in java and wrote the following code:

```
    import org.opencv.core.*;
    import org.opencv.highgui.HighGui;
    import org.opencv.imgcodecs.Imgcodecs;
    import org.opencv.imgproc.Imgproc;

    class AnisotropicImageSegmentationrun{
        public void run(){
           int W = 52;
           double C_Thr = 0.43;
           int LowThr = 35;
           int HighThr = 57;

           Mat imgIn = new Mat();
           imgIn = Imgcodecs.imread("C:\\Users\\goura\\IdeaProjects\\first_project\\data\\gst_input.jpg",Imgcodecs.IMREAD_GRAYSCALE);
           if(imgIn.empty()){
               System.out.println("ERROR : Image cannot be loaded..!!");
               System.exit(-1);
           }
           Mat imgCoherency = new Mat(),imgOrientation = new Mat();
           calcGst(imgIn, imgCoherency, imgOrientation, W);

           Mat imgCoherencyBin = new Mat();
           Core.compare(imgCoherency, new Scalar(C_Thr), imgCoherencyBin, Core.CMP_GT);

           Mat imgOrientationBin = new Mat();
           Core.inRange(imgOrientation, new Scalar(LowThr), new Scalar(HighThr), imgOrientationBin );

           Mat imgBin = new Mat();
           Core.bitwise_and(imgCoherencyBin, imgOrientationBin, imgBin);

           Core.normalize(imgCoherency, imgCoherency, 0, 255, Core.NORM_MINMAX);
           Core.normalize(imgOrientation, imgOrientation, 0, 255, Core.NORM_MINMAX);
           Core.add(imgIn, imgBin,imgIn);
           Core.multiply(imgIn, new Scalar(0.5), imgIn);

           HighGui.imshow("result", imgIn);
           HighGui.imshow("Coherency", imgCoherency);
           HighGui.imshow("Orientation", imgOrientation);

           HighGui.waitKey(0);
           System.exit(0);
        }

        void calcGst(Mat inputImg, Mat imgCoherencyOut, Mat imgOrientationOut, int w){
            Mat img = new Mat();
            inputImg.convertTo(img, CvType.CV_64F);

            // GST components calculation (start)
            // J =  (J11 J12; J12 J22) - GST
            Mat imgDiffX = new Mat(), imgDiffY = new Mat(), imgDiffXY = new Mat();
            Imgproc.Sobel(img, imgDiffX, CvType.CV_64F, 1, 0, 3);
            Imgproc.Sobel(img, imgDiffY, CvType.CV_64F, 0, 1, 3);
            Core.multiply(imgDiffX, imgDiffY, imgDiffXY);

            Mat imgDiffXX = new Mat(), imgDiffYY = new Mat();
            Core.multiply(imgDiffX, imgDiffX, imgDiffXX);
            Core.multiply(imgDiffY, imgDiffY, imgDiffYY);

            // J11, J22 and J12 are GST components
            Mat J11 = new Mat(), J22 = new Mat(), J12 = new Mat();
            Imgproc.boxFilter(imgDiffXX, J11, CvType.CV_64F, new Size(w, w));
            Imgproc.boxFilter(imgDiffYY, J22, CvType.CV_64F, new Size(w, w));
            Imgproc.boxFilter(imgDiffXY, J12, CvType.CV_64F, new Size(w, w));
            // GST components calculation (stop)

            // eigenvalue calculation (start)
            // lambda1 = J11 + J22 + sqrt((J11-J22)^2 + 4*J12^2)
            // lambda2 = J11 + J22 - sqrt((J11-J22)^2 + 4*J12^2)

            Mat tmp1 = new Mat(), tmp2 = new Mat(), tmp3 = new Mat(), tmp4 = new Mat();
            Core.add(J11, J22, tmp1);
            Core.subtract(J11, J22, tmp2);
            Core.multiply(tmp2, tmp2, tmp2);
            Core.multiply(J12, J12, tmp3);

            Mat tmp3Mul4 = new Mat() , tmp2AddTmp3Mul4 = new Mat();

            //4.0*tmp3
            Core.multiply(tmp3, new Scalar(4.0), tmp3Mul4);
            //tmp2 + 4.0*tmp3
            Core.add(tmp3Mul4, tmp2, tmp2AddTmp3Mul4);
            Core.sqrt(tmp2AddTmp3Mul4, tmp4);

            Mat lambda1 = new Mat(), lambda2 = new Mat();
            Core.add(tmp1, tmp4, lambda1);// biggest eigenvalue
            Core.subtract(tmp1, tmp4, lambda2);// smallest eigenvalue
            // eigenvalue calculation (stop)

            // Coherency calculation (start)
            // Coherency = (lambda1 - lambda2)/(lambda1 + lambda2)) - measure of anisotropism
            // Coherency is anisotropy degree (consistency of local orientation)

            Mat lambda1SubLambda2 = new Mat(), lambda1AddLambda2 = new Mat();
            Core.subtract(lambda1, lambda2 ,lambda1SubLambda2);
            Core.add(lambda1, lambda2, lambda1AddLambda2);

            Core.divide(lambda1SubLambda2, lambda1AddLambda2, imgCoherencyOut);
            //System.out.println(imgCoherencyOut.dump());
            // Coherency calculation (stop)

            // orientation angle calculation (start)
            // tan(2*Alpha) = 2*J12/(J22 - J11)
            // Alpha = 0.5 atan2(2*J12/(J22 - J11))

            Mat J22_J11 = new Mat(), J12Mul2 = new Mat();
            Core.subtract(J22, J11, J22_J11);
            Core.multiply ...
(more)
edit retag flag offensive close merge delete

Comments

1

@berak I have added the lines where the error has occurred

Gourav gravatar imageGourav ( 2020-01-03 03:10:17 -0600 )edit

^^ yep, thanks, helpful ! ;)

berak gravatar imageberak ( 2020-01-03 03:13:56 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-01-03 03:04:34 -0600

berak gravatar image

updated 2020-01-03 03:12:14 -0600

unlike its c++ counterpart, HighGui.imshow() cannot process CV_64F (== type 6) images.

you need to convert to uchar before that:

Mat draw = new Mat();
imgCoherency.convertTo(draw, CV_8U, 255); // maybe w/o scale factor ?
HighGui.imshow("Coherency", draw);
edit flag offensive delete link more

Comments

How to change the type then

Gourav gravatar imageGourav ( 2020-01-03 03:12:50 -0600 )edit
1

Yeah that worked Thanks

Gourav gravatar imageGourav ( 2020-01-03 06:32:40 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-01-02 21:28:10 -0600

Seen: 3,989 times

Last updated: Jan 03 '20