栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

OpenCV.自适应阈值.Adaptive

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

OpenCV.自适应阈值.Adaptive

自适应阈值.Adaptive

对于自适应阈值除了OTSU与TRIANGLE外,还有使用图像自适应阈值的方法,该方法具体有C均值与高斯C均值两种。其在OpenCV中实现依赖于adaptiveThreshold() 函数,下面是其声明:

adaptiveThreshold(gray, dst, maxValue, adaptiveMethod, thresholdType, blockSize, C);

各参数解释如下:

  • src
    表示此操作的源(输入图像)的Mat对象。

  • dst
    表示此操作的目标(输出图像)的Mat对象。

  • maxValue
    最大灰度值,通常为255。

  • adaptiveMethod
    自适应方法,通常为C均值或高斯C均值。

  • thresholdType
    阈值方法,通常为THRESH_BINARY

  • blockSize
    分块大小,奇数。

  • C
    常数,阈值化的时候使用计算得到的+C之后作分割。

Java代码(JavaFX Controller层)

public class Controller{

    @FXML private Text fxText;
    @FXML private ImageView imageView;
    @FXML private Label resultLabel;

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

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

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    
    private WritableImage thresholdOfAdaptive(String filePath) throws IOException {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

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

        // Construct an empty mat instance to change src into gray image.
        Mat gray = new Mat();
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
        Imgproc.adaptiveThreshold(gray, dst, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 15, 10);

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

}

运行图

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/588475.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号