Application of hard clustering (HCM) and fuzzy clustering (FCM) in Color Image Segmentation

Source: Internet
Author: User

For the example project, see:

Http://files.cnblogs.com/laviewpbt/%e5%9b%be%e5%83%8f%e6%a8%a1%e7%b3%8a%e8%81%9a%e7%b1%bb.rar

 

I wrote about the VB6.0 Implementation and Application of the Fuzzy Clustering Algorithm (FCM) and hard clustering algorithm (HCM) a year ago.

Later, many colleagues asked me how to apply this algorithm to color image segmentation. In view of the particularity of image data, here we will talk about some details about clustering algorithms in images.

Compared with other clustering algorithms, the C-means clustering algorithm can calculate large amounts of data (I have also written about fuzzy equivalent relationship-based fuzzy clustering analysis.
This method can only analyze a small amount of data), and theoretically it has been proved that the algorithm is converged. Therefore, there are already many methods for image segmentation, especially the color image segmentation of more than two categories (two types of segmentation are the binarization process, see several classic binarization Methods and Their VB.net implementation
But these methods are difficult to expand in multi-class segmentation), has a wide range of applications.

We know that the color image data can be regarded as an M * n * 3 array, but on the one hand, the processing speed of the three-dimensional array is lower than that of the one-dimensional or two-dimensional array. In addition, from the perspective of FCM universality, generally, the object to be split is a sample group, and a sample can be regarded as a point in the n-dimensional space. Therefore, using a two-dimensional array to describe the object to be split is not only significant, but also conducive to improving the computing speed. In Matlab and the FCM function I wrote earlier, the first dimension is the sample, and the second dimension is the specific data of the sample. However, the image data is arranged in the memory in bgrabrgabgra ....... in this way, the data directly read through the API function is generally an array (1 to 4, 1 to M * n). Therefore, to apply the previously written FCM function to image segmentation, first change the order of partial loops in the function.

The data size of color images is very large. To make the FCM or HCM function have practical value, we must grasp every detail of the function. Below I will describe several issues that I have encountered in practice.

1. Select the initial center

The selection of the initial center directly affects the calculation speed and result. An unreasonable initial point may cause the result to converge to an undesired minimum point. Here are several methods to select the initial center for your reference.

  • Random value, when we do not have any prior knowledge about the source or distribution of the sample, this is a good strategy, but be careful not to exceed the distribution range of known data, such as for image data, RND * 255 can be used to ensure that the data is in (0 ~ 255. However, the computing speed of this method is quite slow.
  • It is slightly more reasonable to select a random number of such methods from known data than the first method, and to be more transparent to the center of known data points.
  • It is similar to the preceding method to select several vertices from known data in an equal distance.
  • Mean-standard deviation method. Based on the distribution knowledge of Random Functions, clustering data should be mainly distributed near the mean of all data. Standard deviation is another important indicator for evaluating data distribution. if the mean value of all data is μ and the standard deviation is σ, the data should be mainly distributed between (μ-σ, μ + σ. Assume that the number of classifications is N, and N equal points are selected for classification based on the initial classification points (μ-σ, μ + σ. Image data can be calculated on R/G/B respectively. The actual running results also show that this method is faster than the preceding three methods in most cases.
  • The method of getting the center from the thumbnail is specific to the image. The initial clustering center is obtained by clustering in the thumbnail of a certain size, because the thumbnail can be seen as the compression of the original data, in addition, this compression has little impact on the clustering center, and the generation of thumbnails has a high-speed algorithm. Compared with clustering, the time required is negligible.
  • Manually input data for specific columns of images, such as medical images, we generally know the approximate clustering center, in this way, manually specify the initial cluster center to bring it closer to the final center to speed up.
  • For the FCM algorithm, you can also use HCM to generate a central point.

2. Sample Space Selection

  • Generally, r | G | B can be directly used as the data to be clustered.
  • If you consider the impact of noise, you can first filter or select the data generated by the appropriate template (such as 3*3 Weighted template) as the clustering data source.
  • Data with HSB or other color spaces
  • For color image data, there are actually many pixels of the same color, so the data compression and merging methods are used to reduce the amount of data involved in computing and reduce the computing overhead, this greatly reduces the time for each iteration of the FCM Algorithm and increases the computing speed. First, we calculate the number of different colors actually used in the image and the number of pixels for each color number. Then, the amount of data to be calculated in iteration is greatly reduced. However, this method may not be suitable for HCM and will be discussed later.

3. Distance Definition

Different definitions reflect different separation ideas. The following are commonly used methods for defining a centralized distance.

  • Euclidean distance)
    (1) I believe everyone is very familiar with this distance formula. I learned it in junior high school, also known as the distance between two points. The Euclidean distance between p and q is defined as follows:
    De (p, q) = [(X-S) 2 + (Y-T) 2] 1/2
    (2) intuitive description of distance: the Euclidean distance between the distance (X, Y) and the distance equal to or greater than a certain value of R is the circular plane with the center (x, y) radius of R.
  • City-block distance)
    (1) the distance between p and q is defined as follows:
    D4 (p, q) = | X-S | + | Y-T |
    (2) intuitively describe the distance: the distance between the distance (X, Y) and the city that is less than or equal to a value of R is a diamond with the center located on the (x, y) diagonal line 2R.
  • Board distance (chess board distance)
    (1) The checkboard distance between p and q is defined as follows:
    D8 (p, q) = max (| X-S |, | Y-T |)
    (2) distance: The checkboard distance between a distance (x, y) less than or equal to a value of R is a square with the center on the (x, y) diagonal line 2R.

See: http://www.ownsoft.com/blog/blogview.asp? Logid = 48

4. Optimization skills

  • Use a temporary array to save some commonly used calculated values. For example, when the distance between the city and the Board is used, define the temparray array and assign values as follows:

For I =-255 to 255
Temparray (I) = ABS (I)
Next

This is much more efficient than using ABS functions directly in the code, that is, using the following code

Dist = temparray (data (1, I)-oldcenter (J, 1) + temparray (data (2, I)-oldcenter (J, 2 )) + temparray (data (3, I)-oldcenter (J, 3 ))

Replace statement:

Dist = ABS (data (1, I)-oldcenter (J, 1) + ABS (data (2, I)-oldcenter (J, 2 )) + ABS (data (3, I)-oldcenter (J, 3 ))

A misunderstanding
: When calculating the Euclidean distance, I also defined an array record n ^ 2 with 256 elements, and then used an array instead of N * n for calculation, the results show that this will lead to a decrease in the computing speed. Therefore, in VB, this simple addition, subtraction, multiplication, division is still very fast. Only when functions such as ABS and EXP are used, if it is convenient to calculate the search table beforehand, it will speed up.

  • The two-dimensional array variables in the middle are all replaced by one-dimensional arrays, and the same data size of one-dimensional arrays is faster, which can increase by about 10%.
  • Use N * n to replace N ^ 2, and use exp (log (degree (I, j) * exponent) to replace degree (I, j) ^ exponent, if possible, use right division instead of left division. For such operations in a large number of loops, the former is definitely much faster than the latter.
  • Do not use for k = 1 to 3. Expand each item manually.
  • The reason for getting the minimum convergence distance of 12 is 2 ^ 2 + 2 ^ 2 + 2 ^ 2, which makes little sense because the clustering center of image data is always an integer type. Similarly, in most data types, you can replace the double-precision type with an integer.

The preceding problems are described in the following section:

It can be seen that the use of random centers, especially the FCM algorithm, takes a long time to converge.


 

For beauty classification, the center is obtained from the thumbnail, and the sample comes from the RGB pixel value. The HCM duration is 0.25 s, and the FCM duration is 1 s.

The other conditions remain unchanged. The sample source selects non-repeated RGB pixel values. The increase in HCM is 437 ms, and the decrease in FCM is 657 ms, because for this image, the time increment required for analyzing non-repeated color values in the HCM algorithm is much more time-saving than the cycle time, while the FCM algorithm is much more complex due to its cycle, when the cycle is reduced, the saving time is particularly significant.

Generally, the ratio of non-repeated color values to the total number of pixels in a color image can reach. When the ratio is more obvious, when we use non-repeated RGB pixel values, the speed increases significantly. Especially for the FCM algorithm, the time can be reduced to about 1/3.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.