K-means聚类算法

要求:用VC、Java或Matlab实现K-means聚类算法(做图像处理Matlab最简单),以迭代次数为终止条件,用图像分割数据库中的图片http://www.wisdom.weizmann.ac.il/~vision/Seg_Evaluation_DB/index.html作为数据集(单对象和双对象各20个),提交实验报告与源代码。

转自http://blog.youkuaiyun.com/jia20003/article/details/8828648

 

(1).初始化cluster中心点(随机选取)

Random random = new Random();

       for (int i = 0; i <numOfCluster; i++)

       {

           int randomNumber1 = random.nextInt(width);

           int randomNumber2 = random.nextInt(height);

           index = randomNumber2 * width + randomNumber1;

           ClusterCenter cc = new ClusterCenter(randomNumber1, randomNumber2, inPixels[index]);

           cc.setcIndex(i);

           clusterCenterList.add(cc);

       }

 

(2).初始化所有像素点

for (int row = 0; row < height; ++row)

        {

            for (int col = 0; col < width; ++col)

            {

              index = row * width + col;

              int color = inPixels[index];

              pointList.add(new ClusterPoint(row, col, color));

 

            }

        }

 

(3).计算两个像素点之间欧几里德距离

privatedouble calculateEuclideanDistance(ClusterPoint p, ClusterCenter c)

    {

       //int pa = (p.getPixelColor() >> 24) & 0xff;

        int pr = (p.getPixelColor() >> 16) & 0xff;

        int pg = (p.getPixelColor() >> 8) & 0xff;

        int pb = p.getPixelColor() & 0xff;

        // int ca = (c.getPixelColor() >> 24) & 0xff;

        int cr = (c.getPixelColor() >> 16) & 0xff;

        int cg = (c.getPixelColor() >> 8) & 0xff;

        int cb = c.getPixelColor() & 0xff;

       

        return Math.sqrt(Math.pow((pr - cr), 2.0) + Math.pow((pg - cg), 2.0) + Math.pow((pb - cb), 2.0));

    }

 

(4).重新计算Cluster中心点RGB值

privatedouble[] reCalculateClusterCenters() {

      

       // clear the points now

       for(int i=0; i<clusterCenterList.size(); i++)

       {

            clusterCenterList.get(i).setNumOfPoints(0);

       }

      

       // recalculate the sum and total of points for each cluster

       double[] redSums =newdouble[3];

       double[] greenSum =newdouble[3];

       double[] blueSum =newdouble[3];

       for(int i=0; i<pointList.size(); i++)

       {

           int cIndex = (int)pointList.get(i).getClusterIndex();

           clusterCenterList.get(cIndex).addPoints();

           //int ta = (pointList.get(i).getPixelColor() >> 24) & 0xff;

            int tr = (pointList.get(i).getPixelColor() >> 16) & 0xff;

            int tg = (pointList.get(i).getPixelColor() >> 8) & 0xff;

            int tb =pointList.get(i).getPixelColor() & 0xff;

            //ta = 255;

           redSums[cIndex] += tr;

           greenSum[cIndex] += tg;

           blueSum[cIndex] += tb;

       }

      

       double[] oldClusterCentersColors =newdouble[clusterCenterList.size()];

       for(int i=0; i<clusterCenterList.size(); i++)

       {

           double sum = clusterCenterList.get(i).getNumOfPoints();

           int cIndex =clusterCenterList.get(i).getcIndex();

           int red = (int)(greenSum[cIndex]/sum);

           int green = (int)(greenSum[cIndex]/sum);

           int blue = (int)(blueSum[cIndex]/sum);

           System.out.println("red = " + red +" green = " + green +" blue = " + blue);

           int clusterColor = (255 << 24) | (red << 16) | (green << 8) | blue;

           clusterCenterList.get(i).setPixelColor(clusterColor);

           oldClusterCentersColors[i] = clusterColor;

       }

      

       return oldClusterCentersColors;

    }

 

 附:代码下载

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值