Ask Your Question

Revision history [back]

I hope it helps

#include <opencv2/opencv.hpp> 

using namespace std;
using namespace cv;


void main(void)
{
    Mat img=imread("14780105241945453.png",IMREAD_COLOR);
    cout << "Pixels "<<img.rows*img.cols<<"\n";
    Mat src,srcF;
    cvtColor(img, src, CV_BGR2Lab);
    src.convertTo(srcF, CV_32FC3);
    cout << "Pixels " << srcF.rows*srcF.cols << "\n";
    vector<Vec3f> plan;
    plan.assign((Vec3f*)srcF.datastart, (Vec3f*)srcF.dataend);
    cout << "Pixels " << plan.size() << "\n";
    int clusterCount = 3;
    Mat labels;
    Mat centers;
    kmeans(plan, clusterCount,labels,TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 1.0), 3, KMEANS_PP_CENTERS, centers);
    Mat mask;
    namedWindow("Original", WINDOW_NORMAL);
    imshow("Original", img);
    for (int i = 0; i < clusterCount; i++)
    {
        cv::Mat cloud = (labels == i) ;
        namedWindow(format("Cluster %d",i), WINDOW_NORMAL);
        Mat result = Mat::zeros(img.rows, img.cols, CV_8UC3);
        if (cloud.isContinuous())
            mask = cloud.reshape(0, img.rows);
        else
            cout << "error";
        img.copyTo(result, mask);
        imshow(format("Cluster %d", i), result);

    }
    waitKey();
}

with your image results are cluster 0 image description

cluster 1 (black on black) :image description

and cluster 2 image description

I hope it helps

#include <opencv2/opencv.hpp> 

using namespace std;
using namespace cv;


void main(void)
{
    Mat img=imread("14780105241945453.png",IMREAD_COLOR);
    cout << "Pixels "<<img.rows*img.cols<<"\n";
    Mat src,srcF;
    cvtColor(img, src, CV_BGR2Lab);
    src.convertTo(srcF, CV_32FC3);
    cout << "Pixels " << srcF.rows*srcF.cols << "\n";
    vector<Vec3f> plan;
    plan.assign((Vec3f*)srcF.datastart, (Vec3f*)srcF.dataend);
    cout << "Pixels " << plan.size() << "\n";
    int clusterCount = 3;
    Mat labels;
    Mat centers;
    kmeans(plan, clusterCount,labels,TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 1.0), 3, KMEANS_PP_CENTERS, centers);
    Mat mask;
    namedWindow("Original", WINDOW_NORMAL);
    imshow("Original", img);
    for (int i = 0; i < clusterCount; i++)
    {
        cv::Mat cloud = (labels == i) ;
        namedWindow(format("Cluster %d",i), WINDOW_NORMAL);
        Mat result = Mat::zeros(img.rows, img.cols, CV_8UC3);
        if (cloud.isContinuous())
            mask = cloud.reshape(0, img.rows);
        else
            cout << "error";
        img.copyTo(result, mask);
        imshow(format("Cluster %d", i), result);

    }
    waitKey();
}

with your image results are cluster 0 image description

cluster 1 (black on black) :image description ( with this original image descriptionyou have image description but it becomes cluster 2....)

and cluster 2 image description

I hope it helps

#include <opencv2/opencv.hpp> 

using namespace std;
using namespace cv;


void main(void)
{
    Mat img=imread("14780105241945453.png",IMREAD_COLOR);
    cout << "Pixels "<<img.rows*img.cols<<"\n";
    Mat src,srcF;
    cvtColor(img, src, CV_BGR2Lab);
    src.convertTo(srcF, CV_32FC3);
    cout << "Pixels " << srcF.rows*srcF.cols << "\n";
    vector<Vec3f> plan;
    plan.assign((Vec3f*)srcF.datastart, (Vec3f*)srcF.dataend);
    cout << "Pixels " << plan.size() << "\n";
    int clusterCount = 3;
    Mat labels;
    Mat centers;
    kmeans(plan, clusterCount,labels,TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 1.0), 3, KMEANS_PP_CENTERS, centers);
    Mat mask;
    namedWindow("Original", WINDOW_NORMAL);
    imshow("Original", img);
 int maxCluster=0,ind=-1;
for (int i = 0; i < clusterCount; i++)
 {
     cv::Mat cloud = (labels == i) ;
     namedWindow(format("Cluster %d",i), WINDOW_NORMAL);
     Mat result = Mat::zeros(img.rows, img.cols, CV_8UC3);
      if (cloud.isContinuous())
         mask = cloud.reshape(0, img.rows);
     else
         cout << "error";
    int m=countNonZero(mask);
    if (m > maxCluster)
    {
        maxCluster = m;
        ind=i;
    }
    img.copyTo(result, mask);
     imshow(format("Cluster %d", i), result);
     imwrite(format("Cluster%d.png", i), result);

}
 cout<<"Cluster max is "<<ind<<" with "<<maxCluster<<" pixels";
waitKey();
}

with your image results are cluster 0 image description

cluster 1 (black on black) :image description ( with this original image descriptionyou have image description but it becomes cluster 2....)

and cluster 2 image description