k-means提取图像主色(调用opencv-kmeans)
2017-07-23 14:51
459 查看
一、Kmeans简介
见k-means提取图像主色(手写)。
二、opencv-kmeans主色提取
见k-means提取图像主色(手写)。
二、opencv-kmeans主色提取
//输入彩色图像 k-means获取主色(颜色中心及比例) void DCD(Mat& src) { if(!src.data) { return; } //分割通道 vector<Mat> bgr; split(src, bgr); int nRows = src.rows; int nCols = src.cols; //设定类数 int nClusters = 20; //输入样本 按行存储 Mat samples(nRows*nCols, 1, CV_32FC3/*, Scalar(0)*/); for(int i = 0; i < nRows; ++i) { for(int j = 0; j < nCols; ++j) { for(int iChanel = 0; iChanel < 3; ++iChanel) { samples.at<Vec3f>(i*nCols + j)[iChanel] = bgr[iChanel].at<uchar>(i, j); } } } TermCriteria criteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 1000, FLT_EPSILON); Mat centers(nClusters,1, CV_32FC3); Mat labels(nRows*nCols, 1, CV_8UC1); //进行聚类 kmeans(samples, nClusters, labels, criteria, 1, KMEANS_PP_CENTERS, centers); //分割后的颜色 Mat res(nRows, nCols, CV_8UC3); //每个类别的颜色中心 Vec3f *p = new Vec3f[nClusters]; cout<< "颜色聚类中心"<< endl; for(int i = 0; i < nClusters; ++i) { p[i] = centers.ptr<Vec3f>(i)[0]; cout<<p[i].val[0]<< " "<< p[i].val[1]<<" "<< p[i].val[2]<< endl; } int dis = nRows/nClusters; for(int i = 0; i < nClusters; ++i) { for(int r = i*dis; r < (i + 1)*dis; ++r) { for(int c = 0; c < nCols; ++c) { *res.ptr<Vec3b>(r, c) = p[i]; } } } imshow("res", src); imshow("color", res); //每个类别所占的比例 float* precent = new float[nClusters]; memset(precent, 0, sizeof(p)*nClusters); for(int n = 0; n < nRows*nCols; ++n) { int r = n/nCols; int c = n%nCols; *res.ptr<Vec3b>(r, c) = p[*labels.ptr<uchar>(n, 0)]; precent[*labels.ptr<uchar>(n, 0)] ++; } cout<< "相应颜色比例"<< endl; for(int i = 0; i < nClusters; ++i) { precent[i]/=(nRows*nCols); cout<<precent[i]<<" "; } cout<<endl; waitKey(0); }三、程序结果
相关文章推荐
- OpenCV-调用USB摄像机获取视频流,ROI特定提取,图像变换
- k-means提取图像主色(手写)
- opencv下调用K均值函数cvKMeans2聚类图像例程
- 【图像特征提取8】OpenCv中SURF源码的分析一
- 关于opencv中imshow()函数在VS中调用不显示图像的问题
- python-opencv在有噪音的情况下提取图像的轮廓实例
- OpenCV3_C++_Akaze()图像的特征点提取 实例
- Atitit 图像处理 调用opencv 通过java api attilax总结
- OpenCV 实践——人脸检测与人脸图像提取
- asp.net调用opencv类库,实现图像处理显示
- 使用opencv提取RGB图像的三个通道,并显示
- linux中配置在qt中调用opencv图像库
- 【OpenCV3图像处理】提取轮廓的凸包、外包矩形、最小外包矩形、最小外包圆
- Atitit 图像处理 调用opencv 通过java api attilax总结
- 【图像识别】 Python+Opencv调用摄像头实现人脸识别并保存视频
- opencv图像角点的提取
- opencv提取图像边缘特征sobel算子的运用
- java通过jni调用opencv处理图像
- opencv提取截获图像,任意区域
- Opencv中提取图像中的轮廓及中心点