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

CN118570460BActive Publication Date: 2026-09-22NANJING UNIV OF INFORMATION SCI & TECH
View PDF 2 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

[0002]图像分割常用于将图像划分成多个具有相似性质或特定标记的区域,使得每个区域内的像素具有相似的特征或满足某种标记条件在某些颜色相似的区域,K-means算法常常用于辅助图像分割,但在在某些颜色相似的区域,不能精细划分出不同的片段;由于K-means算法简单易懂、效率高,在实际应用中得到了广泛的应用

Benefits of technology

[0056]有益效果:本发明提供了一种基于k-means算法的改良k值和初始聚类中心的算法,改良算法中,在对初始k值的选取方面,引入了总误差平方和SSE计算的新思路,辅以一定的指标参数,计算出合适的k值;在质心的初始化上,提出了簇内相对数的计算方法,尽可能避免选择离群异常点;每次迭代时通过簇内相对数的思想选择合适的点作为下一次迭代的簇中心,直到当前聚类有稳定的趋势。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118570460B_ABST
    Figure CN118570460B_ABST
Patent Text Reader

Abstract

The application discloses a k-means algorithm-based image color segmentation method, system and device and a storage medium. Image data is acquired to form a sample data set. In the improved algorithm, a new idea of total error square sum SSE calculation is introduced in the selection of an initial k value, and a certain index parameter is supplemented to calculate a suitable k value. In the initialization of a centroid, a cluster relative number calculation method is proposed to avoid selecting an outlier as much as possible. The application mainly analyzes problems related to k value selection, initialization of a clustering center point and an iteration process in the K-means algorithm. The application aims to strengthen the selection of an initial cluster value, improve the random selection of an initial center point in the original algorithm and avoid the negative influence of selecting an outlier as an initial center point on a final clustering result. In addition, related indexes are optimized in the clustering iteration process, so that the clustering result is improved. In a color similar region, different segments 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] This invention discloses an image color segmentation method, system, device, and storage medium based on the k-means algorithm, which relates to the field of computer clustering analysis. Background Technology

[0002] Image segmentation is often used to divide an image into multiple regions with similar properties or specific labels, such that pixels within each region have similar features or satisfy certain labeling conditions. In some color-similar regions, the K-means algorithm is often used to assist image segmentation, but it cannot precisely segment different segments in certain color-similar regions. Due to its simplicity, ease of understanding, and high efficiency, the K-means algorithm is widely used in practical applications. This algorithm mainly faces two problems: 1. The determination of the parameter k has a significant impact on the clustering effect. If k is set too large, it may lead to too many clusters, while setting k too small may result in weak clustering. 2. The quality of clustering largely depends on the initial selection of cluster centers. During the algorithm iteration process, it may get trapped in local optima. Summary of the Invention

[0003] To address the shortcomings of the aforementioned background technology, this 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, increases the accuracy of clustering, and enhances the fine segmentation of images.

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

[0005] Acquire image data to form a sample dataset, and determine the number of cluster centers k of the sample dataset based on the elbow method;

[0006] Calculate the intra-cluster relative counts of all data points in the sample dataset and select the first initial cluster center.

[0007] The remaining cluster centers are selected based on the distance range between the data points and the first initial cluster center, until k initial cluster centers are selected;

[0008] Repeat the preset loop steps until the cluster centers no longer change or the specified number of iterations is reached. The preset loop includes:

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

[0010] Based on the magnitude of the Euclidean distance, the data points are assigned to the nearest cluster center, and the data is divided into k clusters.

[0011] Calculate the distance of each data point in each cluster to the cluster center, sort them in ascending order, and take the third-third value 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 and the formation of a sample dataset includes the following steps: converting each pixel of the image into a color array composed of the three primary color values ​​into a data point of sample data in three-dimensional space.

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

[0014] Divide the sample data into k clusters, and calculate the intra-cluster squared error and SSE corresponding to each value of k.

[0015]

[0016] Among them, SSE i Let c represent the sum of squared errors within the i-th cluster class. i Let p represent the i-th cluster center, and p represent the clusters belonging to the i-th cluster. i The data points in the cluster are the cluster centers, and k represents the number of clusters or the number of cluster centers in the dataset. Based on the clustering characteristics of the dataset, the number of cluster centers k ranges from 1 to 10.

[0017] Different weights are assigned based on the degree of aggregation of each cluster, and the corresponding total squared error (TSS) is calculated.

[0018]

[0019] Then, draw line charts based on different k values ​​and their corresponding TSS values;

[0020] Calculate the rate of change c at 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 a threshold,

[0024] Indicator functions:

[0025] y = log 10 c 2

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

[0027] Furthermore, calculating the intra-cluster relative counts of all data points in the sample dataset and selecting the first initial cluster center 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 significant changes in the sorted Euclidean distance array;

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

[0031] Select data points with larger counts and smaller boundary distances as the first initial cluster centers.

[0032] Furthermore, the step of selecting the remaining cluster centers based on the distance range between the data points and the first initial cluster center, until k initial cluster centers are selected, specifically includes the following steps:

[0033] Calculate the distance between all data points in the sample dataset and the existing cluster center. Use the distance D to the nearest cluster center as a reference value. Select the data point with the largest D value as a candidate center point. 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 follows:

[0035]

[0036] Where: the format of each data point is:

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

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

[0039] Furthermore, the outlier is defined as: a point in the sample space whose general behavior or characteristics are inconsistent with those of other sample points.

[0040] Furthermore, using the data points corresponding to the third digit as new cluster centers specifically includes the following steps:

[0041] Within each cluster [i], calculate the Euclidean distance between each data point and other data points in the same cluster to obtain a distance set, denoted as dis_InCluster[]. Then, use this distance set...

[0042] Sort `dis_InCluster[]` in ascending order to get the ascending distance set, denoted as...

[0043] The data point corresponding to one-third of the value in dis_InCluster_Sorted[] is selected as the new center point.

[0044] An image color segmentation system based on the k-means algorithm includes:

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

[0046] Preliminary selection module: used to calculate the intra-cluster relative count of all data points in the sample dataset and select the first initial cluster center;

[0047] Depth selection module: Selects the remaining cluster centers based on the distance range between the data points and the first initial cluster center, until k initial cluster centers are selected;

[0048] Loop module: Used to repeat preset loop steps until the cluster centers no longer change or the specified number of iterations is reached. The preset loop includes:

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

[0050] Based on the magnitude of the Euclidean distance, the data points are assigned to the nearest cluster center, and the data is divided into k clusters.

[0051] Calculate the distance of each data point in each cluster to the cluster center, sort them in ascending order, and take the third-third value as the next cluster center.

[0052] An image color segmentation device based on the k-means algorithm includes: a processor and a storage medium;

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

[0054] The processor is configured to operate according to the instructions to perform the steps according to the method described above.

[0055] A computer-readable storage medium having a computer program stored thereon that, when executed by a processor, implements the steps of the above method.

[0056] Beneficial effects: This invention provides an improved k-value and initial cluster center algorithm based on the k-means algorithm. In the improved algorithm, a new approach to calculating the sum of squared errors (SSE) is introduced for selecting the initial k value, supplemented by certain index parameters to calculate a suitable k value. In the initialization of centroids, a method for calculating the relative number within the cluster is proposed to avoid selecting outliers as much as possible. In each iteration, a suitable point is selected as the cluster center for the next iteration based on the idea of ​​the relative number within the cluster, until the current clustering shows a stable trend.

[0057] The method of this invention is tested on relevant datasets. 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 performs coarse segmentation of the image based on pixels, but also enhances the local segmentation of the image; in some areas with similar colors, it can still finely segment different segments. Attached Figure Description

[0058] Figure 1 Here is the algorithm flowchart;

[0059] Figure 2 A graph showing the k-SSE values ​​for the traditional elbow method;

[0060] Figure 3 A modified k-TSS value plot;

[0061] Figure 4 These are the experimental results for color segmentation based on the original algorithm and the improved algorithm;

[0062] Figure 5 This is a table showing the predicted k-values ​​based on the algorithm and dataset.

[0063] Figure 6 The results of each algorithm based on the Seeds dataset are shown in the figure;

[0064] Figure 7 This is a statistical table of the results for each algorithm. Detailed Implementation

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

[0066] like Figure 1 The embodiment shown provides an image color segmentation method based on the k-means algorithm, including the following steps:

[0067] Acquire image data to form a sample dataset, and determine the number of cluster centers k of the sample dataset based on the elbow method;

[0068] The process of acquiring image data and forming a sample dataset includes the following steps: converting each pixel of the image into a color array composed of the three primary color values ​​into a data point of the sample data in three-dimensional space;

[0069] The number of cluster centers k ranges from 1 to 10. The sample data is divided into k clusters. The intra-cluster squared error and SSE corresponding to each value of k are calculated.

[0070]

[0071] Among them, SSE i Let c represent the sum of squared errors within the i-th cluster class. i Let p represent the i-th cluster center, and p represent the clusters belonging to the i-th cluster. i The data points in the clusters that are the cluster centers, where k represents the number of clusters or the number of cluster centers in the dataset;

[0072] Different weights are assigned based on the degree of aggregation of each cluster, and the corresponding total squared error (TSS) is calculated.

[0073]

[0074] Then, draw line charts based on different k values ​​and their corresponding TSS values;

[0075] Calculate the rate of change c at 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, wherein the threshold is set to 0.1;

[0079] Indicator functions:

[0080] y = log 10 c 2

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

[0082] Step 2: Calculate the intra-cluster relative counts of all data points in the sample dataset and select the first initial cluster center.

[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 significant changes in the sorted Euclidean distance array;

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

[0086] Select data points with larger counts and smaller boundary distances as the first initial cluster centers.

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

[0088] Calculate the distance between all data points in the sample dataset and the existing cluster center. Use the distance D to the nearest cluster center as a reference value. Select the data point with the largest D value as the candidate center point. If the data point is not an outlier (a point in the sample space whose general behavior or characteristics are inconsistent with other sample points), it can be used as the next initial center point.

[0089] The D value is calculated as follows:

[0090]

[0091] Where: the format of each data point is:

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

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

[0094] Step 4: Calculate the Euclidean distance between the remaining data points in the sample data and the existing cluster centers;

[0095] Step 5: Based on the Euclidean distance, assign the data points to the nearest cluster center, dividing the data into k clusters;

[0096] Step 6: Calculate the distance of each data point in each cluster to the cluster center, sort them in ascending order, and take the value corresponding to one-third of the distance as the next cluster center; the step of taking the data point corresponding to one-third of the distance as the new cluster center specifically includes the following steps:

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

[0098] Step 7: Update the cluster centers and repeat steps 4-6 until the cluster centers no longer change or the specified number of iterations is reached.

[0099] like Figure 2 and 3 As shown: Compared with the k-SSE diagram 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 determine by visual inspection.

[0100] like Figure 4 As shown: In this embodiment, both the improved algorithm and the original algorithm are based on the same k value in the image color segmentation experiment. The overall performance of the algorithms is evaluated by comparing the final output images generated by the two algorithms with the original image. In each set of experiments, the proposed improved algorithm shows a significant improvement compared to the original algorithm. This difference is most pronounced in the background image of WeChat login during the entire image segmentation process.

[0101] like Figure 5 As shown: This improved algorithm can also be applied to dataset processing. Figure 4 The results show the k values ​​predicted by the traditional algorithm and the improved algorithm based on four different datasets. It can be seen that the improved algorithm yields a more accurate k value.

[0102] like Figure 6 As shown: This figure represents the clustering results obtained using five different algorithms on the Seeds dataset. In this case, the traditional K-means algorithm terminates after 13 iterations, while the improved algorithm only requires 5 iterations. By modifying the termination criterion of the traditional algorithm to a maximum of 5 iterations, the final clustering results are as follows: Figure 5 As shown in Figure 3.

[0103] like Figure 7The results show the final clustering results of five algorithms based on three sample datasets. Due to the inherent limitations of the k-means algorithm, it may not achieve 100% clustering accuracy on some datasets. However, compared with other algorithms, the proposed improved algorithm improves efficiency to some extent. Overall, through experiments, the proposed improved algorithm shows improvements in both clustering performance and reduction in workload (number of algorithm iterations).

[0104] An image color segmentation system based on the k-means algorithm includes:

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

[0106] Preliminary selection module: used to calculate the intra-cluster relative count of all data points in the sample dataset and select the first initial cluster center;

[0107] Depth selection module: Selects the remaining cluster centers based on the distance range between the data points and the first initial cluster center, until k initial cluster centers are selected;

[0108] Loop module: Used to repeat preset loop steps until the cluster centers no longer change or the specified number of iterations is reached. The preset loop includes:

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

[0110] Based on the magnitude of the Euclidean distance, the data points are assigned to the nearest cluster center, and the data is divided into k clusters.

[0111] Calculate the distance of each data point in each cluster to the cluster center, sort them in ascending order, and take the third-third value as the next cluster center.

[0112] An image color segmentation device based on the k-means algorithm includes: a processor and a storage medium;

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

[0114] The processor is configured to operate according to the instructions to perform the steps according to the method described above.

[0115] A computer-readable storage medium having a computer program stored thereon that, when executed by a processor, implements the steps of the above method.

[0116] The above description is only a preferred embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the technical principles of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.

Claims

1. An image color segmentation method based on the k-means algorithm, characterized in that, include Acquire image data to form a sample dataset, and determine the number of cluster centers k of the sample dataset based on the elbow method; Calculate the intra-cluster relative counts of all data points in the sample dataset and select the first initial cluster center. The remaining cluster centers are selected based on the distance range between the data points and the first initial cluster center, until k initial cluster centers are selected; Repeat the preset loop steps until the cluster centers no longer change or the specified number of iterations is reached. The preset loop includes: Calculate the Euclidean distance between the remaining data points in the sample data and the existing cluster centers; Based on the magnitude of the Euclidean distance, the data points are assigned to the nearest cluster center, and the data is divided into k clusters. Calculate the distance of each data point in each cluster to the cluster center, sort them in ascending order, and take the third-to-last value as the next cluster center. The process of acquiring image data and forming a sample dataset includes the following steps: converting each pixel of the image into a color array composed of the three primary color values ​​into a data point in the three-dimensional space of the sample data. The process of determining the number of cluster centers k of the sample data based on the elbow method includes the following steps: Divide the sample data into k clusters, and calculate the intra-cluster squared error and SSE corresponding to each value of k. ; in, Let represent the sum of squared errors within the i-th cluster class. Represents the i-th cluster center. Indicates belonging to The data points in the clusters that are the cluster centers, where k represents the number of clusters or the number of cluster centers in the dataset; Different weights are assigned based on the degree of aggregation of each cluster, and the corresponding total squared error (TSS) is calculated. ; Then, draw line charts based on different k values ​​and corresponding TSS values; Calculate the rate of change c for each data point on the line chart using the k value and the corresponding TSS value; ; Where: x[i] represents the TSS value corresponding to k=i; Setting indicator functions And set a threshold, Indicator functions: ; When the function value falls below the threshold, the SSE curve reaches the elbow point; that is, the corresponding k value represents the true number of clusters. The step of selecting the remaining cluster centers based on the distance range between the data points and the first initial cluster center, until k initial cluster centers are selected, specifically includes the following steps: Calculate the distance between all data points in the sample dataset and the existing cluster center. Use the distance D to the nearest cluster center as a reference value. Select the data point with the largest D value as a candidate center point. If the data point is not an outlier, it can be used as the next initial center point. The D value is calculated as follows: ; Where: the format of each data point is: ; m is the data dimension. Let j represent the i-th data point, where j ranges from 1 to num, and num is the number of existing cluster centers. This represents the j-th existing cluster center.

2. The image color segmentation method based on the k-means algorithm according to claim 1, characterized in that, Calculating the intra-cluster relative counts of all data points in the sample dataset and selecting the first initial cluster center includes 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 significant changes in the sorted Euclidean distance array; The number of data points within the boundary distance range is a relative count within the cluster. Select data points with larger counts and smaller boundary distances as the first initial cluster centers.

3. The image color segmentation method based on the k-means algorithm according to claim 1, characterized in that, Outliers are defined as points in the sample space whose general behavior or characteristics are inconsistent with those of other sample points.

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

5. An image color segmentation system based on the k-means algorithm, characterized in that, include: Acquisition module: used to acquire image data, form a sample dataset, and determine the number of cluster centers k of the sample dataset based on the elbow method; Preliminary selection module: used to calculate the intra-cluster relative count of all data points in the sample dataset and select the first initial cluster center; Depth selection module: Selects the remaining cluster centers based on the distance range between the data points and the first initial cluster center, until k initial cluster centers are selected; Loop module: Used to repeat preset loop steps until the cluster centers no longer change or the specified number of iterations is reached. The preset loop includes: Calculate the Euclidean distance between the remaining data points in the sample data and the existing cluster centers; Based on the magnitude of the Euclidean distance, the data points are assigned to the nearest cluster center, and the data is divided into k clusters. Calculate the distance of each data point in each cluster to the cluster center, sort them in ascending order, and take the third-to-last value as the next cluster center. The process of acquiring image data and forming a sample dataset includes the following steps: converting each pixel of the image into a color array composed of the three primary color values ​​into a data point in the three-dimensional space of the sample data. The process of determining the number of cluster centers k of the sample data based on the elbow method includes the following steps: Divide the sample data into k clusters, and calculate the intra-cluster squared error and SSE corresponding to each value of k. ; in, Let represent the sum of squared errors within the i-th cluster class. Represents the i-th cluster center. Indicates belonging to The data points in the clusters that are the cluster centers, where k represents the number of clusters or the number of cluster centers in the dataset; Different weights are assigned based on the degree of aggregation of each cluster, and the corresponding total squared error (TSS) is calculated. ; Then, draw line charts based on different k values ​​and corresponding TSS values; Calculate the rate of change c for each data point on the line chart using the k value and the corresponding TSS value; ; Where: x[i] represents the TSS value corresponding to k=i; Setting indicator functions And set a threshold, Indicator functions: ; When the function value falls below the threshold, the SSE curve reaches the elbow point; that is, the corresponding k value represents the true number of clusters. The step of selecting the remaining cluster centers based on the distance range between the data points and the first initial cluster center, until k initial cluster centers are selected, specifically includes the following steps: Calculate the distance between all data points in the sample dataset and the existing cluster center. Use the distance D to the nearest cluster center as a reference value. Select the data point with the largest D value as a candidate center point. If the data point is not an outlier, it can be used as the next initial center point. The D value is calculated as follows: ; Where: the format of each data point is: ; m is the data dimension. Let j represent the i-th data point, where j ranges from 1 to num, and num is the number of existing cluster centers. This represents the j-th existing cluster center.

6. An image color segmentation device based on the 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 perform the steps of the method according to any one of claims 1-4.

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

Citation Information

Patent Citations

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

    CN110084325A

  • Accelerated discrete distribution clustering under wasserstein distance

    US20170083608A1