How to display decision region for svm prediction ?

asked 2016-03-10 06:16:23 -0600

sumit patel gravatar image

updated 2016-03-11 02:04:40 -0600

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)
edit retag flag offensive close merge delete

Comments

I have seriously no idea of what you're trying to achieve, but

  1. sampleMat = (Mat_<float>(1, 3) << (0,i),(1,i),(2,i)); this doesn't make sense at all
  2. Your sampleMat is equal for all the pixels of the same row. Then, the response is also equal. So you get a "striped" decision region matrix
  3. Bonus: SVM Linear kernels do not have a degree property

If you want someone to further help, please post your full code (full meaning minimum needed)

LorenaGdL gravatar imageLorenaGdL ( 2016-03-10 08:32:48 -0600 )edit

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.

sumit patel gravatar imagesumit patel ( 2016-03-10 11:39:01 -0600 )edit

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

sumit patel gravatar imagesumit patel ( 2016-03-10 11:40:36 -0600 )edit

for i=4, your sampleMat will be [4,4,4] (think of how the comma operator works).

i do not think, you wanted that. what did you want there ?

berak gravatar imageberak ( 2016-03-11 01:01:15 -0600 )edit

@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 ) ?

sumit patel gravatar imagesumit patel ( 2016-03-11 01:12:02 -0600 )edit

if you train your svm on pixel colors (do you ?) , your test sample should be a color, too, not a position.

training on the upper half of the image, and testing on the lower would make sense to me, i don't see, how the random helps. or how / what you're trying to predict.

but again, it's impossible to tell, since a lot of information is missing from your question.

berak gravatar imageberak ( 2016-03-11 01:24:49 -0600 )edit

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.

sumit patel gravatar imagesumit patel ( 2016-03-11 02:03:01 -0600 )edit