在Mat中的OpenCv中使用put(int,int,int)方法
问题描述:
感谢您的帮助 我将为java编写边缘检测代码,我从this链接中获得prewitt边缘检测代码,粘贴到一个类,程序运行完美。 但是当我将此代码粘贴到其他程序中的一种方法时,会发生错误。在Mat中的OpenCv中使用put(int,int,int)方法
错误说: 在类型垫放(INT,INT,双[])的方法并不适用于参数(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;
}
答
方法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[])
注意所有方法的签名,所述第三参数是一个数组。
尝试如下:
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;
}
谢谢,这个问题就解决了,但是又在for循环中没有定义“关口”参数:/ –