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 use OpenCV to realize threshold operation in C++

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail how to use OpenCV to achieve threshold operation in C++. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

What is the threshold?

The simplest method of image segmentation.

Application example: use the threshold to segment the part of the object we need from an image (of course, the object here can be a part or a whole). This image segmentation method is based on the gray difference between the object and the background in the image, and this segmentation belongs to pixel-level segmentation.

In order to extract the part we need from an image, the gray value of each pixel in the image should be compared with the selected threshold, and the corresponding judgment should be made. (note: the selection of the threshold depends on the specific problem. That is, objects may have different grayscale values in different images.

Once we have found the pixels of the object to be segmented, we can set some specific values for these pixels to represent them. (for example, the grayscale value of the pixel of the object can be set to:'0' (black), and the grayscale value of other pixels is: '255' (white); of course, the grayscale value of the pixel can be arbitrary, but it is best to set a strong contrast between the two colors to facilitate the observation of the results.

Type of thresholding:

The threshold (threshold) function is provided in OpenCV: threshold.

There are five types of thresholding for this function, which will be described in detail in the following sections.

To explain the process of threshold segmentation, let's look at a simple picture about the grayscale of pixels, as shown below. The blue horizontal line in the figure represents a specific threshold.

Binary thresholding

The type of thresholding is shown in the following formula:

Explanation: when using this threshold type, you must first select a specific threshold value, such as 125, so that the new threshold generation rule can be interpreted as setting the gray value of pixels greater than 125 to the maximum value (for example, the maximum gray value of 8 bits is 255). The gray value of pixels with a gray value less than 125 is set to 0.

Anti-binary thresholding

The threshold type is shown in the following formula:

Explanation: this thresholding is similar to binary thresholding, in which a specific grayscale value is selected as the threshold, but the last set value is the opposite. (in 8-bit grayscale images, for example, greater than the threshold is set to 0 and less than the threshold is set to 255).

Truncation threshold

The type of thresholding is shown in the following formula:

Explanation: first of all, a threshold needs to be selected, and the pixels in the image that are larger than the threshold are set to the threshold, and those smaller than the threshold remain unchanged. (for example, if the threshold is 125, the threshold that is less than 125 remains unchanged, and the pixel with a grayscale value greater than 125 (230) is set as the threshold).

Threshold to 0

The threshold type is shown in the following formula:

Explanation: first select a threshold, and then deal with the image as follows: (1) the gray value of the pixel is greater than the threshold without any change; (2) if the gray value of the pixel is less than the threshold, the gray value of the pixel becomes 0.

The inverse threshold is changed to 0

The threshold type is shown in the following formula:

Explanation: the principle is similar to the 0 threshold, but on the contrary when processing the image, that is, the gray value of the pixel is less than the threshold without any change, while the part greater than the threshold, the gray value all becomes 0.

Code demonstration

Create a new project opencv-0014, configure properties (VS2017 configure OpenCV common properties), and then write # include and main methods in the source file. This time, we directly add a TrackBar to the second picture to display it directly, and then do the operation, in which we define a value of thresthold_value as the initial value of TrackBar, and then write an event that defines a TrackbarEvent (int,void*) to associate with Trackbar. This time, the picture we loaded is a picture of geometric shape.

The display effect is

Binarization threshold

The basic step is to change the picture to a single-head image, that is, a grayscale image, if it is a color three-channel image binarization will be a problem, and then the gray image binarization processing.

Let's write about the TrackBarEvent incident.

Running effect

The respective effects when the modified threshold becomes smaller and changes.

As we said earlier, there are five types of binarization thresholds, so we add a TrackBar to the image to change the effect of different types of display.

Add a definition of a type to the above

Then add a TrackBar, and the corresponding event is my TrackBarEvent () event

Then modify the TrackBarEvent event

On C++ on how to use OpenCV to achieve threshold operation to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report