在Mat中使用put(int,int,int)方法在java中使用OpenCv

时间:2015-09-26 13:45:04

标签: java opencv

谢谢你的帮助 我要在java中编写边缘检测代码,我从this链接获得prewitt边缘检测代码,当我将其粘贴到一个类时,程序运行完美。 但是当我将此代码粘贴到其他程序中的一个方法时,就会发生错误。

错误说: 在mat类型中放置(int,int,double [])的方法不适用于参数(int,int,int) 我变得困惑,为什么这个代码独立运行正确,但当我将其粘贴到其他程序的一个方法....

public BufferedImage edgePrewitt(File arg){
    BufferedImage out1 = null;
    try {

         int kernelSize = 9;
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );

         Mat source = Imgcodecs.imread(arg.getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
         Mat destination = new Mat(source.rows(),source.cols(),source.type());

         Mat kernel = new Mat(kernelSize,kernelSize, CvType.CV_8UC1);
         int[][] data=new int[][]{{-1,0,1},{-1,0,1},{-1,0,1}}; 
         for(int row=0;row<3;row++){
               for(int col=0;col<3;col++){
                   kernel.put(row   , col   , data[row][col]);
                   System.out.println(data[row][col]);
               }
            }

         Imgproc.filter2D(source, destination, -1, kernel);
         Imgcodecs.imwrite("output.jpg", destination);
         out1 = mat2Img(destination);
      } catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
      }
    return out1;
}

1 个答案:

答案 0 :(得分:0)

方法put(类org.opencv.core.Mat)的签名是:

put(int, int, double[])

put(int, int, byte[])

put(int, int, int[])

put(int, int, short[])

put(int, int, float[])

documentation

注意所有方法签名,第三个参数是数组。

尝试以下操作:

public BufferedImage edgePrewitt(File arg){
    BufferedImage out1 = null;
    try {

         int kernelSize = 9;
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );

         Mat source = Imgcodecs.imread(arg.getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
         Mat destination = new Mat(source.rows(),source.cols(),source.type());

         Mat kernel = new Mat(kernelSize,kernelSize, CvType.CV_8UC1);
         int[][] data=new int[][]{{-1,0,1},{-1,0,1},{-1,0,1}}; 
         for(int row=0;row<3;row++){
            kernel.put(row   , col   , data[row]);
         }

         Imgproc.filter2D(source, destination, -1, kernel);
         Imgcodecs.imwrite("output.jpg", destination);
         out1 = mat2Img(destination);
      } catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
      }
    return out1;
}