OpenCV實現(xiàn)反閾值二值化
反閾值二值化
反閾值二值化與閾值二值化互為逆操作。在OpenCV中該類的實現(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;
}
}
運行圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaSE學(xué)習(xí)之內(nèi)部類及常用API
這篇文章主要介紹了JavaSE中的內(nèi)部類和幾個常用的API,文中的示例代碼介紹詳細,對我們學(xué)習(xí)JavaSEI有一定的幫助,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12
關(guān)于Idea中的.properties文件顯示問題
這篇文章主要介紹了關(guān)于Idea中的.properties文件顯示問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
springboot自動配置原理以及spring.factories文件的作用詳解
這篇文章主要介紹了springboot自動配置原理以及spring.factories文件的作用詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringBoot實現(xiàn)application.yml文件敏感信息加密
本文主要介紹了SpringBoot實現(xiàn)application.yml文件敏感信息加密,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

