kmeans clustering [closed]

asked 2014-12-03 13:05:42 -0600

  1. ---------- #include "opencv/cv.h" // include it to used Main OpenCV functions. #include "opencv/highgui.h" #include "opencv2/highgui/highgui.hpp"

    include <opencv2 imgproc="" imgproc.hpp="">

include <opencv2\core\core.hpp> #include <opencv2\core\mat.hpp> #include <vector> #include <iostream> #include <fstream>

include <string> #include <complex> #include <cmath>

include <algorithm> #include "opencv/cxcore.h" #define ClusterNum 6 using namespace cv; using namespace std;

   int main( int argc, const char** argv    ) {  Mat    imgMat,im,im1,im2,im3,im4,im5,im6,temp,im11,im12,im13,im14,imi,imd,imr; float a,b,c,d;  Mat im_gray =    imread("mdb012.pgm");//im_gray =    cvLoadImage("mdb012.pgm",CV_LOAD_IMAGE_GRAYSCALE); imshow("image", im_gray);     resize(im_gray, imgMat, Size(512,    512), 0, 0, INTER_CUBIC);     //cvSetImageROI(im_gray, cvRect(0, 0,    512, 512));  //cvShowImage("image_1",    im_gray);  //int height, width;     //height =  im_gray->height;  //width    =  im_gray->width;

    for(int i=0;i<(imgMat.rows);i++)  {      for(int j=0;j<(imgMat.cols);j++)
 {
      if( imgMat.at<uchar>(i,j)>176)   
      {
             imgMat.at<uchar>(i,j)=1;
      }      }  }  imshow("image_2",imgMat);     namedWindow( "Display window",    WINDOW_AUTOSIZE );// Create a window for display.  std::cout << "image: " << imgMat.rows << ", " << imgMat.cols    << std::endl; assert(imgMat.type() ==    CV_8UC3); cv::imshow("image",    imgMat); cv::Mat reshaped_image =    imgMat.reshape(1, imgMat.cols *    imgMat.rows); std::cout << "reshaped image: " << reshaped_image.rows << ",    " << reshaped_image.cols <<    std::endl;    assert(reshaped_image.type() ==    CV_8UC1);

cv::Mat reshaped_image32f;     reshaped_image.convertTo(reshaped_image32f, CV_32FC1, 1.0 / 255.0);  std::cout <<    "reshaped image 32f: " <<    reshaped_image32f.rows << ", " <<    reshaped_image32f.cols << std::endl; assert(reshaped_image32f.type() ==   CV_32FC1);
         cv::Mat labels; int cluster_number = 10; cv::TermCriteria    criteria (cv::TermCriteria::COUNT,    512, 1); cv::Mat centers;    cv::kmeans(reshaped_image32f,    cluster_number, labels, criteria, 1, cv::KMEANS_RANDOM_CENTERS, centers);
   std::cout << "===\n"; std::cout <<    "labels: " << labels.rows << " " <<    labels.cols << std::endl; std::cout    << "centers: " << centers.rows << " "   << centers.cols << std::endl;
       assert(labels.type() == CV_32SC1);
       assert(centers.type() == CV_32FC1);

       //cv::Mat rgb_image(imgMat.rows, imgMat.cols,  CV_8UC3);
       cv::MatIterator_<cv::Vec3b> rgb_first =    imgMat.begin<cv::Vec3b>();
       cv::MatIterator_<cv::Vec3b> rgb_last

= imgMat.end<cv::vec3b>(); cv::MatConstIterator_<int> label_first = labels.begin<int>();

       cv::Mat centers_u8;
       centers.convertTo(centers_u8, CV_8UC1, 255.0);
       cv::Mat centers_u8c3 = centers_u8.reshape(3);

       while ( rgb_first != rgb_last )          {
               const cv::Vec3b rgb = centers_u8c3.ptr<Vec3b>(*label_first)[4];

               *rgb_first = rgb;
               ++rgb_first;
               ++label_first;
       }

       cv::imshow("tmp", imgMat);   

waitKey(0);   return 0;  }
edit retag flag offensive reopen merge delete

Closed for the following reason not a real question by FooBar
close date 2014-12-04 07:15:14.170948