Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

how to use openmp for making parallel kmeans ?

I tried with different directives of openmp, but i can't get difference. Or i get errors. (access violation). I am using ms visual studio 2012 (v110) on windows 8.1 . I tried for basic openmp program after changing in configuration of vs 2012. And i got 8 line output. Without enable openmp support, i got one line output. I placed my code here, Please suggest me , where i can use openmp and how.

#include <opencv/highgui.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
#include <opencv\cv.h>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\imgcodecs.hpp>
#include <opencv2\core\core.hpp>
#include <opencv\cvaux.hpp>
#include <fstream>
#include <ctime>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <windows.h>
#include <atlstr.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <omp.h>

using namespace cv;
using namespace cv::ml;
using namespace std;

void printDim(Mat mat,const char *name)
{
    std::cout<<name<<" :"<<mat.rows<<"x"<<mat.cols<<"  depth :"<<mat.depth()<<"  channel :"<<mat.channels()<<std::endl;
}

void readData(cv::Mat& data, int cl)
{
    vector<const char *> filenames;
    filenames.push_back("Data/ik147_1.txt");
    filenames.push_back("Data/ik147_2.txt");
    filenames.push_back("Data/ik147_3.txt");
    vector<Mat> raw_data(cl);//= new std::vector<cv::Mat>(4);
    int row;
    int i;
    double  min,max;

    #pragma omp parallel for 

    for( i =0;i<cl;i++)
    {
        ifstream file( filenames[i] );
        while( file>>row )
        {
            raw_data[i].push_back(row);
        }
        minMaxLoc(raw_data[i],&min,&max);
      cout<<filenames[i]<<" min :"<<min<<", max :"<<max<<std::endl;

    }
    int N=raw_data[0].rows;
   // cv::Mat data(N,3,CV_32FC1);
    cout<<"Number of data (row) : "<< N << endl;
    int columns_to_read=cl;
    data.create(N,columns_to_read,CV_32FC1);

    //#pragma omp parallel for private(i) 
    for( i=0;i<columns_to_read;i++)
    {
        raw_data[i](Rect(0,0,1,N)).copyTo(data(Rect(i,0,1,N)));
    }

}

void resize1(Mat src, string name, string name1)
{
    Size size(512,512);//the dst image size,e.g.100x100
    Mat dst;
    resize(src,dst,size);//resize image
    //cout<<"" <<endl;
    imshow(name,dst);
    imwrite(name1,dst);
}

int main()
{

    int fn;
    int width, height;
    cout << "Enter number of read files :" << endl;
    cin>> fn;
    cout<< "   "<< endl;

    cout << "Enter width and height :" << endl;
    cin>> width >> height;
    cout<< "   "<< endl;
    cout << "Image has width : " <<width <<" and height : " << height << endl;
    cout<< "   "<< endl;


    LARGE_INTEGER frequency;
    LARGE_INTEGER start;
    LARGE_INTEGER end;
    double interval;

    QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&start);

    Mat image = Mat::zeros(height, width, CV_8UC3);

    Mat data1; 
    readData(data1, fn);
    printDim(data1,"data1");
    cout << "----------------------- " << endl;

    Mat data=data1.clone();
    printDim(data,"data");
    data1.release();
    printDim(data1,"release data1 ");

    Mat train_data1 = data.reshape(fn, width );      // Mat train_data1 = data.reshape(3, 512 );
    printDim(train_data1, "Dimension of train_data1");

    double Min, Max;
    minMaxLoc(train_data1, &Min, &Max);

    Mat image1;
    train_data1.convertTo(image1,CV_8UC3);

    imwrite("3channel.jpg",image1);
    resize1(image1,"source","source.jpg");

    int nthreads;
    int i;
    int N=data.rows; //number of data points
    cout << "N of vv rows:"<< N << endl;
    cout <<"  "<<endl;
    int K=10;
    int clusterCount=K;
    int sampleCount = N;
    Mat labelsMatKMeans;
    Mat centers;
    /*#pragma omp parallel 
    {
    #pragma omp sections
    {
    #pragma omp section 
    {*/

    kmeans(data, clusterCount, labelsMatKMeans,
               TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 0.1),
               3, KMEANS_PP_CENTERS, centers);
    /*}
    }
    }*/

        cout<< "  " << endl;
        cout<< "final center :"<<endl;
        printDim(centers,"Dim of centers");

        cout<< "  " << endl;
        cout<< "labels :"<<endl;
        printDim(labelsMatKMeans,"Dim of labels");
        cout<< "  " << endl;

        for(int  y = 0; y < height; y++ )

      //#pragma omp for
            for(int  x = 0; x < width; x++ )
        {
          //    cout<< "("<<y<<" , " << x <<") :"<<" test 1 " << endl;
            int cluster_idx = labelsMatKMeans.at<int>(x + y*512,0);

            //      int cluster_idx = labelsMatKMeans.at<int>(y);
            img_kmeans.at<Vec3b>(y,x)[0] = centers.at<float>(cluster_idx, 0);

            img_kmeans.at<Vec3b>(y,x)[1] = centers.at<float>(cluster_idx, 1);
            img_kmeans.at<Vec3b>(y,x)[2] = centers.at<float>(cluster_idx, 2);

       }



        printDim(img_kmeans,"img_kmeans");
    //    imshow("K-Means clustering", img_kmeans);
        imwrite("k_means.png",img_kmeans);
        resize1(img_kmeans, "dst","dst.png");
    QueryPerformanceCounter(&end);
    interval = (double) (end.QuadPart - start.QuadPart) / frequency.QuadPart;

    cout<<"Time taken : "<< interval << endl;
        waitKey(0);
}