Image color segmentation method, system and device based on k-means algorithm and storage medium

The k value and the initial cluster center point are determined by the elbow method, and the K-means algorithm is improved by combining the Euclidean distance and the relative number within the cluster method, which solves the problem of difficult parameter determination and local optimal solutions, and improves the image color. Segmentation accuracy and efficiency.

CN118570460AActive Publication Date: 2024-08-30NANJING UNIV OF INFORMATION SCI & TECH
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202410259823.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-03-07
Publication Date
2024-08-30
Estimated Expiration
2044-03-07

AI Technical Summary

Technical Problem

The K-means algorithm has difficulties in determining the size of the parameter k in image color segmentation, which results in too many clusters or inconspicuous clustering, and the clustering quality depends on the initial selection, and it is easy to fall into a local optimal solution.

Method used

The elbow-based method is used to determine the number of clustering center points k, and the initial clustering center point is selected by calculating the relative count within the cluster. During the iteration process, the clustering center point is adjusted by the method of Euclidean distance and relative number within the cluster to reduce number of iterations to improve clustering accuracy.

Benefits of technology

The improved algorithm reduces the number of iterations, improves the accuracy of clustering and the ability to finely divide images, especially in areas with similar colors, which can finely divide different segments.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118570460A
    Figure CN118570460A
Patent Text Reader

Abstract

The invention discloses an image color segmentation method, system and device based on a k-means algorithm, and a storage medium, and the method comprises the steps: introducing a new thought of total sum of squares SSE calculation in the aspect of the selection of an initial k value in obtaining image data, forming a sample data set, and improving the algorithm, and carrying out the assistance of a certain index parameter, and calculating a proper k value; on the initialization of the centroid, a calculation method of relative numbers in a cluster is provided, and selection of outlier abnormal points is avoided as much as possible; according to the method, problems related to k value selection, clustering center point initialization and an iteration process in a K-means algorithm are mainly analyzed; the method aims at enhancing selection of initial cluster values, improving random selection of initial center points in an original algorithm, and avoiding negative effects on final clustering results when outliers are selected as the initial center points. In addition, related indexes are optimized in the clustering iteration process, so that the clustering result is improved; in areas with similar colors, different fragments can still be finely divided, and the picture feature extraction effect is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention discloses a k-means algorithm-based image color segmentation method, system, device and storage medium, and relates to the field of computer cluster analysis. Background Art

[0002] Image segmentation is often used to divide an image into multiple regions with similar properties or specific labels, so that the pixels in each region have similar features or meet certain labeling conditions. In some regions with similar colors, the K-means algorithm is often used to assist image segmentation, but in some regions with similar colors, different fragments cannot be finely divided; because the K-means algorithm is simple to understand and efficient, it has been widely used in practical applications. The algorithm mainly faces two problems: 1. The determination of the size of the parameter k has a great influence on the clustering effect. If k is set too large, it may lead to too many clusters, and if k is set too small, the clustering may not be obvious. 2. The quality of clustering depends largely on the initial selection of the cluster center. During the algorithm iteration process, it is possible to fall into a local optimal solution. Summary of the invention

[0003] In view of the defects in the above-mentioned background technology, the present invention provides an image color segmentation method, system, device and storage medium based on the k-means algorithm. The improved algorithm reduces the number of iterations, improves the accuracy of clustering, and enhances the fine division of images.

[0004] To achieve the above object, the technical solution adopted by the present invention is as follows: an image color segmentation method based on a k-means algorithm comprises the following steps:

[0005] Obtain image data to form a sample data set, and determine the number of cluster center points k of the sample data set based on the elbow method;

[0006] Calculate the relative counts within the cluster of all data points in the sample data set and select the first initial cluster center point;

[0007] Select the remaining cluster centers according to the distance range between the data point and the first initial cluster center point until k initial cluster centers are selected;

[0008] Repeat the preset loop steps until the cluster center point no longer changes or reaches a specified number of iterations, wherein the preset loop includes:

[0009] Calculate the Euclidean distance between the remaining data points in the sample data and the existing cluster center points;

[0010] According to the size of the Euclidean distance, the data points are assigned to the nearest cluster center point and the data is divided into k clusters;

[0011] Calculate the distance between the data points in each cluster and the cluster center, and arrange them in ascending order. Take the value of the one-third digit as the next cluster center. The accurate selection of the next cluster center can improve the clustering effect of the algorithm and reduce the number of iterations.

[0012] Furthermore, the acquisition of image data to form a sample data set includes the following steps: converting a color array consisting of three primary color values ​​of each pixel of the image into a data point of the sample data in a three-dimensional space.

[0013] Furthermore, the method of determining the number k of cluster center points of sample data based on the elbow method includes the following steps:

[0014] Divide the sample data into k clusters, and calculate the intra-cluster square error and SSE values ​​corresponding to the k value.

[0015]

[0016] Among them, SSE i represents the sum of squared errors within the i-th cluster, c i represents the i-th cluster center, and p represents the cluster center of c i is the data point in the cluster of the cluster center, k represents the number of clusters or the number of cluster center points in the data set. Based on the clustering characteristics of the data set, the number of cluster center points k ranges from 1 to 10;

[0017] Assign different weights according to the degree of aggregation of each cluster, and calculate the corresponding total square error (TSS);

[0018]

[0019] Then draw a line graph based on different k values ​​and corresponding TSS values;

[0020] Calculate the rate of change c of each point using the k value and the corresponding TSS value;

[0021]

[0022] Where: x[i] represents the TSS value corresponding to k=i;

[0023] Set the indicator function log 10 c 2 And set the threshold,

[0024] Indicator function:

[0025] y=log 10 c 2

[0026] When the function value is lower than the threshold, the SSE curve reaches the elbow point, that is, the corresponding k value represents the actual number of clusters.

[0027] Furthermore, the relative counts within the cluster of all data points in the sample data set are calculated, and the selection of the first initial cluster center point specifically includes the following steps:

[0028] Calculate the Euclidean distance between all data points and other data points, and then sort the resulting Euclidean distance array in ascending order;

[0029] Determine the boundary distance based on the significant change of the sorted Euclidean distance array;

[0030] The number of data points within the boundary distance range is the relative count within the cluster;

[0031] Data points with large counts and small boundary distances are selected as the first initial cluster center points.

[0032] Furthermore, the method of selecting the remaining cluster center points according to the distance range between the data point and the first initial cluster center point until k initial cluster center points are selected specifically includes the following steps:

[0033] Calculate the distance between all data points in the sample data set and the existing cluster center point, take the distance D to the nearest cluster center point as the reference value, select the data point with the largest D value as the candidate center point, and if the data point is not an outlier, it can be used as the next initial center point;

[0034] The D value is calculated as:

[0035]

[0036] Where: Each data point format is:

[0037] x i =(x i1 ,x i2 ,…,x im )

[0038] m is the data dimension, x i represents the i-th data point, j ranges from 1 to num, num is the number of existing cluster centers, c j Represents the jth existing cluster center.

[0039] Furthermore, the outlier point is a point in the sample space that is inconsistent with the general behavior or characteristics of other sample points.

[0040] Furthermore, taking the data point corresponding to the one-third digit as the new cluster center point specifically includes the following steps:

[0041] In each cluster [i], the Euclidean distance between each data point and other data points in the same cluster is calculated to obtain a distance set, denoted as dis_InCluster[], and then this distance set is

[0042] dis_InCluster[] is sorted in ascending order to obtain the distance set in ascending order, recorded as

[0043] dis_InCluster_Sorted[], select the data point corresponding to the one-third digit value in dis_InCluster_Sorted[] as the new center point.

[0044] An image color segmentation system based on k-means algorithm, comprising:

[0045] Acquisition module: used to acquire image data, form a sample data set, and determine the number of cluster center points k of the sample data set based on the elbow method;

[0046] Preliminary selection module: used to calculate the relative counts within the cluster of all data points in the sample data set and select the first initial cluster center point;

[0047] Depth selection module: select the remaining cluster centers according to the distance range between the data point and the first initial cluster center, until k initial cluster centers are selected;

[0048] Loop module: used to repeat the preset loop steps until the cluster center point no longer changes or reaches the specified number of iterations. The preset loop includes:

[0049] Calculate the Euclidean distance between the remaining data points in the sample data and the existing cluster center points;

[0050] According to the size of the Euclidean distance, the data points are assigned to the nearest cluster center point and the data is divided into k clusters;

[0051] Calculate the distance between the data points in each cluster and the cluster center, arrange them in ascending order, and take the value of the third digit as the next cluster center.

[0052] A k-means algorithm-based image color segmentation device, comprising: a processor and a storage medium;

[0053] The storage medium is used to store instructions;

[0054] The processor is used to operate according to the instructions to execute the steps according to the above method.

[0055] A computer-readable storage medium stores a computer program, which implements the steps of the above method when executed by a processor.

[0056] Beneficial effect: The present invention provides an algorithm for improving k value and initial cluster center based on k-means algorithm. In the improved algorithm, in terms of the selection of initial k value, a new idea of ​​total square error (SSE) calculation is introduced, and with the assistance of certain indicator parameters, a suitable k value is calculated; in the initialization of centroid, a method for calculating the relative number within the cluster is proposed to avoid selecting outliers as much as possible; in each iteration, the idea of ​​the relative number within the cluster is used to select a suitable point as the cluster center of the next iteration until the current cluster has a stable trend.

[0057] The method of the present invention is tested based on relevant data sets. Compared with the original k-means algorithm, DB algorithm and other related algorithms, the improved algorithm reduces the number of iterations and improves the accuracy of clustering. The improved algorithm not only roughly segments the image according to pixels, but also enhances the local part of the image; in some areas with similar colors, it can still finely divide different fragments. BRIEF DESCRIPTION OF THE DRAWINGS

[0058] Figure 1 is the algorithm flow chart;

[0059] Figure 2 This is the k-SSE value diagram of the traditional elbow method;

[0060] Figure 3 It is the improved k-TSS value diagram;

[0061] Figure 4 The experimental results of color segmentation based on the original algorithm and the improved algorithm are shown;

[0062] Figure 5 This is a table of predicted k values ​​based on the algorithm and data set;

[0063] Figure 6 The result graphs of each algorithm based on the Seeds dataset;

[0064] Figure 7 This is a statistical table of the results of each algorithm. DETAILED DESCRIPTION

[0065] The implementation of the technical solution is further described in detail below in conjunction with the accompanying drawings. The following embodiments are only used to more clearly illustrate the technical solution of the present invention, and cannot be used to limit the protection scope of the present invention.

[0066] like Figure 1 An embodiment shown in the figure provides: a method for image color segmentation based on a k-means algorithm, comprising the following steps:

[0067] Obtain image data to form a sample data set, and determine the number of cluster center points k of the sample data set based on the elbow method;

[0068] The acquisition of image data to form a sample data set comprises the following steps: converting a color array consisting of three primary color values ​​of each pixel of the image into a data point of sample data in a three-dimensional space;

[0069] The number of cluster center points k is 1 to 10, the sample data is divided into k clusters, and the values ​​of intra-cluster square error and SSE corresponding to the k value are calculated respectively.

[0070]

[0071] Among them, SSE i represents the sum of squared errors within the i-th cluster, c i represents the i-th cluster center, and p represents the cluster center of c i is the data point in the cluster center, k represents the number of clusters or cluster center points in the data set;

[0072] Assign different weights according to the degree of aggregation of each cluster, and calculate the corresponding total square error (TSS);

[0073]

[0074] Then draw a line graph based on different k values ​​and corresponding TSS values;

[0075] Calculate the rate of change c of each point using the k value and the corresponding TSS value;

[0076]

[0077] Where: x[i] represents the TSS value corresponding to k=i;

[0078] Set the indicator function log 10 c 2 And set a threshold, the threshold is set to 0.1;

[0079] Indicator function:

[0080] y=log 10 c 2

[0081] When the function value is lower than the threshold, the SSE curve reaches the elbow point, that is, the corresponding k value represents the actual number of clusters.

[0082] Step 2: Calculate the relative counts within the cluster for all data points in the sample data set and select the first initial cluster center point:

[0083] Calculate the Euclidean distance between all data points and other data points, and then sort the resulting Euclidean distance array in ascending order;

[0084] Determine the boundary distance based on the significant change of the sorted Euclidean distance array;

[0085] The number of data points within the boundary distance range is the relative count within the cluster;

[0086] Data points with large counts and small boundary distances are selected as the first initial cluster center points.

[0087] Step 3: Select the remaining cluster centers according to the distance range between the data point and the first initial cluster center, until k initial cluster centers are selected:

[0088] Calculate the distance between all data points in the sample data set and the existing cluster center point, take the distance D to the nearest cluster center point as the reference value, select the data point with the largest D value as the candidate center point, and if the data point is not an outlier (a point in the sample space that is inconsistent with the general behavior or characteristics of other sample points), it can be used as the next initial center point;

[0089] The D value is calculated as:

[0090]

[0091] Where: Each data point format is:

[0092] x i =(x i1 ,x i2 ,…,x im )

[0093] m is the data dimension, x i represents the i-th data point, j ranges from 1 to num, num is the number of existing cluster centers, c j Represents the jth existing cluster center.

[0094] Step 4, calculate the Euclidean distance between the remaining data points in the sample data and the existing cluster center points;

[0095] Step 5: According to the size of the Euclidean distance, the data points are assigned to the nearest cluster center point and the data is divided into k clusters;

[0096] Step 6, calculate the distance between the data points in each cluster and the cluster center point, and arrange them in ascending order, and take the value of the one-third digit as the next cluster center point; the data point corresponding to the one-third digit as the new cluster center point specifically includes the following steps:

[0097] In each cluster [i], calculate the Euclidean distance between each data point and other data points in the same cluster to obtain a distance set, recorded as dis_InCluster[]. Then sort this distance set dis_InCluster[] in ascending order to obtain the ascending distance set, recorded as dis_InCluster_Sorted[]. Select the data point corresponding to the one-third digit value in dis_InCluster_Sorted[] as the new center point.

[0098] Step 7: Update the cluster center point and repeat steps 4 to 6 until the cluster center point no longer changes or the specified number of iterations is reached.

[0099] like Figure 2 and 3 As shown in the figure: Compared with the k-SSE graph obtained by the traditional elbow method, the improved algorithm can solve the problem of unclear inflection points that may occur in the traditional elbow method to a certain extent, making the inflection points easier to obtain by visual inspection.

[0100] like Figure 4 As shown in the figure, in the image color segmentation experiment of this embodiment, the improved algorithm and the original algorithm are based on the same k value. The overall effect of the algorithm is evaluated by comparing the final output images generated by the two algorithms with the original images. In each set of experiments, the proposed improved algorithm has obvious improvements compared with the original algorithm. In the whole image segmentation process, this difference is most obvious in the background picture of WeChat login.

[0101] like Figure 5 As shown: This improved algorithm can also be used to process data sets. Figure 4 The figure shows the k values ​​predicted by the traditional algorithm and the improved algorithm based on four different data sets. It can be seen that the k value obtained by the improved algorithm is more accurate.

[0102] like Figure 6 As shown in Figure 1, the clustering results are obtained by clustering the Seeds dataset using five algorithms. In this case, the traditional K-means algorithm terminates after 13 iterations, while the improved algorithm only needs 5 iterations. The termination criteria of the traditional algorithm is modified to a maximum of 5 iterations. The final clustering results are shown in Figure 1. Figure 5 As shown in Figure 3.

[0103] like Figure 7As shown in the figure: the final clustering results of the five algorithms based on three sample data sets; due to the limitations of the k-means algorithm itself, it may not be possible to achieve 100% clustering accuracy on some data sets. However, compared with other algorithms, the proposed improved algorithm has improved efficiency to a certain extent. Overall, through experiments, the proposed improved algorithm has improved in terms of clustering effect and reduced workload (number of algorithm iterations).

[0104] An image color segmentation system based on k-means algorithm, comprising:

[0105] Acquisition module: used to acquire image data, form a sample data set, and determine the number of cluster center points k of the sample data set based on the elbow method;

[0106] Preliminary selection module: used to calculate the relative counts within the cluster of all data points in the sample data set and select the first initial cluster center point;

[0107] Depth selection module: select the remaining cluster centers according to the distance range between the data point and the first initial cluster center, until k initial cluster centers are selected;

[0108] Loop module: used to repeat the preset loop steps until the cluster center point no longer changes or reaches the specified number of iterations. The preset loop includes:

[0109] Calculate the Euclidean distance between the remaining data points in the sample data and the existing cluster center points;

[0110] According to the size of the Euclidean distance, the data points are assigned to the nearest cluster center point and the data is divided into k clusters;

[0111] Calculate the distance between the data points in each cluster and the cluster center, arrange them in ascending order, and take the value of the third digit as the next cluster center.

[0112] A k-means algorithm-based image color segmentation device, comprising: a processor and a storage medium;

[0113] The storage medium is used to store instructions;

[0114] The processor is used to operate according to the instructions to execute the steps according to the above method.

[0115] A computer-readable storage medium stores a computer program, which implements the steps of the above method when executed by a processor.

[0116] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the technical principles of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.

Claims

1. The image color segmentation method based on the k-means algorithm is characterized by: include Obtain image data to form a sample data set, and determine the number of cluster center points k of the sample data set based on the elbow method; Calculate the relative counts within the cluster of all data points in the sample data set and select the first initial cluster center point; Select the remaining cluster centers according to the distance range between the data point and the first initial cluster center point until k initial cluster centers are selected; Repeat the preset loop steps until the cluster center point no longer changes or reaches a specified number of iterations, the preset loop includes: Calculate the Euclidean distance between the remaining data points in the sample data and the existing cluster center points; According to the size of the Euclidean distance, the data points are assigned to the nearest cluster center point and the data is divided into k clusters; Calculate the distance between the data points in each cluster and the cluster center, arrange them in ascending order, and take the value of the third digit as the next cluster center.

2. The image color segmentation method based on the k-means algorithm according to claim 1, characterized in that: The method of acquiring image data and forming a sample data set includes the following steps: converting a color array consisting of three primary color values ​​of each pixel point of the image into a data point of the sample data in a three-dimensional space.

3. The image color segmentation method based on the k-means algorithm according to claim 2, characterized in that: The method of determining the number k of cluster center points of sample data based on the elbow method comprises the following steps: Divide the sample data into k clusters, and calculate the intra-cluster square error and SSE values ​​corresponding to the k value. Among them, SSE i represents the sum of squared errors within the i-th cluster, c i represents the i-th cluster center, and p represents the cluster center of c i is the data point in the cluster center, k represents the number of clusters or cluster center points in the data set; Assign different weights according to the degree of aggregation of each cluster, and calculate the corresponding total square error (TSS); Then draw a line graph based on different k values ​​and corresponding TSS values; Use the k value and the corresponding TSS value to calculate the rate of change c of each data point on the line graph; Where: x[i] represents the TSS value corresponding to k=i; Set the indicator function log 10 c 2 And set the threshold, Indicator function: y=log 10 c 2 When the function value is lower than the threshold, the SSE curve reaches the elbow point, that is, the corresponding k value represents the actual number of clusters.

4. The image color segmentation method based on the k-means algorithm according to claim 1, characterized in that: Calculate the relative counts of all data points in the sample data set and select the first initial cluster center point, including the following steps: Calculate the Euclidean distance between all data points and other data points, and then sort the resulting Euclidean distance array in ascending order; Determine the boundary distance based on the significant change of the sorted Euclidean distance array; The number of data points within the boundary distance range is the relative count within the cluster; Data points with large counts and small boundary distances are selected as the first initial cluster center points.

5. The image color segmentation method based on the k-means algorithm according to claim 1, characterized in that: The method of selecting the remaining cluster center points according to the distance range between the data point and the first initial cluster center point until k initial cluster center points are selected specifically includes the following steps: Calculate the distance between all data points in the sample data set and the existing cluster center point, take the distance D to the nearest cluster center point as the reference value, select the data point with the largest D value as the candidate center point, and if the data point is not an outlier, it can be used as the next initial center point; The D value is calculated as: Where: Each data point format is: x i =(x i1 ,x i2 ,…,x im ) m is the data dimension, x i represents the i-th data point, j ranges from 1 to num, num is the number of existing cluster centers, c j Represents the jth existing cluster center.

6. The image color segmentation method based on the k-means algorithm according to claim 1, characterized in that: The outlier point is a point in the sample space that is inconsistent with the general behavior or characteristics of other sample points.

7. The image color segmentation method based on the k-means algorithm according to claim 1, characterized in that: The method of taking the value of the one-third digit as the next cluster center point specifically includes the following steps: In each cluster, the Euclidean distance between each data point and other data points in the same cluster is calculated to obtain a distance set; Then sort this distance set in ascending order to obtain an ascending distance set; Select the data point corresponding to the middle third digit value as the new center point.

8. An image color segmentation system based on k-means algorithm, characterized in that: include: Acquisition module: used to acquire image data, form a sample data set, and determine the number of cluster center points k of the sample data set based on the elbow method; Preliminary selection module: used to calculate the relative counts within the cluster of all data points in the sample data set and select the first initial cluster center point; Depth selection module: select the remaining cluster centers according to the distance range between the data point and the first initial cluster center, until k initial cluster centers are selected; Loop module: used to repeat the preset loop steps until the cluster center point no longer changes or reaches the specified number of iterations. The preset loop includes: Calculate the Euclidean distance between the remaining data points in the sample data and the existing cluster center points; According to the size of the Euclidean distance, the data points are assigned to the nearest cluster center point and the data is divided into k clusters; Calculate the distance between the data points in each cluster and the cluster center, arrange them in ascending order, and take the value of the third digit as the next cluster center.

9. An image color segmentation device based on k-means algorithm, characterized in that: include: Processor and storage media; The storage medium is used to store instructions; The processor is configured to operate according to the instructions to execute the steps of the method according to any one of claims 1-7.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.

Citation Information

Patent Citations

  • K-Means improved algorithm for selecting initial clustering center based on region

    CN110084325A

  • New energy power generation cluster division method considering resource space-time correlation

    CN114944645A

  • Accelerated discrete distribution clustering under wasserstein distance

    US20170083608A1

  • System and Method for Extremely Efficient Image and Pattern Recognition and Artificial Intelligence Platform

    US20180204111A1