OpenCV實現(xiàn)反閾值二值化
反閾值二值化
反閾值二值化與閾值二值化互為逆操作。在OpenCV中該類的實現(xiàn)依賴于threshold() 函數(shù)。下面是該函數(shù)的聲明:
threshold(src, dst, thresh, maxval, type);
各參數(shù)解釋
·src
表示此操作的源(輸入圖像)的Mat對象。
·mat
表示目標(輸出)圖像的類Mat的對象。
·thresh
表示閾值T。
·maxval
表示最大灰度值,一般為255。
·type
表示要使用的閾值類型的整數(shù)類型變量,反閾值二值化為Imgproc.THRESH_BINARY_INV。
其數(shù)學描述解釋如下:
對于給定的src(x,y),若其像素值大于閾值T(thresh),則其返回0,否則為為像素最大值。
那么dst其像素描述如下:
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 = thresholdOfNonBinary(filePath); Platform.runLater(new Runnable() { @Override public void run() { imageView.setImage(writableImage); } }); } catch (IOException e) { e.printStackTrace(); } } }).start(); } private WritableImage thresholdOfNonBinary(String filePath) throws IOException { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat src = Imgcodecs.imread(filePath); Mat dst = new Mat(); Imgproc.threshold(src, dst, 130, 255, Imgproc.THRESH_BINARY_INV); 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; } }
運行圖
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringCloud中使用Sentinel實現(xiàn)限流的實戰(zhàn)
限流在很多地方都可以使用的到,本篇博客將介紹如何使用SpringCloud中使用Sentinel實現(xiàn)限流,從而達到服務降級的目的,感興趣的可以了解一下2022-01-01Spring BeanFactory和FactoryBean有哪些區(qū)別
這篇文章主要介紹了Spring BeanFactory 與 FactoryBean 的區(qū)別詳情,BeanFactory 和 FactoryBean 的區(qū)別卻是一個很重要的知識點,在本文中將結合源碼進行分析講解,需要的小伙伴可以參考一下2023-02-02一文搞明白Java?Spring?Boot分布式事務解決方案
這篇文章主要介紹了一文搞明白Java?Spring?Boot分布式事務解決方案,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07dom4j創(chuàng)建和解析xml文檔的實現(xiàn)方法
下面小編就為大家?guī)硪黄猟om4j創(chuàng)建和解析xml文檔的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06