OpenCV實(shí)現(xiàn)反閾值二值化
反閾值二值化
反閾值二值化與閾值二值化互為逆操作。在OpenCV中該類的實(shí)現(xiàn)依賴于threshold() 函數(shù)。下面是該函數(shù)的聲明:
threshold(src, dst, thresh, maxval, type);
各參數(shù)解釋
·src
表示此操作的源(輸入圖像)的Mat對象。
·mat
表示目標(biāo)(輸出)圖像的類Mat的對象。
·thresh
表示閾值T。
·maxval
表示最大灰度值,一般為255。
·type
表示要使用的閾值類型的整數(shù)類型變量,反閾值二值化為Imgproc.THRESH_BINARY_INV。
其數(shù)學(xué)描述解釋如下:
對于給定的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; } }
運(yùn)行圖
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring中利用IOC實(shí)現(xiàn)注入的方式
Spring IOC(控制反轉(zhuǎn))實(shí)現(xiàn)依賴注入,將對象創(chuàng)建和依賴關(guān)系的管理交由Spring容器處理,通過注解或XML配置,實(shí)現(xiàn)對象之間的松耦合,提高代碼復(fù)用性和可維護(hù)性2023-04-04springboot注解Aspect實(shí)現(xiàn)方案
本文提供一種自定義注解,來實(shí)現(xiàn)業(yè)務(wù)審批操作的DEMO,不包含審批流程的配置功能。對springboot注解Aspect實(shí)現(xiàn)方案感興趣的朋友一起看看吧2022-01-01SpringCloud中使用Sentinel實(shí)現(xiàn)限流的實(shí)戰(zhàn)
限流在很多地方都可以使用的到,本篇博客將介紹如何使用SpringCloud中使用Sentinel實(shí)現(xiàn)限流,從而達(dá)到服務(wù)降級的目的,感興趣的可以了解一下2022-01-01Spring BeanFactory和FactoryBean有哪些區(qū)別
這篇文章主要介紹了Spring BeanFactory 與 FactoryBean 的區(qū)別詳情,BeanFactory 和 FactoryBean 的區(qū)別卻是一個很重要的知識點(diǎn),在本文中將結(jié)合源碼進(jìn)行分析講解,需要的小伙伴可以參考一下2023-02-02一文搞明白Java?Spring?Boot分布式事務(wù)解決方案
這篇文章主要介紹了一文搞明白Java?Spring?Boot分布式事務(wù)解決方案,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07Java開發(fā)學(xué)習(xí)之Bean的作用域和生命周期詳解
這篇文章主要介紹了淺談Spring中Bean的作用域,生命周期和注解,從創(chuàng)建到消亡的完整過程,例如人從出生到死亡的整個過程就是一個生命周期。本文將通過示例為大家詳細(xì)講講,感興趣的可以學(xué)習(xí)一下2022-06-06Java異常(Exception)處理以及常見異??偨Y(jié)
在《Java編程思想》中這樣定義異常,阻止當(dāng)前方法或作用域繼續(xù)執(zhí)行的問題,雖然java中有異常處理機(jī)制,但是要明確一點(diǎn),決不應(yīng)該用"正常"的態(tài)度來看待異常,這篇文章主要給大家介紹了關(guān)于Java異常(Exception)處理以及常見異常的相關(guān)資料,需要的朋友可以參考下2021-10-10dom4j創(chuàng)建和解析xml文檔的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猟om4j創(chuàng)建和解析xml文檔的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06