OpenCV實(shí)現(xiàn)反閾值二值化
反閾值二值化
反閾值二值化與閾值二值化互為逆操作。在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_INV。
其數(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 = 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)行圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaSE學(xué)習(xí)之內(nèi)部類及常用API
這篇文章主要介紹了JavaSE中的內(nèi)部類和幾個(gè)常用的API,文中的示例代碼介紹詳細(xì),對(duì)我們學(xué)習(xí)JavaSEI有一定的幫助,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12
關(guān)于Idea中的.properties文件顯示問(wèn)題
這篇文章主要介紹了關(guān)于Idea中的.properties文件顯示問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
springboot自動(dòng)配置原理以及spring.factories文件的作用詳解
這篇文章主要介紹了springboot自動(dòng)配置原理以及spring.factories文件的作用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringBoot實(shí)現(xiàn)application.yml文件敏感信息加密
本文主要介紹了SpringBoot實(shí)現(xiàn)application.yml文件敏感信息加密,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Java中重寫(xiě)和重載的區(qū)別及說(shuō)明
Java語(yǔ)言中的重載和重寫(xiě)是實(shí)現(xiàn)多態(tài)的兩種方式,但他們的實(shí)現(xiàn)方式和規(guī)則有所不同,重載發(fā)生在一個(gè)類中,同名的方法如果有不同的參數(shù)列表,則視為重載,重寫(xiě)則發(fā)生在子類和父類之間,要求子類重寫(xiě)方法和父類被重寫(xiě)方法有相同的返回類型2024-10-10
Java實(shí)現(xiàn)文件名倒序排序的技術(shù)指南
在實(shí)際開(kāi)發(fā)過(guò)程中,我們經(jīng)常需要對(duì)文件進(jìn)行操作和處理,一個(gè)常見(jiàn)的需求是按文件名倒序排列文件列表,以便于文件的管理和查找,本文將介紹如何在Java中實(shí)現(xiàn)文件名倒序排序,并提供詳細(xì)的代碼案例,需要的朋友可以參考下2024-08-08

