OpenCV實(shí)現(xiàn)普通閾值
普通閾值
閾值本質(zhì)上就是對(duì)圖像進(jìn)行分割的一個(gè)過(guò)程。利用閾值二值化可對(duì)灰度或彩色圖像進(jìn)行像素?cái)?shù)據(jù)分類。普通閾值即閾值二值化就是針對(duì)給定的圖像,以T作為閾值進(jìn)行分割的過(guò)程。在OpenCV中該類的實(shí)現(xiàn)依賴于threshold() 函數(shù)。下面是該函數(shù)的聲明:
threshold(src, dst, thresh, maxval, type);
各參數(shù)解釋
·src
表示此操作的源(輸入圖像)的Mat對(duì)象。
·mat
表示目標(biāo)(輸出)圖像的類Mat的對(duì)象。
·thresh
表示閾值T。
·maxval
表示最大灰度值,一般為255。
·type
表示要使用的閾值類型的整數(shù)類型變量,閾值二值化為Imgproc.THRESH_BINARY。
其數(shù)學(xué)描述解釋如下:
對(duì)于給定的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 = 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; } }
運(yùn)行圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
對(duì)ThreadLocal內(nèi)存泄漏及弱引用的理解
這篇文章主要介紹了對(duì)ThreadLocal內(nèi)存泄漏及弱引用的理解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01MyBatis關(guān)于二級(jí)緩存問(wèn)題
本篇文章主要介紹了MyBatis關(guān)于二級(jí)緩存問(wèn)題,二級(jí)緩存是Mapper級(jí)別的緩存,多個(gè)sqlSession操作同一個(gè)Mapper,其二級(jí)緩存是可以共享的。2017-03-03Kotlin基礎(chǔ)教程之伴生對(duì)象,getter,setter,內(nèi)部,局部,匿名類,可變參數(shù)
這篇文章主要介紹了Kotlin基礎(chǔ)教程之伴生對(duì)象,getter,setter,內(nèi)部,局部,匿名類,可變參數(shù)的相關(guān)資料,需要的朋友可以參考下2017-05-05JAVA初級(jí)項(xiàng)目——實(shí)現(xiàn)圖書管理系統(tǒng)
這篇文章主要介紹了JAVA如何實(shí)現(xiàn)圖書管理系統(tǒng),文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06SpringMVC中參數(shù)綁定問(wèn)題實(shí)例詳解
springmvc是用來(lái)處理頁(yè)面的一些請(qǐng)求,然后將數(shù)據(jù)再通過(guò)視圖返回給用戶的,下面這篇文章主要給大家介紹了關(guān)于SpringMVC中參數(shù)綁定問(wèn)題的相關(guān)資料,需要的朋友可以參考下2022-04-04SpringBoot使用JDBC獲取相關(guān)的數(shù)據(jù)方法
這篇文章主要介紹了SpringBoot使用JDBC獲取相關(guān)的數(shù)據(jù)方法,JDBC與數(shù)據(jù)庫(kù)建立連接、發(fā)送 操作數(shù)據(jù)庫(kù)的語(yǔ)句并處理結(jié)果,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03