温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

OpenCV如何实现普通阈值

发布时间:2021-11-17 14:26:57 来源:亿速云 阅读:114 作者:iii 栏目:开发技术

这篇文章主要讲解了“OpenCV如何实现普通阈值”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“OpenCV如何实现普通阈值”吧!

普通阈值

阈值本质上就是对图像进行分割的一个过程。利用阈值二值化可对灰度或彩色图像进行像素数据分类。普通阈值即阈值二值化就是针对给定的图像,以T作为阈值进行分割的过程。在OpenCV中该类的实现依赖于threshold() 函数。下面是该函数的声明:

threshold(src, dst, thresh, maxval, type);

各参数解释

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

·mat
表示目标(输出)图像的类Mat的对象。

·thresh
表示阈值T。

·maxval
表示最大灰度值,一般为255。

·type
表示要使用的阈值类型的整数类型变量,阈值二值化为Imgproc.THRESH_BINARY。

其数学描述解释如下:

对于给定的src(x,y),若其像素值大于阈值T(thresh),则其返回像素最大值,否则为0。

OpenCV如何实现普通阈值

那么dst其像素描述如下:

OpenCV如何实现普通阈值

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

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

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

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

        Imgproc.threshold(src, dst, 150, 255, Imgproc.THRESH_BINARY);

        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如何实现普通阈值

感谢各位的阅读,以上就是“OpenCV如何实现普通阈值”的内容了,经过本文的学习后,相信大家对OpenCV如何实现普通阈值这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI