reset password
Author Message
ccorra15
Posts: 12
Posted 10:43 Apr 15, 2020 |

How is the K-means algorithm comparing a grayscale value to 4 different rgb values? Are we supposed to get a grayscale value for each rgb vector? and if so, how do we do that? 

yzhao52
Posts: 32
Posted 18:28 Apr 15, 2020 |
Each 2x2 block in the image becomes one 12-D vector as follows:
 
P1R P1G P1B P2R P2G P2B P3R P3G P3B P4R P4G P4B

 

Each codeword (centroid of one cluster) is also one 12-D vector. You do NOT need to and should NOT convert one pixel from 24-bit RGB to grayscale when conducting VQ. Instead, simply concatenate the RGB values for the four pixels in one block when forming the vector.
ccorra15
Posts: 12
Posted 18:41 Apr 15, 2020 |

I understand how to concatenate them. I am just confused on how to compare them, or in other words, calculate the distance to the nearest cluster.

yzhao52
Posts: 32
Posted 18:51 Apr 15, 2020 |

Assume that the candidate centroid (12-D vector) is stored in ck (one 1-D array of length 12) and the current block vector is stored in x (also one 1-D array of length 12). The formula to calculate the Euclidean distance is as follows:

\(d\left(x, c_k\right) = \sqrt{\sum_{j=0}^{11}\left(x\left[j\right]-c_k\left[j\right]\right)^2}\)

The C/Java code is as follows:

sum = 0;

for (int j = 0; j < 12; j++)

    sum += (x[j] - ck[j]) * (x[j] -ck[j]);

dist = sqrt(sum);

Last edited by yzhao52 at 18:55 Apr 15, 2020.