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 segment an image

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use openCV to segment images". In daily operation, I believe many people have doubts about how to use openCV to segment images. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to use openCV to segment images". Next, please follow the editor to study!

This experiment shares the specific implementation code of openCV image segmentation for your reference, the specific contents are as follows

one。 Experimental purpose

Further understand the principle of image threshold segmentation and edge detection.

Master the principle of image basic global threshold method and maximum inter-class variance method (otsu method) and realize it by programming.

The edge detection of image is realized by programming.

two。 Experimental contents and requirements

Image threshold segmentation (basic global threshold method and otsu method) and edge detection are realized by programming.

three。 Main instruments, equipment and materials of the experiment

Computer, VS2017+OpenCV

four。 Principle and method of experiment

The basic principle of Image threshold Segmentation

One of the main contents of image binarization processing in image segmentation is to set the gray level of the points on the image to 0 or 255, that is, the whole image shows an obvious black-and-white effect. If I is used to represent the original graph and R is used to represent the binarized graph, the binarization process can be expressed by the following formula:

Thr represents the selected threshold. The process of binarization is that when the pixel gray value of the original image is greater than the threshold value, it will be whitened, otherwise it will be blackened. That is, the grayscale image of 256 luminance levels is changed into two grayscale levels by selecting the appropriate threshold, so that only two grayscale images play a very important role in the process of image processing and analysis. especially in practical image processing.

According to the use of a unified threshold for the whole picture or different thresholds for different regions, it can be divided into global threshold method (global thresholding) and local threshold method (local thresholding, also known as adaptive threshold method adaptive thresholding); this kind of threshold related to coordinates is also called dynamic threshold, for specific methods, you can refer to the relevant image processing books.

1. The basic global threshold method, that is, all pixels in the whole image have the same threshold thr. The specific steps are as follows:

(1) Select an initial estimate T

(2) use T to segment the image. This results in two sets of pixels: G1 consists of all pixels with grayscale values greater than T, and G2 consists of all pixels with grayscale values less than or equal to T.

(3) the average gray values U1 and U2 are calculated for all pixels in G1 and G2.

(4) calculate the new threshold: t = (U1 + U2) / 2.

(5) repeat steps (2) to (4) until the difference of the obtained T value is less than a pre-defined parameter T0.

2. The algorithm steps of Otsu method are as follows:

(1) calculate the normalized histogram of the image first.

(2) I represents the threshold of classification, that is, a grayscale, iterating from 0.

(3) through the normalized histogram, the ratio of 0 pixel (background pixel) to the whole image is calculated, and the average gray level (U0) of background pixel is counted; the ratio W1 of the pixel (foreground pixel) to the whole image is calculated, and the average gray level (U1) of foreground pixel is calculated.

(4) calculate the variance of foreground pixels and background pixels g = w0w1 (u0-u1) (u0-u1)

(5) I finish the iteration until I is 256

(6) the corresponding I value of the maximum g is taken as the global threshold of the image.

Edge detection

Edge detection in an image can be realized with the help of first-order and second-order differential. The common first-order edge detection operators include Roberts operator, Prewitt operator and Sobel operator. The second-order operator is mainly Laplacian operator. Because of the great influence of noise, the image is usually smoothed before use. LOG operator is to smooth the image first, and then Laplace transform is used to find the zero crossing point. Canny operator is the optimal edge detection operator.

five。 Content of the experiment

1. Threshold segmentation of image:

The image is the license plate image, write code to achieve the basic global threshold method and Otsu method, and compare the segmentation results.

2. Edge detection

The edge detection operator can be used to process the license plate image, which can be processed by gradient operator, Laplacian operator or Canny operator (Canny operator can directly use OpenCV function). Compare whether there is a difference between threshold segmentation followed by edge detection and direct edge detection of the image.

Note: grayscale edges can be extracted here.

Code:

# include "pch.h" # include # include using namespace std;using namespace cv;// Laplace sharpening function void LaplacianSharpDeal (const Mat & src, Mat & dst) {if (! src.data) return; for (int I = 0; I

< src.rows; ++i) for (int j = 0; j < src.cols; ++j) { float a; if (i >

1 & & I

< src.rows - 1 && j >

1 & & j

< src.cols - 1) { a = 5 * (float)src.at(i, j) - (float)src.at(i - 1, j) - (float)src.at(i, j - 1) - (float)src.at(i, j + 1) - (float)src.at(i + 1, j); } else {//边缘赋值 a = src.at(i, j); } if (a >

255 | | a

< 0) { dst.at(i, j) = src.at(i, j); } else { dst.at(i, j) = a; } }}// 基本全局阈值方法函数int BasicGlobalThreshold(Mat src, float oldValue){ int cols = src.cols; int rows = src.rows; float G1 = 0; float G2 = 0; float g1 = 0; float g2 = 0; float u1 = 0; float u2 = 0; float T0 = 0; // 计算灰度直方图分布,统计像素数和频率 for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (src.at(i, j) >

OldValue) {G1 + = src.at (I, j); G1 + = 1;} else {G2 + = src.at (I, j); G2 + = 1;}} U1 = G1 / G1; U2 = G2 / G2; T0 = (U1 + U2) / 2; std::cout

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