Ask Your Question

Revision history [back]

I hanen't check channels but it works for lena

cv::Mat image = cv::imread("f:/lib/opencv/samples/data/lena.jpg");
cv::Mat fImage;
image.convertTo(fImage, CV_32F);
fImage = fImage.reshape(3, image.cols * image.rows);
cv::Mat labels;
cv::Mat centers;
cv::kmeans(fImage, 10, labels, cv::TermCriteria(), 3, cv::KMEANS_RANDOM_CENTERS, centers);
cout<<centers<<endl;
cv::Mat segmented = labels.reshape(1, image.rows);
    Mat im = Mat::zeros(image.size(),CV_8UC3);
    vector<Mat> plan;
    split(im,plan);

    for (int i = 0; i < 10; i++)
{
    Mat mask=(segmented==i);
    Vec3b v(centers.at<float>(i,0),centers.at<float>(i,1),centers.at<float>(i,2));
    for (int j = 00; j<plan.size();j++)
    add(plan[j], v[j],plan[j],mask);

}
    merge(plan,im);
segmented.convertTo(segmented, CV_8UC3);
imshow("Segmented color",im);
imshow("Segmented",segmented);
imwrite("segemented.png",segmented);
waitKey();

I hanen't haven't check channels but it works for lena

cv::Mat image = cv::imread("f:/lib/opencv/samples/data/lena.jpg");
cv::Mat fImage;
image.convertTo(fImage, CV_32F);
fImage = fImage.reshape(3, image.cols * image.rows);
cv::Mat labels;
cv::Mat centers;
cv::kmeans(fImage, 10, labels, cv::TermCriteria(), 3, cv::KMEANS_RANDOM_CENTERS, centers);
cout<<centers<<endl;
cv::Mat segmented = labels.reshape(1, image.rows);
    Mat im = Mat::zeros(image.size(),CV_8UC3);
    vector<Mat> plan;
    split(im,plan);

    for (int i = 0; i < 10; i++)
{
    Mat mask=(segmented==i);
    Vec3b v(centers.at<float>(i,0),centers.at<float>(i,1),centers.at<float>(i,2));
    for (int j = 00; j<plan.size();j++)
    add(plan[j], v[j],plan[j],mask);

}
    merge(plan,im);
segmented.convertTo(segmented, CV_8UC3);
imshow("Segmented color",im);
imshow("Segmented",segmented);
imwrite("segemented.png",segmented);
waitKey();

I haven't check channels but it works seems working for lena

cv::Mat image = cv::imread("f:/lib/opencv/samples/data/lena.jpg");
cv::Mat fImage;
image.convertTo(fImage, CV_32F);
fImage = fImage.reshape(3, image.cols * image.rows);
cv::Mat labels;
cv::Mat centers;
cv::kmeans(fImage, 10, labels, cv::TermCriteria(), 3, cv::KMEANS_RANDOM_CENTERS, centers);
cout<<centers<<endl;
cv::Mat segmented = labels.reshape(1, image.rows);
    Mat im = Mat::zeros(image.size(),CV_8UC3);
    vector<Mat> plan;
    split(im,plan);

    for (int i = 0; i < 10; i++)
{
    Mat mask=(segmented==i);
    Vec3b v(centers.at<float>(i,0),centers.at<float>(i,1),centers.at<float>(i,2));
    for (int j = 00; j<plan.size();j++)
    add(plan[j], v[j],plan[j],mask);

}
    merge(plan,im);
segmented.convertTo(segmented, CV_8UC3);
imshow("Segmented color",im);
imshow("Segmented",segmented);
imwrite("segemented.png",segmented);
waitKey();
click to hide/show revision 4
No.4 Revision

I haven't check channels but it seems working for lena

cv::Mat image = cv::imread("f:/lib/opencv/samples/data/lena.jpg");
cv::Mat fImage;
image.convertTo(fImage, CV_32F);
fImage = fImage.reshape(3, image.cols * image.rows);
cv::Mat labels;
cv::Mat centers;
cv::kmeans(fImage, 10, labels, cv::TermCriteria(), 3, cv::KMEANS_RANDOM_CENTERS, centers);
cout<<centers<<endl;
cv::Mat segmented = labels.reshape(1, image.rows);
    Mat cv::Mat im = Mat::zeros(image.size(),CV_8UC3);
    vector<Mat> cv::Mat::zeros(image.size(), CV_8UC3);
std::vector< cv::Mat > plan;
    split(im,plan);
cv::split(im, plan);
  for (int i = 0; i < 10; centers.rows; i++)
{
    Mat mask=(segmented==i);
cv::Mat mask = (segmented == i);
 Vec3b v(centers.at<float>(i,0),centers.at<float>(i,1),centers.at<float>(i,2));
cv::Vec3b v(centers.at<float >(i, 0), centers.at< float >(i, 1), centers.at< float >(i, 2));
    for (int j = 00; j<plan.size();j++)
0; j < plan.size(); j++)
 add(plan[j], v[j],plan[j],mask);

}
 merge(plan,im);
cv::add(plan[j], v[j], plan[j], mask);    
}
cv::merge(plan, im);
segmented.convertTo(segmented, CV_8UC3);
imshow("Segmented color",im);
imshow("Segmented",segmented);
imwrite("segemented.png",segmented);
waitKey();
cv::imshow("Segmented color", im);
cv::imshow("Segmented", segmented);
cv::imwrite("segemented.png", segmented);
cv::waitKey();
click to hide/show revision 5
No.5 Revision

I haven't check channels but it seems working for lena

cv::Mat image = cv::imread("f:/lib/opencv/samples/data/lena.jpg");
cv::Mat fImage;
image.convertTo(fImage, CV_32F);
fImage = fImage.reshape(3, image.cols * image.rows);
cv::Mat labels;
cv::Mat centers;
cv::kmeans(fImage, 10, labels, cv::TermCriteria(), 3, cv::KMEANS_RANDOM_CENTERS, centers);
cout<<centers<<endl;
cv::Mat segmented = labels.reshape(1, image.rows);
cv::Mat im = cv::Mat::zeros(image.size(), CV_8UC3);
std::vector< cv::Mat > plan;
cv::split(im, plan);

for (int i = 0; i < centers.rows; i++)
{
    cv::Mat mask = (segmented == i);
    cv::Vec3b v(centers.at<float v(centers.at< float >(i, 0), centers.at< float >(i, 1), centers.at< float >(i, 2));
    for (int j = 0; j < plan.size(); j++)
        cv::add(plan[j], v[j], plan[j], mask);    
}
cv::merge(plan, im);
segmented.convertTo(segmented, CV_8UC3);
cv::imshow("Segmented color", im);
cv::imshow("Segmented", segmented);
cv::imwrite("segemented.png", segmented);
cv::waitKey();

I haven't check channels but it seems working for lena

cv::Mat image = cv::imread("f:/lib/opencv/samples/data/lena.jpg");
cv::Mat fImage;
image.convertTo(fImage, CV_32F);
fImage = fImage.reshape(3, image.cols * image.rows);
cv::Mat labels;
cv::Mat centers;
cv::kmeans(fImage, 10, labels, cv::TermCriteria(), 3, cv::KMEANS_RANDOM_CENTERS, centers);
cout<<centers<<endl;
cv::Mat segmented = labels.reshape(1, image.rows);
cv::Mat im = cv::Mat::zeros(image.size(), CV_8UC3);
std::vector< cv::Mat > plan;
cv::split(im, plan);

for (int i = 0; i < centers.rows; i++)
{
    cv::Mat mask = (segmented == i);
    cv::Vec3b v(centers.at< float >(i, 0), centers.at< float >(i, 1), centers.at< float >(i, 2));
    for (int j = 0; j < plan.size(); j++)
        cv::add(plan[j], v[j], plan[j], mask); plan[j].setTo(v[j],mask);    
}
cv::merge(plan, im);
segmented.convertTo(segmented, CV_8UC3);
cv::imshow("Segmented color", im);
cv::imshow("Segmented", segmented);
cv::imwrite("segemented.png", segmented);
cv::waitKey();

I haven't check channels but it seems working for lena

cv::Mat image = cv::imread("f:/lib/opencv/samples/data/lena.jpg");
cv::Mat fImage;
image.convertTo(fImage, CV_32F);
fImage = fImage.reshape(3, image.cols * image.rows);
cv::Mat labels;
cv::Mat centers;
cv::kmeans(fImage, 10, labels, cv::TermCriteria(), 3, cv::KMEANS_RANDOM_CENTERS, centers);
cout<<centers<<endl;
cv::Mat segmented = labels.reshape(1, image.rows);
cv::Mat im = cv::Mat::zeros(image.size(), CV_8UC3);
std::vector< cv::Mat > plan;
cv::split(im, plan);

for (int i = 0; i < centers.rows; i++)
{
    cv::Mat mask = (segmented == i);
    cv::Vec3b v(centers.at< float >(i, 0), centers.at< float >(i, 1), centers.at< float >(i, 2));
    for (int j = 0; j < plan.size(); j++)
        plan[j].setTo(v[j],mask); im.setTo(v,mask);    
}
cv::merge(plan, im);
segmented.convertTo(segmented, CV_8UC3);
cv::imshow("Segmented color", im);
cv::imshow("Segmented", segmented);
cv::imwrite("segemented.png", segmented);
cv::waitKey();