OpenCV.自定义卷积核.锐化

自定义卷积核—模糊

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 = filter2DOfSharpening(filePath);

                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            imageView.setImage(writableImage);
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private WritableImage filter2DOfSharpening(String filePath) throws IOException {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat src = Imgcodecs.imread(filePath);
        Mat dst = new Mat();

        // Self define kernel of size 3x3.
        Mat kernel = new Mat(3,3, CvType.CV_32FC1);
        float[] data = new float[]{
                0,-1,0,
                -1,5,0,
                0,-1,0
        };
        kernel.put(0,0,data);

        Imgproc.filter2D(src, dst, -1, kernel);

        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;
    }

}

OpenCV.自定义卷积核.锐化_第1张图片
图1 - 自定义3X3锐化卷积核

OpenCV.自定义卷积核.锐化_第2张图片

图2 - 自定义3X3强化锐化算子八零域卷积核

运行图
OpenCV.自定义卷积核.锐化_第3张图片

图 3 - 使用自定义的3X3锐化卷积核

你可能感兴趣的:(OpenCV,JavaFX,opencv,人工智能,计算机视觉)