簡單來說這就是一個分群集的演算法。
詳細說明可以參考:

https://jason-chen-1992.weebly.com/home/-k-means-clustering
https://edisonx.pixnet.net/blog/post/84122954
https://dotblogs.com.tw/dragon229/2013/02/04/89919

程式步驟(假設分3群集):

  1. 隨機產生3個群集中心
  2. 計算這些點離群集中心的距離,並分配這些點給較近的群集中心
  3. 重新計算群中心距離
  4. 循環2-3步驟,直至群集中心不變或者變動較小為止。

代碼如下:

#include<opencv2/opencv.hpp>
#include<iostream>
#include <cstdlib> /* 亂數相關函數 */
#include <ctime>   /* 時間相關函數 */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>using  namespace cv;
using namespace std;
#define K_GROUP 4
Mat srcImage, grayImage;
int k_cnt = 0;
int max_x, min_x;
void K_Manes(Point *kp, int k_group);int main()
{char k_first_Flag = 0;Point K_center[K_GROUP];srcImage = imread("C:\\Users\\User\\Downloads\\Image\\1cm.bmp");cvtColor(srcImage, grayImage, CV_RGB2GRAY);//get point rangefor (int x = 0; x < grayImage.rows; x++){for (int y = 0; y < grayImage.cols; y++){if (grayImage.ptr<uchar>(x)[y] != 0){if (k_first_Flag == 0){k_first_Flag = 1;min_x = x;}max_x = x;k_cnt++;//get total point}}}printf("min_x=%d\n", min_x);printf("max_x=%d\n", max_x);//get rand pointint lx, ly;srand(time(NULL));for (int i = 0; i < K_GROUP; i++){lx = rand() % (max_x - min_x + 1) + min_x;printf("lx=%d\n", lx);ly = (rand() % grayImage.cols) + 1;K_center[i].x = lx;K_center[i].y = ly;printf("ly=%d\n", ly);}//k_manes calculateK_Manes(K_center, K_GROUP);imshow("show",grayImage);waitKey();return 0;}void K_Manes(Point *kp, int k_group)
{Point p2;int d[K_GROUP];Point **data;unsigned long **dataD;unsigned long *loc_cnt;long loc_g = 0;//char rand_flag[K_GROUP];int lx, ly;int t = 100, stop_cnt = 0;Point last_p[K_GROUP];data = new Point*[K_GROUP]; dataD = new unsigned long*[K_GROUP];loc_cnt = new unsigned long[K_GROUP];srand(time(NULL));for (int i = 0; i < K_GROUP; i++){data[i] = new Point[k_cnt];dataD[i] = new unsigned long[k_cnt];//rand_flag[i] = 0;last_p[i].x = 0;last_p[i].y = 0;}    for (int i = 0; i < K_GROUP; i++){for (int j = 0; j < k_cnt; j++){data[i][j].x = 0;data[i][j].y = 0;dataD[i][j] = 0;}}//show pointMat show = Mat::zeros(grayImage.rows, grayImage.cols, grayImage.type());for (int x = 0; x < grayImage.rows; x++){for (int y = 0; y < grayImage.cols; y++){for (int i = 0; i < K_GROUP; i++)if (x == kp[i].x && y == kp[i].y){show.ptr<uchar>(x)[y] = 255;for (int j = 0; j < 10; j++){if (x - j > 0 && x + j < grayImage.rows && y + j < grayImage.cols && y - j>0){show.ptr<uchar>(x - j)[y] = 255;show.ptr<uchar>(x + j)[y] = 255;show.ptr<uchar>(x)[y - j] = 255;show.ptr<uchar>(x)[y + j] = 255;}}}}}imshow("show point", show);while (t--){//clear loc_cntfor (int i = 0; i < K_GROUP; i++){loc_cnt[i] = 0;for (int j = 0; j < k_cnt; j++){data[i][j].x = 0;data[i][j].y = 0;dataD[i][j] = 0;}}int samll_d= 0;//find groupfor (int x = 0; x < grayImage.rows; x++){for (int y = 0; y < grayImage.cols; y++){if (grayImage.ptr<uchar>(x)[y] != 0){p2.x = x;p2.y = y;//find distanceloc_g = 0;samll_d = d[0];for (int i = 0; i < K_GROUP; i++){d[i] = sqrt(pow(p2.x - kp[i].x, 2) + pow(p2.y - kp[i].y, 2));if (samll_d > d[i]){loc_g = i;samll_d = d[i];}}//save datadata[loc_g][loc_cnt[loc_g]].x = p2.x;data[loc_g][loc_cnt[loc_g]].y = p2.y;dataD[loc_g][loc_cnt[loc_g]] = samll_d;loc_cnt[loc_g]++;data[loc_g][loc_cnt[loc_g]].x = 0;data[loc_g][loc_cnt[loc_g]].y = 0;dataD[loc_g][loc_cnt[loc_g]] = 0;}}}//count max pointint s = loc_cnt[0];int sl = 0;for (int i = 0; i < K_GROUP; i++){if (s < loc_cnt[i]){s = loc_cnt[i];sl = i;}}//draw center pointchar p = 5;Mat show2 = Mat::zeros(show.rows, show.cols, show.type());for (int x = 0; x < show.rows; x++){for (int y = 0; y < show.cols; y++){for (int i = 0; i < K_GROUP; i++){if (x == kp[i].x && y == kp[i].y){show2.ptr<uchar>(x)[y] = 255;i == sl ? p = 10 : p = 5;for (int j = 0; j < p; j++){if (x - j > 0 && x + j < grayImage.rows && y + j < grayImage.cols && y - j>0){show2.ptr<uchar>(x - j)[y] = 255;show2.ptr<uchar>(x + j)[y] = 255;show2.ptr<uchar>(x)[y - j] = 255;show2.ptr<uchar>(x)[y + j] = 255;}}}}}}//return loop Mat show3 = Mat::zeros(show2.rows, show2.cols, show2.type());for (int i = 0; i < K_GROUP; i++){if (stop_cnt > 3 || t == 0){printf("t=%d\n", 100 - t);for (int x = 0; x < grayImage.rows; x++){for (int y = 0; y < grayImage.cols; y++){if (grayImage.ptr<uchar>(x)[y] != 0){show2.ptr<uchar>(x)[y] = 255;}}}for (int x = 0; x < K_GROUP; x++){printf("loc_cnt[%d]=%ld\n", x, loc_cnt[x]);printf("kp[%d].x=%d  kp[%d].y=%d \n", x, kp[x].x, x, kp[x].y);}imshow("show2 point", show2);return;}if (last_p[i].x == kp[i].x && last_p[i].y == kp[i].y && loc_cnt[i]>0)stop_cnt++;else if (sqrt(pow(last_p[i].x - kp[i].x, 2) + pow(last_p[i].y - kp[i].y, 2))<5)stop_cnt++;}//calcuate K-group center againPoint max_loc[K_GROUP], min_loc[K_GROUP];unsigned long long max_p, min_p;unsigned long long  new_point_X[K_GROUP];unsigned long long  new_point_Y[K_GROUP];for (int i = 0; i < K_GROUP; i++){new_point_X[i] = 0;new_point_Y[i] = 0;max_p = dataD[i][0];min_p = dataD[i][0];max_loc[i].x = data[i][0].x;min_loc[i].y = data[i][0].y;for (int j = 0; j < loc_cnt[i]; j++){if (data[i][j].x != 0 && data[i][j].y != 0){if (loc_cnt[i] > 0){/*average point*/new_point_X[i] += data[i][j].x;new_point_Y[i] += data[i][j].y;}}}}//update group centerfor (int i = 0; i < K_GROUP; i++){if (loc_cnt[i] > 0){/*average point*/new_point_X[i] /= loc_cnt[i];new_point_Y[i] /= loc_cnt[i];last_p[i].x = kp[i].x;last_p[i].y = kp[i].y;kp[i].x = new_point_X[i];kp[i].y = new_point_Y[i];}}}
}

此程式碼將圖片(灰階)載入後,取白灰點做處理並分群集。

K-means (代碼)相关推荐

  1. root_path运用python_Python current_app.root_path方法代碼示例

    本文整理匯總了Python中flask.current_app.root_path方法的典型用法代碼示例.如果您正苦於以下問題:Python current_app.root_path方法的具體用法? ...

  2. java getselecteditem_Java JComboBox.getSelectedItem方法代碼示例

    本文整理匯總了Java中javax.swing.JComboBox.getSelectedItem方法的典型用法代碼示例.如果您正苦於以下問題:Java JComboBox.getSelectedIt ...

  3. python execute_command err_Python management.execute_from_command_line方法代碼示例

    本文整理匯總了Python中django.core.management.execute_from_command_line方法的典型用法代碼示例.如果您正苦於以下問題:Python manageme ...

  4. rowdata java_Java RowDataUtil.addRowData方法代碼示例

    本文整理匯總了Java中org.pentaho.di.core.row.RowDataUtil.addRowData方法的典型用法代碼示例.如果您正苦於以下問題:Java RowDataUtil.ad ...

  5. python template languages_Python template.TemplateSyntaxError方法代碼示例

    本文整理匯總了Python中django.template.TemplateSyntaxError方法的典型用法代碼示例.如果您正苦於以下問題:Python template.TemplateSynt ...

  6. bls java_Java PairingFactory.getPairing方法代碼示例

    本文整理匯總了Java中it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory.getPairing方法的典型用法代碼示例.如果您正苦於以下問題:Java ...

  7. python time strptime_Python time.strptime方法代碼示例

    本文整理匯總了Python中time.strptime方法的典型用法代碼示例.如果您正苦於以下問題:Python time.strptime方法的具體用法?Python time.strptime怎麽 ...

  8. python柱状图zzt_Python torch.diag方法代碼示例

    本文整理匯總了Python中torch.diag方法的典型用法代碼示例.如果您正苦於以下問題:Python torch.diag方法的具體用法?Python torch.diag怎麽用?Python ...

  9. ASP + Serv-u 實現FTP的代碼

    ASP + Serv-u 實現FTP的代碼 發佈者:[飛翔] 瀏覽:[ ] 評論:[0]  <!--#include file="md5.asp"--> <% ' ...

  10. java entry getvalue_Java Entry.getValue方法代碼示例

    本文整理匯總了Java中java.util.Map.Entry.getValue方法的典型用法代碼示例.如果您正苦於以下問題:Java Entry.getValue方法的具體用法?Java Entry ...

最新文章

  1. python 归一化 标准化
  2. linux 入门教程
  3. (win10 64位系统中)Visual Studio 2015+OpenCV 3.3.0环境搭建,100%成功
  4. H.264边缘块进行帧内预测时,上边缘和左边缘块的预测情况。
  5. SpringBoot 自带工具类~AopUtils
  6. 基于ansj_seg和nlp-lang的简单nlp工具类
  7. python实现的摩斯电码解码\编码器
  8. c#中dateTimePicker怎么获取当前的日期
  9. C#中 ?? 的用法
  10. php 如何生成txt文件,PHP生成TXT文件
  11. E8.Net工作流开发架构
  12. 为什么 Mac 适合编程?
  13. ant design——Modal
  14. BGP手动路由聚合(学习笔记+实验验证)
  15. OPNsense用户手册-初始安装和配置
  16. Excel2010基础-学习笔记
  17. java.security.InvalidKeyException:illegal Key Size 报错解决方案
  18. 这应该是最全的软件测试工程师必读书籍
  19. BananaPi Wifi 连接
  20. react中CodeMirror (代码编辑器)

热门文章

  1. 光伏产业红与黑 需求寒冬背后暗藏分布式春天
  2. 给媳妇做一个记录心情的小程序
  3. android系统toast是什么意思,Android - Snackbar vs Toast - 使用和区别
  4. 家乐福中国:曾经的零售之王走上资本运作之路
  5. 微前端在小米 CRM 系统的实践
  6. 教你怎么学好Java
  7. 用于播放硬盘中avi视频的简单程序(opencv)
  8. 记我的第一次腾讯游戏策划面试
  9. Google Glass: 未来不遥远
  10. 认识SOAR-安全事件编排自动化响应