自定义卷积核—梯度
OpenCV除了提供由库进行计算的kernel,还支持自定义kernel来进行自定义滤波。常见的包括模糊、锐化、梯度计算等方式。利用梯度可检测图片边缘变化较大的地方,在二值化和特征提取时有很大作用。自定义kernel的实现依赖于filter2D() 函数。下面是该函数的声明:
filter2D(src, dst, ddepth, kernel);
各参数解释如下:
-
src
表示此操作的源(输入图像)的Mat对象。 -
dst
表示此操作的目标(输出图像)的Mat对象。 -
ddepth
输出图像深度,值为-1则表示与输入图像一致。 -
kernel
自定义的卷积核。
Java代码(JavaFX Controller层)
public class Controller{
@FXML private Text fxText;
@FXML private ImageView imageView;
@FXML public void handleButtonEvent(ActionEvent actionEvent) throws IOException {
Node source = (Node) actionEvent.getSource();
Window theStage = source.getScene().getWindow();
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.png");
fileChooser.getExtensionFilters().add(extFilter);
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JPG Files(*.jpg)", "*.jpg"));
File file = fileChooser.showOpenDialog(theStage);
runInSubThread(file.getPath());
}
private void runInSubThread(String filePath){
new Thread(new Runnable() {
@Override
public void run() {
try {
WritableImage writableImage = filter2DOfGradient(filePath);
Platform.runLater(new Runnable() {
@Override
public void run() {
imageView.setImage(writableImage);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private WritableImage filter2DOfGradient(String filePath) throws IOException {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat src = Imgcodecs.imread(filePath);
Mat dst = new Mat();
// Self define kernel of gradient.
Mat k_x = new Mat(3,3,CvType.CV_32FC1);
Mat k_y = new Mat(3,3,CvType.CV_32FC1);
// Kernel of X direction gradient.
float[] robert_x = new float[]{-1, 0,
0,1};
k_x.put(0, 0, robert_x);
// Kernel of Y direction gradient.
float[] robert_y = new float[]{0, 1,
-1,0};
k_y.put(0, 0, robert_y);
Imgproc.filter2D(src, dst, -1, k_x);
Imgproc.filter2D(src, dst, -1, k_y);
MatOfByte matOfByte = new MatOfByte();
Imgcodecs.imencode(".jpg", dst, matOfByte);
byte[] bytes = matOfByte.toArray();
InputStream in = new ByteArrayInputStream(bytes);
BufferedImage bufImage = ImageIO.read(in);
WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
return writableImage;
}
}
运行图