如何应用K表示图像的掩码而不是整个图像

时间:2015-03-13 16:47:59

标签: opencv cluster-analysis k-means

我想在OpenCV上使用K Means表示图像区域不是正方形或矩形。例如,源图像是:

enter image description here

现在我选择一个自定义面具:

enter image description here

并应用K表示K = 3:

enter image description here

显然没有考虑边界(白色)。

相反,我可以用OpenCV做的是K Means但考虑到了界限:

enter image description here

这会弄乱我的最终形象,因为黑色被认为是一种颜色。

你有什么线索吗?

提前谢谢。

1 个答案:

答案 0 :(得分:3)

快速而肮脏的解决方案。

vector<Vec3b> points;
vector<Point> locations;
for( int y = 0; y < src.rows; y++) {
    for( int x = 0; x < src.cols; x++) {
        if ( (int)mask.at<unsigned char>(y,x) != 0 ) {
            points.push_back(src.at<Vec3b>(y,x));
            locations.push_back(Point(x,y));
        }
    }   
}
Mat kmeanPoints(points.size(), 3, CV_32F);
for( int y = 0; y < points.size(); y++ ) {
    for( int z = 0; z < 3; z++) {
        kmeanPoints.at<float>(y, z) = points[y][z];
    }
}

Mat labels;
Mat centers;
kmeans(kmeanPoints, 4, labels, TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 10, 0.1), 10, cv::KMEANS_PP_CENTERS, centers);
Mat final = Mat::zeros( src.size(), src.type() );
Vec3b tempColor;

for(int i = 0; i<locations.size(); i++) {
    int cluster_idx = labels.at<int>(i,0);
    tempColor[0] = centers.at<float>(cluster_idx, 0);
    tempColor[1] = centers.at<float>(cluster_idx, 1);
    tempColor[2] = centers.at<float>(cluster_idx, 2);
    final.at<Vec3b>(locations[i]) = tempColor;
}
相关问题