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

An example Analysis of the collision between Python OpenCV and Machine Learning

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

Share

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

This article mainly explains "an example analysis of the collision between Python OpenCV and machine learning". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "an example Analysis of the collision between Python OpenCV and Machine Learning".

0. Preface

Machine learning is a subset of artificial intelligence, which provides automatic prediction or decision-making ability for computers and other systems with computing power. Machine learning applications such as virtual assistants, license plate recognition systems, intelligent recommendation systems and other machine learning applications bring convenient experience to our daily life. The vigorous development of machine learning benefits from the following three key factors: 1) massive data sets; 2) the rapid development of algorithms; 3) the development of computer hardware. In this article, we will learn the common machine learning algorithms and techniques provided by OpenCV to solve practical problems in computer vision projects, such as classification and regression problems.

1. A brief introduction to Machine Learning

Machine learning is a process of using computer programming to learn from historical data to predict new data. Machine learning can be divided into three categories-supervised learning, unsupervised learning and semi-supervised learning. The algorithms involved in these techniques are shown in the following figure:

1.1 supervised learning

The samples used in supervised learning have corresponding expected output values (or sample labels). Because we know the correct label of each training data, supervised learning can correct these predictions according to the difference between the prediction and the corresponding expected output. Based on these calibrations, the algorithm can learn from the errors to adjust its internal parameters to fit the function between the nearest sample set and the corresponding expected output.

Supervised learning problems can be further classified into the following categories and regression:

Classification: when the output variable is a category, the problem can be considered as a classification problem. In the classification problem, the algorithm maps inputs to output tags.

Regression: when the output variable is real, in the regression problem, the algorithm maps the input to the continuous real output.

In supervised learning, the following issues need to be considered:

Deviation-variance trade-off (Bias-variance trade-off): models with underfitted data have high deviations, while models with overfitted data have high variances:

Deviation is the error caused by wrong assumptions in the learning algorithm, which can be defined as the difference between the prediction of the model and the expected correct value. The model with high deviation can not find all the patterns in the data (underfitting), so it does not fit the training set well, nor does it fit the test set well.

Variance is defined as the tendency of the algorithm to learn wrong things, which will fit the real signal and noise in the data at the same time. Therefore, the model with high variance (over-fitting) is very suitable for the training set, but it cannot be generalized to the test set because it learns the noise in the data.

Model complexity and training data volume: model complexity refers to the complexity attempted by machine learning algorithms. The complexity of the model is usually determined by the training data: for example, if a small amount of data is used to train the model, a low-complexity model is preferable because a high-complexity model can lead to overfitting.

Dimension of input space: learning can be very difficult when dealing with high-dimensional spatial data because there are many additional features that confuse the learning process, also known as dimensional disaster. Therefore, when dealing with high-dimensional spatial data, the common method is to modify the learning algorithm to make it have high deviation and low variance.

1.2 unsupervised learning

In unsupervised learning, the sample set lacks the output value corresponding to each sample (the sample set is not marked, classified or classified). The goal of unsupervised learning is to model and infer the structure or distribution in the sample set. Therefore, in unsupervised learning, the algorithm makes inference from the data and tries to reveal the hidden distribution information. Clustering and dimension reduction are the two most commonly used algorithms in unsupervised learning.

1.3 semi-supervised learning

Semi-supervised learning can be seen as a tradeoff between supervised learning and unsupervised learning because it uses both labeled and untagged data for training. Many real-world machine learning problems can be classified as semi-supervised because correctly tagging all data can be difficult or time-consuming, while untagged data is easier to collect.

2. K-means (K-Means) clustering

OpenCV provides the cv2.kmeans () function to implement the K-Means clustering algorithm, which finds the center of the cluster and groups the input samples around the cluster.

The goal of K-Means clustering algorithm is to divide n samples into K clusters, where each sample belongs to a cluster with the nearest mean. The usage of the cv2.kmeans () function is as follows:

Retval, bestLabels, centers=cv.kmeans (data, K, bestLabels, criteria, attempts, flags [, centers])

Data represents the input data for clustering, it is the np.float32 data type, each column contains a feature; K specifies the final number of clusters needed; the algorithm termination criteria are specified by the criteria parameter, which sets the maximum number of iterations or the required precision. When these criteria are met, the algorithm terminates. Criteria is a tuple with three parameters (type, max_item, epsilon):

Standard examples of criteria parameters are as follows:

Criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20,1.0)

The above statement indicates that the maximum number of iterations is set to 20 (max_iterm = 20) and the required precision is 1.0 (epsilon = 1.0).

The attempts parameter specifies the number of times the algorithm is executed with different initial tags. The flags parameter specifies the method to initialize the cluster center. The available values include: cv2.KMEANS_RANDOM_CENTERS randomly initializes the cluster center every time; cv2.KMEANS_PP_CENTERS initializes the cluster center using the Kmuri Meanswatch + center proposed by Arthur et al.

Cv2.kmeans () returns the following:

The return value interprets the bestLabels integer array, which is used to store the cluster index center of each sample, which contains the array compactness at the center of each cluster, the sum of the square of the distance from each point to the center of its cluster and the 2.1K-Means clustering example.

As an example, we will use the K-Means clustering algorithm to cluster a set of 2D points. This set of 2D points consists of 240 points and is described using two features:

# 2D data data = np.float32 ((np.random.randint (0,50, (80,2)), np.random.randint (40,90, (80,2)), np.random.randint (70,110,( 80,2) # Visualization plt.scatter (data [:, 0], data [:, 1]) plt.show ()

As shown in the figure above, the data will be used as input to the clustering algorithm, and each data point has two features corresponding to (x, y) coordinates, for example, these coordinates can represent the height and weight of 240 people, while the K-Means clustering algorithm is used to determine the size of clothes (for example, the corresponding size is S, M or L).

Next, we divide the data into two clusters. The first step is to define the termination criteria of the algorithm, setting the maximum number of iterations to 20 (max_iterm = 20) and epsilon to 1.0 (epsilon = 1.0):

Criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20,1.0)

Then call the cv2.kmeans () function to apply the K-Means algorithm:

Ret, label, center = cv2.kmeans (data, 2, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

Because the return value label stores the clustering index of each sample, we can split the data into different clusters according to label:

A = data [label.ravel () = = 0] B = data [label.ravel () = = 1]

Finally, draw An and B and the data before and after clustering, in order to better understand the clustering process:

Fig = plt.figure (figsize= (12,6) plt.suptitle ("K-means clustering algorithm", fontsize=14,fontweight='bold') # drawing raw data ax = plt.subplot (1,2,1) plt.scatter (data [:, 0], data [:, 1], centering plt.title ("data") # drawing clustered data and cluster center ax = plt.subplot (1,2,2) plt.scatter (A [:, 0], A [:, 1] ) plt.scatter (B [:, 0], B [:, 1]) plt.scatter (center [:, 0], center [:, 1], plt.title ("clustered data and centroids (K = 2)") plt.show ()

Next, we modify the parameter K to cluster and visualize accordingly. For example, if you need to divide the data into three clusters, first apply the same process to cluster the data. You only need to modify the parameters (Know3) to divide the data into three clusters:

Ret, label, center = cv2.kmeans (data, 3, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

Then, when you use tags to output detached data, the data is divided into three groups:

A = data [label.ravel () = = 0] B = data [label.ravel () = = 1] C = data [label.ravel () = = 2]

The final step is to display A, B, and C, as well as cluster center and training data:

Fig = plt.figure (figsize= (12,6) plt.suptitle ("K-means clustering algorithm", fontsize=14,fontweight='bold') # drawing raw data ax = plt.subplot (1,2,1) plt.scatter (data [:, 0], data [:, 1], centering plt.title ("data") # drawing clustered data and cluster center ax = plt.subplot (1,2,2) plt.scatter (A [:, 0], A [:, 1] Cymb') plt.scatter (B [:, 0], B [:, 1]) plt.scatter (C [:, 0], C [:, 1], cymbalr') plt.scatter (center [:, 0], center [:, 1], plt.title ("clustered data and centroids (K = 3)") plt.show ()

We can also set the number of clusters to 4 to observe the results of the algorithm:

Ret, label, center = cv2.kmeans (data, 4, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

3. K nearest neighbor

K-nearest neighbor (k-nearest neighbours, kNN) is one of the simplest algorithms in supervised learning. KNN can be used for classification and regression problems. In the training phase, kNN stores the feature vectors and category tags of all training samples. In the testing phase, the untagged vectors are classified into the most frequent class tags among the nearest k training samples, where k is a user-defined constant:

As shown in the figure above, if k = 5, the green circle (the unmarked test sample) is classified as a triangle because there are 3 triangles but only 1 diamond in the nearest 5 samples, and if k = 9, the green circle is classified as a diamond because there are 5 diamonds but only 4 triangles in the nearest 9 samples.

In OpenCV, to use the kNN classifier, you first need to create the kNN classifier using cv2.ml.KNearest_create (), and then provide data and tags to train the kNN classifier using the train () method. Finally, use the findNearest () method to find the neighbor of the test sample, using the following:

Retval, results, neighborResponses, dist=cv2.ml_KNearest.findNearest (samples, k [, results [, neighborResponses [, dist])

Where samples is the input sample, k is set to the number of nearest neighbors, results stores the predicted value of each input sample, neighborResponses stores the corresponding neighbor, and dist stores the distance between the input sample and the corresponding neighbor.

3.1 K nearest neighbor exampl

Next, to demonstrate the kNN algorithm, first randomly create a set of points and assign a label (0 or 1). Label 0 will represent a red triangle and label 1 will represent a blue square; then, use the kNN algorithm to classify sample points based on k nearest neighbors.

The first step is to create a set of points with appropriate labels and sample points to classify:

# Point set consists of 50 points data = np.random.randint (0,100, (50,2)) .astype (np.float32) # create a label for 1 (0: red, 1: blue) labels = np.random.randint (0,2, (50,1)) .astype (np.float32) # create sample points to be classified sample = np.random.randint (0,100, (1,2)) .astype (np.float32)

Next, create the kNN classifier, train the classifier, and find the k nearest neighbors to classify the sample points:

# create kNN classifier knn = cv2.ml.KNearest_create () # train kNN classifier knn.train (data, cv2.ml.ROW_SAMPLE, labels) # find k nearest neighbors k = 3ret, results, neighbours, dist = knn.findNearest (sample) K) # print result print ("result: {}" .format (results)) print ("neighbours: {}" .format (neighbours)) print ("distance: {}" .format (dist)) # Visualization fig = plt.figure (figsize= (8,6)) red_triangles = data [labels.ravel () = 0] plt.scatter (red_triangles [:, 0], red_triangles [:, 1], 200,'r' '^') blue_squares = data [labels.ravel () = = 1] plt.scatter (blue_squares [:, 0], blue_squares [:, 1], 200,200, 'baked,' s') plt.scatter (sample [:, 0], sample [:, 1], 200,200, 'gathers,' o') plt.show ()

The results are as follows:

Result: [[0.]]

Neighbours: [[0. 0. 1.]]

Distance: [[13. 40. 65.]]

Therefore, the green dot is classified as a red triangle, and the visualization is as follows:

4. Support vector machine

Support vector machine (Support Vector Machine, SVM) is a supervised learning technology, which constructs one or a group of hyperplanes in high-dimensional space by best separating the training data according to the specified class.

Taking the two-dimensional plane as an example, you can see in the following figure that the green line is the best hyperplane that can separate the two classes because its distance to the nearest element in the two classes is the largest:

In the first case of the figure above, the decision boundary is a line, while in the second case, the decision boundary is a circular curve, and the dotted line represents other decision boundaries, but they are not the decision boundaries that best separate the two classes.

The SVM implementation in OpenCV is based on LIBSVM, creates an empty model using the cv2.ml.SVM_create () function, and then assigns the main parameters to the model:

SvmType: set the SVM type. Available values are as follows:

SVM_C_SVC:C CC- support vector classification, which can be used for n-classification (n-≥ 2) problems

NU_SVC: v vv- support vector classification

ONE_CLASS: distribution estimation (single-class SVM)

EPS_SVR: "\ epsilon"-support vector regression

NU_SVR: v vv- support vector regression

KernelType: this sets the core type of SVM. Available values are as follows:

LINEAR: linear kernel

POLY: polynomial kernel

RBF: Radial Basis Function (RBF), which is a good choice in most cases

SIGMOID: Sigmoid core

CHI2: exponential Chi2 kernel, similar to RBF kernel

INTER: histogram intersection kernel; faster kernel

Degree: the degree parameter of the kernel function (for POLY kernel)

Gamma: the γ\ gamma γ parameter of the kernel function (for POLY/RBF/SIGMOID/CHI2 kernels)

Coef0: the coef0 parameter of the kernel function (for POLY/SIGMOID kernel)

Cvalue: C parameter of the SVM optimization problem (for C_SVC/EPS_SVR/NU_SVR type)

Nu: v vv parameter for SVM optimization problem (for NU_SVC/ONE_CLASS/NU_SVR type)

P: the "\ epsilon" parameter of the SVM optimization problem (for EPS_SVR type)

ClassWeights: optional weights in C_SVC questions, assigned to specific classes

TermCrit: termination criteria of iterative SVM training process

The choice of kernel function usually depends on the data set, and the RBF kernel can usually be used for testing first, because the kernel maps the sample nonlinearly to a higher dimensional space, which can easily deal with the situation that the relationship between class tags and attributes is nonlinear.

The default constructor initializes SVM with the following values:

SvmType: C_SVC, kernelType: RBF, degree: 0, gamma: 1, coef0: 0, C: 1, nu: 0, p: 0, classWeights: 0, termCrit: TermCriteria (MAX_ITER+EPS, 1000, FLT_EPSILON) 4.1 support vector machine example

To understand how to use SVM in OpenCV, you first need to create training data and tags:

Labels = np.array ([1,1,1,1,1]) data = np.matrix ([[800,40], [850,400], [500,10], [550,300], [450,600]], dtype=np.float32)

The above code creates 5 points, the first 2 points are designated as Class 1, and the other 3 points are designated as Class-1. Next, initialize the SVM model with the svm_init () function:

Def svm_init (gamma=0.50625 12.5): "create the SVM model and assign its main parameters Return model "" model = cv2.ml.SVM_create () model.setGamma (gamma) model.setC (C) model.setKernel (cv2.ml.SVM_LINEAR) model.setType (cv2.ml.SVM_C_SVC) model.setTermCriteria ((cv2.TERM_CRITERIA_MAX_ITER, 100,100, 1e-6)) return model# initialize SVM model svm_model = svm_init (cv2.TERM_CRITERIA_MAX_ITER 12.5, gamma=0.50625)

The created SVM core type is set to LINEAR,SVM and the type is set to C_SVC.

Then, write the svm_train () function to train the SVM model:

Def svm_train (model, samples, responses): # use samples and responses training model model.train (samples, cv2.ml.ROW_SAMPLE, responses) return model# to train SVMsvm_train (svm_model, data, labels)

Then create an image and draw the SVM response:

Def show_svm_response (model, image): colors = {1: (255,255,0),-1: (0,255,255)} for i in range (image.shape [0]): for j in range (image.shape [1]): sample = np.matrix ([[j, I]], dtype=np.float32) response = svm_predict (model, sample) image [I J] = colors [response.item (0)] cv2.circle (image, (800,40), 10, (255,0,0),-1) cv2.circle (image, (850,400), 10, (255,0,0),-1) cv2.circle (image, (500,10), 10, (0255,0),-1) cv2.circle (image, (550,300), 10, (0,255,0) -1) cv2.circle (image, (450,600), 10, (0,255,0),-1) support_vectors = model.getUncompressedSupportVectors () for i in range (support_vectors.shape [0]): cv2.circle (image, (support_vectors [I, 0], support_vectors [I, 1]), 15, (0,0,255), 6) # create an image img_output = np.zeros ((640,0,3) Dtype= "uint8") # display SVM response show_svm_response (svm_model, img_output)

As shown in the figure above, SVM uses training data for training and can be used to classify all the points in the image. SVM divides the image into yellow and cyan regions, and you can see that the boundary between the two regions corresponds to the best interval between the two classes, because the distance to the nearest element in the two classes is the largest, and the support vector is displayed with a red border.

At this point, I believe you have a deeper understanding of the "example analysis of the collision between Python OpenCV and machine learning". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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: 239

*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