Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the General threshold in OpenCV

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "OpenCV how to achieve the general threshold", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "OpenCV how to achieve the general threshold" bar!

General threshold

Threshold is essentially a process of image segmentation. Threshold binarization can be used to classify pixel data of grayscale or color images. The ordinary threshold, that is, threshold binarization, is the process of segmentation with T as the threshold for a given image. The implementation of this class in OpenCV depends on the threshold () function. The following is the declaration of the function:

Threshold (src, dst, thresh, maxval, type)

Interpretation of each parameter

Src

A Mat object that represents the source of this operation (the input image).

Mat

An object like Mat that represents the target (output) image.

Thresh

Represents the threshold T.

Maxval

Represents the maximum grayscale value, usually 255.

Type

An integer type variable that represents the type of threshold to use, and the threshold is binarized to Imgproc.THRESH_BINARY.

The mathematical description is explained as follows:

For a given src (xQuery y), if its pixel value is greater than the threshold T (thresh), it returns the maximum pixel value, otherwise it is 0.

Then the pixel description of dst is as follows:

Java code (JavaFX Controller layer)

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;}}

Operation diagram

Thank you for your reading, the above is the content of "how to achieve the general threshold of OpenCV". After the study of this article, I believe you have a deeper understanding of how to achieve the general threshold of OpenCV, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report