学习OpenCV——Kmean(C++)
从前也练习使用过OpenCV的Kmean算法,但是那版本低,而且也是基于C的开发。这两天由于造论文的需要把它重新翻出来在研究一下C++,发现有了些改进
kmeans
- C++: doublekmeans(InputArraydata, int K, InputOutputArray bestLabels, TermCriteriacriteria, int attempts, int flags, OutputArraycenters=noArray() )
- data:输入样本,要分类的对象,浮点型,每行一个样本(我要对颜色分类则每行一个像素);
- K: 类型数目;
- bestLabels: 分类后的矩阵,每个样本对应一个类型label;
- TermCriteria criteria:结束条件(最大迭代数和理想精度)
- int attempts:根据最后一个参数确定选取的最理想初始聚类中心(选取attempt次初始中心,选择compactness最小的);
- int flags :
Flag that can take the following values:
- KMEANS_RANDOM_CENTERS Select random initial centers in each attempt.
- KMEANS_PP_CENTERS Use kmeans++ center initialization by Arthur and Vassilvitskii [Arthur2007].
- KMEANS_USE_INITIAL_LABELS During the first (and possibly the only) attempt, use the user-supplied labels instead of computing them from the initial centers. For the second and further attempts, use the random or semi-random centers. Use one of KMEANS_*_CENTERS flag to specify the exact method.
centers:输出聚类中心,每行一个中心(第一列是聚类中心,但是还有其他列,这里不太明白,大家谁懂,求科普啊!~~)
compactness: 测试初始中心是否最优
上代码:
- #include <string>
- #include <iostream>
- #include <math.h>
- #include <vector>
- #include <map>
- #include "opencv/cv.h"
- #include "opencv/highgui.h"
- #include "opencv/cxcore.h"
- #define ClusterNum (6)
- using namespace cv;
- using namespace std;
- string filename="D:/demo1.jpg";
- Mat clustering(Mat src)
- {
- int row = src.rows;
- int col = src.cols;
- unsigned long int size = row*col;
- Mat clusters(size, 1, CV_32SC1); //clustering Mat, save class label at every location;
- //convert src Mat to sample srcPoint.
- Mat srcPoint(size, 1, CV_32FC3);
- Vec3f* srcPoint_p = (Vec3f*)srcPoint.data;//////////////////////////////////////////////
- Vec3f* src_p = (Vec3f*)src.data;
- unsigned long int i;
- for(i = 0;i < size; i++)
- {
- *srcPoint_p = *src_p;
- srcPoint_p++;
- src_p++;
- }
- Mat center(ClusterNum,1,CV_32FC3);
- double compactness;//compactness to measure the clustering center dist sum by different flag
- compactness = kmeans(srcPoint, ClusterNum, clusters,
- cvTermCriteria (CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 0.1),ClusterNum,
- KMEANS_PP_CENTERS , center);
- cout<<"center row:"<<center.rows<<" col:"<<center.cols<<endl;
- for (int y = 0; y < center.rows; y++)
- {
- Vec3f* imgData = center.ptr<Vec3f>(y);
- for (int x = 0; x < center.cols; x++)
- {
- cout<<imgData[x].val[0]<<" "<<imgData[x].val[1]<<" "<<imgData[x].val[2]<<endl;
- }
- cout<<endl;
- }
- double minH,maxH;
- minMaxLoc(clusters, &minH, &maxH); //remember must use "&"
- cout<<"H-channel min:"<<minH<<" max:"<<maxH<<endl;
- int* clusters_p = (int*)clusters.data;
- //show label mat
- Mat label(src.size(), CV_32SC1);
- int* label_p = (int*)label.data;
- //assign the clusters to Mat label
- for(i = 0;i < size; i++)
- {
- *label_p = *clusters_p;
- label_p++;
- clusters_p++;
- }
- Mat label_show;
- label.convertTo(label_show,CV_8UC1);
- normalize(label_show,label_show,255,0,CV_MINMAX);
- imshow("label",label_show);
- map<int,int> count; //map<id,num>
- map<int,Vec3f> avg; //map<id,color>
- //compute average color value of one label
- for (int y = 0; y < row; y++)
- {
- const Vec3f* imgData = src.ptr<Vec3f>(y);
- int* idx = label.ptr<int>(y);
- for (int x = 0; x < col; x++)
- {
- avg[idx[x]] += imgData[x];
- count[idx[x]] ++;
- }
- }
- //output the average value (clustering center)
- //计算所得的聚类中心与kmean函数中center的第一列一致,
- //以后可以省去后面这些繁复的计算,直接利用center,
- //但是仍然不理解center的除第一列以外的其他列所代表的意思
- for (i = 0; i < ClusterNum; i++)
- {
- avg[i] /= count[i];
- if (avg[i].val[0]>0&&avg[i].val[1]>0&&avg[i].val[2]>0)
- {
- cout<<i<<": "<<avg[i].val[0]<<" "<<avg[i].val[1]<<" "<<avg[i].val[2]<<" count:"<<count[i]<<endl;
- }
- }
- //show the clustering img;
- Mat showImg(src.size(),CV_32FC3);
- for (int y = 0; y < row; y++)
- {
- Vec3f* imgData = showImg.ptr<Vec3f>(y);
- int* idx = label.ptr<int>(y);
- for (int x = 0; x < col; x++)
- {
- int id = idx[x];
- imgData[x].val[0] = avg[id].val[0];
- imgData[x].val[1] = avg[id].val[1];
- imgData[x].val[2] = avg[id].val[2];
- }
- }
- normalize(showImg,showImg,1,0,CV_MINMAX);
- imshow("show",showImg);
- waitKey();
- return label;
- }
- int main()
- {
- Mat img=imread(filename,1);
- GaussianBlur(img,img,Size(3,3),0);
- img.convertTo(img,CV_32FC3);
- Mat pixId=clustering(img);
- }
学习OpenCV——Kmean(C++)的更多相关文章
- 学习opencv之路(一)
先看一下<学习opencv> 找几个demo 学会相机标定 我做的是单目相机的标定.
- [纯小白学习OpenCV系列]官方例程00:世界观与方法论
2015-11-11 ----------------------------------------------------------------------------------- 其实,写博 ...
- 《学习OpenCV》中求给定点位置公式
假设有10个三维的点,使用数组存放它们有四种常见的形式: ①一个二维数组,数组的类型是CV32FC1,有n行,3列(n×3) ②类似①,也可以用一个3行n列(3×n)的二维数组 ③④用一个n行1列(n ...
- 对人脑处理视觉的描述(摘《学习OpenCV(中文版)》)
人脑将视觉信号划分入很多个通道,将各种不同的信息输入你的大脑.你的大脑有一个关注系统,会根据任务识别出图像的重要部分,并做重点分析,而其他部分则分析得较少 .在人类视觉流中存在大量的反馈,但是目前我们 ...
- 《学习OpenCV》练习题第五章第二题abc
代码: #include <stdio.h> #include <opencv/highgui.h> #include <opencv/cv.h> #include ...
- 《学习OpenCV》练习题第五章第一题ab
这道题是载入一幅带有有趣纹理的图像并用不同的模板(窗口,核)大小做高斯模糊(高斯平滑),然后比较用5*5大小的窗口平滑图像两次和用11*11大小的窗口平滑图像一次是否接近相同. 先说下我的做法,a部分 ...
- 《学习OpenCV》练习题第四章第八题ab
这道题是利用OpenCV例子程序里自带的人脸检测程序,做点图像的复制操作以及alpha融合. 说明:人脸检测的程序我参照了网上现有的例子程序,没有用我用的OpenCV版本(2.4.5)的facedet ...
- 《学习OpenCV》练习题第四章第三题b
#include <highgui.h> #include <cv.h> #include "opencv_libs.h" /* *<学习OpenCV ...
- 《学习OpenCV》练习题第四章第三题a
#include <highgui.h> #include <cv.h> #include "opencv_libs.h" #pragma comment ...
随机推荐
- php安装json模块
centOS上因为看php源码中没有json模块,于是采用pecl自动编译安装:# yum install php-devel# yum install php-pear# yum install g ...
- npm reset config
To reset user defaults Run this in the command line (or git bash on windows): echo "" > ...
- P2296 寻找道路
#include <bits/stdc++.h> using namespace std; const int maxn = 10005; set<int> to[maxn]; ...
- HybridTime - Accessible Global Consistency with High Clock Uncertainty
Amazon's Dynamo [9] and Facebook's Cassandra [13], relax the consistency model,and offer only eventu ...
- ubuntu挂载其他分区到/home下,将当前分区内容替换
有时候,我们装系统时,可能因为没注意,把某一个分区分小了,导致到最后,我们的那个盘容不下了, 这时,面临的两个选择就是:要么卸载一些软件,要么重新分区,重装系统,其实,还可以这样,去把其他 多余的盘分 ...
- C# 操作Cookie类
1.Cookie操作类 using System; using System.Data; using System.Configuration;using System.Web;using Syste ...
- Delphi XE5 与其他版本共存
来源于http://www.board4allcz.eu 作者是gateway 如果你想使用Delphi诸如XE4.XE3.XE2.XE之类的版本跟Delphi XE5共存的话,在cglm.ini中简 ...
- Redis学习笔记(9)-管道/分布式
package cn.com; import java.util.Arrays; import java.util.List; import redis.clients.jedis.Jedis; im ...
- SQLite.net发布后找不到"SQLite.Interop.dll"的问题
http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki sqlite-netFx40-static-binary-b ...
- QComboBox
#include "dialog.h" #include "ui_dialog.h" #include <QtCore> #include < ...