Ask Your Question

sumit patel's profile - activity

2019-04-19 11:45:48 -0600 received badge  Notable Question (source)
2019-04-19 11:45:48 -0600 received badge  Popular Question (source)
2016-04-04 03:59:20 -0600 commented question how to use openmp for making parallel kmeans ?

In readData function , i try openmp at first for loop but accees violation error is occured. In main function , I placed kmeans() function in for loop and i try #pragma omp parallel for . It takes less time than kmeans in for loop without openmp.

2016-04-04 02:22:42 -0600 asked a question 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 ...
(more)
2016-03-11 02:03:01 -0600 commented question How to display decision region for svm prediction ?

I upload whole code here. Please suggest me to which kind of information is required? After generating random numbers, i find row and column numbers than I store pixels value in vector at that location(row ,column). And store vector in mat and use it for train svm.

2016-03-11 01:12:02 -0600 commented question How to display decision region for svm prediction ?

@berak, how to apply your suggestion? for image which i added in question, i want to know , what is the output of svm prediction (how will decision region ) ?

2016-03-10 11:40:36 -0600 commented question How to display decision region for svm prediction ?

how to make different response for all pixels. i will remove degree property.

2016-03-10 11:39:01 -0600 commented question How to display decision region for svm prediction ?

i have 78642 by 3 dimension data. i don't know my samplemat is right or wrong. I tried different thing, only this thing give me output as shown. please give me idea to change response or to get proper response.

2016-03-10 06:16:23 -0600 asked a question How to display decision region for svm prediction ?

I follow opencv documentation. I have four txt file (512 by 512 type:double) They are channels value.I cannot upload them here.. They are generated with envy software. I combine three files and made data of 262144 by 3 dimension. I generate image from it.

image

function for read data :

  void readData(cv::Mat& data)
    {
        std::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");
      //  filenames.push_back("Data/ik147_4.txt");
        string line;
        vector<Mat> raw_data(cl);//= new std::vector<cv::Mat>(4);
        int row;
        double  min,max;
        for(int 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);

        for(int i=0;i<columns_to_read;i++)
        {
            raw_data[i](Rect(0,0,1,N)).copyTo(data(Rect(i,0,1,N)));
        }

    }
int main()
{
int width = 512, height = 512;
    Mat image = Mat::zeros(height, width, CV_8UC3);
    Mat img = Mat::zeros(height, width, CV_8UC3);
    Mat img_kmeans = Mat::zeros(height, width, CV_8UC3);
//  Mat image3 = Mat::zeros(height, width, CV_8UC3);

    Mat data, train_data11, test_data11;
    readData(data);
    printDim(data,"data");
    //print(mat1,3);                                                 // direct reshape train to 512 by 512//
//  printDim(mat1, "Dimension of Mat1");
    Mat train_data1 = data.reshape(3, 512 );      //    Mat train_data1 = data.reshape(3, 512 );
    printDim(train_data1, "Dimension of train_data1");
    Mat image1;

    double Min, Max;
    minMaxLoc(train_data1, &Min, &Max);
    //  train_data.convertTo(train_data,CV_8UC3);
    train_data1.convertTo(image1,CV_8U,255.0/(Max-Min),-255.0/Min);

    printDim(image1,"Dimension of Image");
    //Mat img1 = image1.reshape(3, 512 );
    //printDim(img1,"Dimension of Image");
    imshow( "window_name", image1 );
    imwrite("3channel.jpg",image1);


    /*For take 30% of total data, i randomly generate numbers and with help of it, i store pixel values of image in other matrix(78642 by 3) . Than perform kmeans for generating labels.  I use 10 clusters. Than I perform training phase.* /

            int N1=78642;
        Mat location(N1, 1, CV_32FC1);

           RNG rng((unsigned)time(NULL)); 
        RNG rng_center(20); 


            //RNG rng(0xFFFF);

            cout<<"New 1 :"<< endl;
            //rng.fill(location, RNG::UNIFORM, Scalar(0,5), Scalar(4,8));
            rng.fill(location, RNG::UNIFORM, 0, 1);
            //print(location,3);

            cout<<"New 2 :"<< endl;
            location=78642 * location;
            //print(location,3);


            for(int i=0;i<N1;++i)
            {
            location.at<float>(i,0)= cvCeil(location.at<float>(i,0));

           // labelsMat.at<int>(i)= cvCeil(labelsMat.at<int>(i));
        }
            location=abs(location); 

            cout<<"New 2 :"<< endl;
            //print(location,3);


            //cout << "test of .data : "<< (int)location.data << endl;

            Mat image3 = imread("3channel.jpg", 1);
            imshow("value test",image3);
            vector<int> c1;
            vector<int> r1;
        //  int i,j;
            for(int i=0;i<location.rows;i++)
                //for(int j=0; j<image.cols; j++ )
                {
                //  cout<< "value of t : "<< location.at<float>(i,0) << endl;
                    int t=location.at<float>(i,0);
            //      cout<< "value of t.1 : "<< t << endl;
                    int s ...
(more)
2016-03-08 05:58:39 -0600 commented question How to train my data only once

My data's dimension is 262144 by 3. I use 78642 train data and 78642 labels. I done training and prediction. I am not sure about, it is right or wrong. I want to display decision region. As per this code ,variable response is used for decision region. So how can i display it?

2016-02-19 01:18:20 -0600 commented question how to load matrix from text file(.txt) as float values

after reshape i want display matrix as image , how to do that?

2016-02-13 06:21:19 -0600 received badge  Enthusiast
2016-02-11 08:45:50 -0600 commented question how to use centers generated by kmeans in svm training in opencv ?

I searched already but i dont know about hog and bow and extractor. so i post this questionand also I am using svm. in svm I can generate clusters (5 in my case) . but for that I want to use centers which are generated from code which I posted. if I print centers matrix (in kmeans function ) , I got new value which are different from intial.

2016-02-11 01:28:54 -0600 asked a question how to use centers generated by kmeans in svm training in opencv ?

I define initial centers and use some data, than perform kmeans. I also print new centers , they are different from my initial centers. Here, I added code of that. I want to use these centers in svm training. Give me idea or add example code for that. For data set, I have text files, in those data are 512 x 512. I create data from text files with this dimension (262144 (row) x 2 (cols) ).

int N=train_data.rows; //number of data points
 //   
    int K=5; //number of labels


    RNG rng((unsigned)time(NULL)); 
    RNG rng_center(20); 

  Mat labelsMat(N,1,CV_32SC1);
      Mat labelsMatKMeans;

    int clusterCount=K;
    int sampleCount = N;
    Mat centers(5,cl,CV_32FC1);
    readCenters(centers,"centers.txt");

    Point center;
    center.x = 0;//rng_center.uniform(0, height);
    center.y = 0;//rng_center.uniform(0, width);

    // Set up training data
    /* generate random labeld sample from multigaussian distribution */ 
    for( int k = 0; k < clusterCount; ++k )
    {
        Mat pointChunk = train_data.rowRange(k*sampleCount/clusterCount,
                                            k == clusterCount - 1 ? sampleCount :
                                            (k+1)*sampleCount/clusterCount);
        rng.fill(pointChunk, CV_RAND_NORMAL, Scalar(center.x, center.y), Scalar(width*0.05, height*0.05));

        Mat repeat_center; 
        repeat(centers.row(k),pointChunk.rows,1,repeat_center);
        ////cout<<repeat_center;
        //pointChunk=pointChunk+repeat_center;

        Mat labelChunk = labelsMat.rowRange(k*sampleCount/clusterCount,
                                            k == clusterCount - 1 ? sampleCount :
                                            (k+1)*sampleCount/clusterCount);

        labelChunk=k;
    }

    for(int i=0;i<N;++i)
    {
        train_data.at<float>(i,0)= cvCeil(train_data.at<float>(i,0));
        train_data.at<float>(i,1)= cvCeil(train_data.at<float>(i,1));
    //  train_data.at<float>(i,2)= cvCeil(train_data.at<float>(i,2));

        labelsMat.at<int>(i)= cvCeil(labelsMat.at<int>(i));
    }
    printDim(labelsMat,"labelMat");
     train_data=abs(train_data); 
    //absdiff(testDataMat,Scalar::all(0),testDataMat);

       vector<Vec3b> colors_region;
     vector<Vec3b> colors_data;
    //generate random colors for each label
    for(int i=0;i<K;++i)
    {
        int icolor = (unsigned) rng;
        colors_region.push_back( Vec3b( icolor&255, (icolor>>8)&255, (icolor>>16)&255 ));
        icolor=(unsigned)rng;
        colors_data.push_back( Vec3b( icolor&255, (icolor>>8)&255, (icolor>>16)&255 ));

    }

  //draw k-means output
          kmeans(train_data, clusterCount, labelsMatKMeans,
               TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0),
               3, KMEANS_PP_CENTERS, centers);

         // cout << "M = "<< endl << " "  << labelsMatKMeans << endl << endl;
          cout<< "Final center :"<<endl;
          print(centers, 3);
          cout<< "  " << endl;

    /*     while( file>>row>>col )
    {
        centers.at<float>(i,0)=row;
        centers.at<float>(i,1)=col;
        i++;
        cout<<"center "<< i <<": ("<< row <<","<< col << ")" <<endl;
    }*/

        img_kmeans = Scalar::all(0);

        for( int i = 0; i < sampleCount; i++ )
        {
            int clusterIdx = labelsMatKMeans.at<int>(i);

            //cout << "C_ID : " << labelsMatKMeans.at<float>(i);
            //cout << "C_ID : " << clusterIdx<< endl;
            Point ipt ;
            ipt.x= (int)train_data.at<float>(i,0);
            ipt.y=(int)train_data.at<float>(i,1);
            circle( img_kmeans, ipt, 5, Scalar(colors_data[clusterIdx]), -1, 8 );
             }

        imshow("K-Means clustering", img_kmeans);
        imwrite("k_means.png",img_kmeans);
2016-02-01 03:15:40 -0600 asked a question opencv error : sizes of input arguments do not match

opencv error : sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array') in arithm_op, c:\opencv\modules\core\src\arithm.cpp line 1987

I can get output upto cout << "error" << ; then program threw error. in test_data i use 3 columns. Regarding it, I need to make changes in further part. Please suggest me , where have I need to changes and how ? code:

//test_data=78644 x 3 depth=5 channel= 1

int N=test_data.rows; //number of data points
    int K=5;  //number of labels

    RNG rng((unsigned)time(NULL)); 
    RNG rng_center(20);
    //Mat testDataMat(N, 2, CV_32FC1);
    int clusterCount=K;
    int sampleCount = N;
    Mat centers(5,2,CV_32FC1);
    readCenters(centers,"centers.txt");

    Mat labelsMat(N,1,CV_32SC1);
    Mat labelsMatKMeans;

    cout << " value of defined labelsMatKmeans : \n " << labelsMatKMeans << endl;
    cout<< "  " << endl;

    Mat img_kmeans(512, 512, CV_8UC3);

    Point center;
    center.x=0;//centers.at<float>(k,0);
    center.y=0;//:centers.at<float>(k,1);


    // Set up training data
    /* generate random labeld sample from multigaussian distribution */ 
    for( int k = 0; k < clusterCount; ++k )
    {
        Mat pointChunk = test_data.rowRange(k*sampleCount/clusterCount,
                                            k == clusterCount - 1 ? sampleCount :
                                            (k+1)*sampleCount/clusterCount);

        cout<< " error " << endl;
        rng.fill(pointChunk, CV_RAND_NORMAL, Scalar(center.x, center.y,0,0), Scalar(width*0.10, height*0.10));
        Mat repeat_center;
        repeat(centers.row(k),pointChunk.rows,1,repeat_center);
        pointChunk=pointChunk+repeat_center; 



        Mat labelChunk = labelsMat.rowRange(k*sampleCount/clusterCount,
                                            k == clusterCount - 1 ? sampleCount :
                                            (k+1)*sampleCount/clusterCount);

        labelChunk=k;

    }

    test_data=abs(test_data);
    for(int i=0;i<N;++i)
    {

        test_data.at<float>(i,0)= cvCeil(test_data.at<float>(i,0));
        test_data.at<float>(i,1)= cvCeil(test_data.at<float>(i,1));

    }

     std::vector<Vec3b> colors_region;
    std::vector<Vec3b> colors_data;
    //generate random colors for each label
    for(int i=0;i<K;++i)
    {
        int icolor = (unsigned) rng;
        colors_region.push_back( Vec3b( icolor&255, (icolor>>8)&255, (icolor>>16)&255 ));
        icolor=(unsigned)rng;
        colors_data.push_back( Vec3b( icolor&255, (icolor>>8)&255, (icolor>>16)&255 ));

    }

  //draw k-means output
          kmeans(test_data, clusterCount, labelsMatKMeans,
               TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0),
               3, KMEANS_PP_CENTERS, centers);

          cout<< "  " << endl;


        img_kmeans = Scalar::all(0);

        for( int i = 0; i < sampleCount; i++ )
        {
            int clusterIdx = labelsMatKMeans.at<int>(i);

            //cout << "C_ID : " << labelsMatKMeans.at<float>(i);
            //cout << "C_ID : " << clusterIdx<< endl;
            Point ipt ;
            ipt.x= (int)test_data.at<float>(i,0);
            ipt.y=(int)test_data.at<float>(i,1);
            circle( img_kmeans, ipt, 5, Scalar(colors_data[clusterIdx]), -1, 8 );
        }

        imshow("K-Means clustering", img_kmeans);
        imwrite("k_means.png",img_kmeans);
}
2016-01-29 05:34:53 -0600 commented question How to find which pixel belongs to which clusters or centers?

When i print labelMatKmeans , i got 0,1,2,3,4 value . They are repeated (total value : 78644) . I found that these are cluster indices. So how can i know that 0 and other value for which cluster ?

2016-01-27 06:10:06 -0600 commented question How to find which pixel belongs to which clusters or centers?

in output image, there are 5 clusters. I want print 1 to 5 numbers assign to cluster in output image. When i print image matrix (img_kmeans) [code : cout << mat << endl ], i got numeric value like 0 0 0 0 10 52 21 10 52 21 ... 0 0 0 44 55 47 44 55 47 0 0 0 I think these is RGB value. How can i know which value are for which cluster ?

2016-01-27 04:27:25 -0600 commented question How to find which pixel belongs to which clusters or centers?

i want output like (1,1) is in cluster 1. SO how can i print it?

2016-01-27 03:48:39 -0600 commented question How to find which pixel belongs to which clusters or centers?

ok thanks. i edited with code.

2016-01-27 03:38:05 -0600 commented question How to find which pixel belongs to which clusters or centers?

where i can add my code?

2016-01-27 03:27:22 -0600 received badge  Editor (source)
2016-01-27 03:21:13 -0600 commented question How to find which pixel belongs to which clusters or centers?

center.txt in which i defined initial centres. In my program, i use text file for reading initial center.

2016-01-27 01:47:45 -0600 asked a question How to find which pixel belongs to which clusters or centers?

image description I generated clusters with k-means clustering algorithm. I used 5 centres ( defined in text file like (0,0) (256,200) (500,500) ). I got output windows with 5 groups of points at different place.

I want to find points with cluster numbers.

ex. (1,1) is in cluster no. 1 ; (2,10) is in cluster no. 3

Give me any idea or suggestion about this. 

code:

(Include header files)
using namespace cv;
using namespace cv::ml;
using namespace std;
void readCenters(cv::Mat&, const char *);

int CLUSTER_COUNT=5;//number of clusters
Mat train_data;
Mat test_data;

void printDim(cv::Mat mat,const char *name)

void splitData(cv::Mat &data,cv::Mat &train_data,cv::Mat &test_data)


void readData(cv::Mat& data)
{
    std::vector<const char *> filenames;
    filenames.push_back("Data/ik147_1.txt");
    filenames.push_back("Data/ik147_2.txt");
string line;
    vector<Mat> raw_data(2);//= new std::vector<cv::Mat>(4);
    int row;
    double  min,max;
    for(int i =0;i<2;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<<"N value : "<< N << endl;
    int columns_to_read=2;
    data.create(N,columns_to_read,CV_32FC1);

    for(int i=0;i<columns_to_read;i++)
    {
        raw_data[i](Rect(0,0,1,N)).copyTo(data(Rect(i,0,1,N)));
    }

}

void computeLabelledData(Mat& data,Mat &data_with_labels)
{
    Mat labels,centers;    
    kmeans(data, CLUSTER_COUNT, labels,
               TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0),
               3, KMEANS_PP_CENTERS, centers);


    data_with_labels.create(data.rows,data.cols+1,CV_32FC1);
    data.copyTo(data_with_labels(Rect(0,0,data.cols,data.rows)));
    labels.copyTo(data_with_labels(Rect(data.cols,0,labels.cols,labels.rows)));
}

void print(Mat mat, int prec)
{      
    for(int i=0; i<mat.size().height; i++)
    {
        cout << "[";
        for(int j=0; j<mat.size().width; j++)
        {
            cout << setprecision(prec) << mat.at<float>(i,j);
            if(j != mat.size().width-1)
                cout << ", ";
            else
                cout << "]" << endl; 
        }
    }
}
int main()
{
    // Data for visual representation
    int width = 512, height = 512;
    Mat image = Mat::zeros(height, width, CV_8UC3);

        Mat data;
    readData(data);
    printDim(data,"data");
    Mat data_with_labels,train_data_labels,test_data_labels;
     computeLabelledData(data,data_with_labels);
      splitData(data_with_labels,train_data_labels,test_data_labels);
        //splitData(data,train_data,test_data);

    Mat train_data,train_labels;
    train_data_labels(cv::Rect(0,0,train_data_labels.cols-1,train_data_labels.rows)).copyTo(train_data);
    train_data_labels(cv::Rect(train_data_labels.cols-1,0,1,train_data_labels.rows)).copyTo(train_labels);

    Mat test_data,test_labels,predicted_labels,confusion_matrix;
    test_data_labels(cv::Rect(0,0,test_data_labels.cols-1,test_data_labels.rows)).copyTo(test_data);
    test_data_labels(cv::Rect(test_data_labels.cols-1,0,1,test_data_labels.rows)).copyTo(test_labels);

    printDim(train_data,"train_data"); 
    printDim(test_data,"test_data"); 

    cout <<"train data : "  << endl;
    //print(train_data, 3);
    cout <<"test data : "  << endl;
    //print(test_data, 3);


    int N=test_data.rows; //number of data points
    int K=5;  //number of labels

    RNG rng((unsigned)time(NULL)); 
    RNG rng_center(20);
    int clusterCount=K;
    int sampleCount = N;
    Mat centers(5,2,CV_32FC1);
    readCenters(centers,"centers.txt");
    Mat labelsMat(N,1,CV_32SC1);
    Mat labelsMatKMeans;
    Mat img_kmeans(512, 512, CV_8UC3);

    Point center;
    center.x=0;//centers.at<float>(k,0);
    center.y=0;//:centers.at<float>(k,1 ...
(more)