Recently I tested the Kmeans clustering of an RGB image with the
CLUSTER and CLUST_WTR commands.
The results were not very promising, because they depended on the
image orientation and on the number of image pixels.
I'm not a specialist in clustering, but in my opinion the results
should be identical, especially regardless of the order of the data
points.
Here is a short script for testing. The total number of pixels can be
set with the variable "SmallSize" for the CONGRID command.
The REVERSE or TRANSPOSE commands can be activated optionally.
Any ideas?
Many thanks for any input!
Helmut Ahammer
Institute of Biophysics
MUG, Austria
PRO Cluster_KMean_Image
;Load RGB Image
ImageName = DIALOG_PICKFILE()
CD, FILE_DIRNAME(ImageName)
READ_JPEG, ImageName, RGBImage
IIMAGE, RGBImage, /NO_SAVEPROMPT, /OVERPLOT
Info = SIZE(RGBImage)
ImageSizeX = Info[2]
ImageSizeY = Info[3]
;Make image smaller and therfore the clustering time shorter
SmallSize = 20
RGBImage = CONGRID(RGBImage, 3, SmallSize, SmallSize, /INTERP)
;Optional
;RGBImage[0,*,*] = REVERSE(REFORM(RGBImage[0,*,*]), 2)
;RGBImage[1,*,*] = REVERSE(REFORM(RGBImage[1,*,*]), 2)
;RGBImage[2,*,*] = REVERSE(REFORM(RGBImage[2,*,*]), 2)
;or
;RGBImage = TRANSPOSE(RGBImage, [0, 2, 1])
PixelN = ULONG(SmallSize * SmallSize)
;Convert to RGB Space with 3 variables and PixelN items
RGBSpace = REFORM(RGBImage, 3, PixelN)
;KMean Clustering
k = 2
Weights = CLUST_WTS(RGBSpace, N_CLUSTERS = k, N_ITERATIONS = 20)
CenterInd = CLUSTER (RGBSpace, Weights, N_CLUSTERS = 2)
Center = TRANSPOSE(Weights)
;Set RGB space to the cluster centers
FOR j = 0, k-1 DO BEGIN
Ind = WHERE(CenterInd EQ j, Count)
PRINT, 'Count Cluster k', j+1, Count
FOR d = 0l, Count-1 DO RGBSpace[*, Ind[d]] =
REFORM(Center[j,*])
ENDFOR
;Convert RGB Space to Image
Image_Clust = REFORM(RGBSpace, 3, SmallSize, SmallSize)
;Display magnified clustered image over original image
IIMAGE, CONGRID(Image_Clust, 3, ImageSizeX/2, ImageSizeY/2), /
NO_SAVEPROMPT, /OVERPLOT
END
|