java.lang.UnsupportedOperationException:Mat数据类型不兼容:4

时间:2014-04-17 02:03:24

标签: java opencv image-processing

我已经编写了自己的算法来确定边缘然后找到角点。 但后来我在转换图像方面遇到了麻烦。我已经编写了一些代码来实现结果但是我得到了上面指定的错误。 任何人都可以帮我取悦。 提前谢谢!

    Mat mat = new Mat(w, h, CvType.CV_32S);

    MatOfPoint2f finalc = new MatOfPoint2f();

    MatOfPoint2f ma = new MatOfPoint2f();

    mat.put(0, 0, array);

    Mat quad = new Mat(w,h,CvType.CV_32S);

    quad.put(0,0,w,0,w,h,0,w);

ma.put((int)a[0][1],(int)a[0][0],(int)a[1][1],(int)a[1][0],(int)a[2][1],(int)a[2][0],(int)a[3][1],(int)a[3][0]);

    Imgproc.approxPolyDP(ma,finalc,2.0,true);

    Mat transmtx = Imgproc.getPerspectiveTransform(finalc, quad);

    Imgproc.warpPerspective(mat, quad, transmtx, quad.size());

    MatOfByte matOfByte = new MatOfByte();

    Highgui.imencode(".jpg", quad, matOfByte); 

    byte[] byteArray = matOfByte.toArray();

    //InputStream in = new ByteArrayInputStream(byteArray);

    //BufferedImage bufferedImage = ImageIO.read(in);

    File f = new File("retrieve1.jpg");

    BufferedImage img1 = new BufferedImage(w, h, 12);

    WritableRaster raster = (WritableRaster)img1.getData();

    raster.setDataElements(0,0,w,h,byteArray);

    img1.setData(raster);

    try
    {

        ImageIO.write(img1,"jpg",f);

    }
    catch(Exception e)
    {}
}

我基本上要做的就是将这个(C ++)代码的一部分移植到java。 https://github.com/bsdnoobz/opencv-code/blob/master/quad-segmentation.cpp

1 个答案:

答案 0 :(得分:0)

我创建的解决方案:

    Mat mat = new Mat(h,w, CvType.CV_8U);

    mat.put(0, 0, array);

    //Imshow is = new Imshow("try");for verification

    MatOfPoint2f quad = new MatOfPoint2f(p);//array of corner points

    MatOfPoint2f rect = new MatOfPoint2f(farray);//final array of corner points into which mat should be warped into

    Mat transmtx = Imgproc.getPerspectiveTransform(quad,rect);

    Mat output = new Mat(w,h,CvType.CV_8U); 

    Imgproc.warpPerspective(mat, output, transmtx, new Size(w,h),Imgproc.INTER_CUBIC);

    //is.showImage(output);     

    MatOfByte matOfByte = new MatOfByte();

    Highgui.imencode(".jpg", output, matOfByte); 

    byte[] byteArray = matOfByte.toArray();

    File f = new File("retrieve1.jpg");

    BufferedImage img1 =null;

    InputStream in = new ByteArrayInputStream(byteArray);

    img1  = ImageIO.read(in);

    WritableRaster raster = (WritableRaster)img1.getData();

    raster.setDataElements(0,0,byteArray);

    img1.setData(raster);

由于我不需要验证角点,因此我删除了approxpolydp并将所有内容放入点中,而不是直接将它们输入matofpoint2fs。和"阵列"是与使用中的图像等效的字节。