comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » KMean Clustering of RGB Images
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
KMean Clustering of RGB Images [message #53928] Tue, 08 May 2007 03:45
helaha is currently offline  helaha
Messages: 26
Registered: March 2004
Junior Member
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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: genetic algorithm
Next Topic: regular expressions (parsing strings)

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 13:38:50 PDT 2025

Total time taken to generate the page: 0.00510 seconds