Ask Your Question

kaushikmit's profile - activity

2016-02-09 11:20:16 -0600 asked a question Train set of images using libsvm

I have a set of multiple classes of positive and a folder of negative images. I want to use multi class classification using libsvm. I have downloaded and installed the libsvm libraries on ubuntu 14.04 and gone through the commands. I did few research to know about the process of object detection using svm. What i could understand is that I have to make a file containing labels class and value so that I can train that. But i am not sure how i can make that file. Please help me with it. Is there any code for writing that file from the given images.

2015-12-15 10:56:58 -0600 commented question face and eye detection error : OpenCV Error: Assertion failed

You are never checking if the roi values that you are using are going out of frame or not. I mean the last part of the function.

2015-12-15 08:24:12 -0600 commented question Object Detection using SVM

@LorenaGdL thanx for the steps. Seems as if I already have covered the second and third points. But I don't have any clue about the first step. Can you please help me with it. I was using this for converting to 1d array Mat p= img_gray.reshape(1, img_gray.channels()*img_gray.size().area());.

2015-12-14 07:15:42 -0600 commented question Object Detection using SVM

Thanx @LorenaGdL, but how do I do it ? Shall I upload my train function which saves the xml file?

2015-12-14 05:59:20 -0600 commented question Object Detection using SVM

Thanx for the suggestion. I removed the train() function outside the loop. The error is samples.cols == var_count. in predict fnction

2015-12-14 05:01:56 -0600 asked a question Object Detection using SVM

I was following this answer, which explains step by step the coding for svm. But I am having a runtime error in the predict function, here is the code I came up with

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<opencv2/ml.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
using namespace cv::ml;

int main ( int argc, char** argv )
{
    cout << "Manas\n";
    char* filename;
    Mat input_image;
    VideoCapture capture(0);
    FileStorage fs;
    fs.open("SVM.xml", FileStorage::READ);
    Mat SVM_TrainingData;
    Mat SVM_Classes;
    fs["TrainingData"] >> SVM_TrainingData;
    fs["classes"] >> SVM_Classes;
    Ptr<SVM> SVM_params = SVM::create();
    SVM_params->setType(SVM::C_SVC);
    SVM_params->setKernel(SVM::LINEAR);
    SVM_params->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 0.01));
    Ptr<TrainData> td = TrainData::create(SVM_TrainingData, ROW_SAMPLE, SVM_Classes);
    SVM_params->train(td);
    while (true)
    {
        capture.read(input_image);
        resize(input_image, input_image, Size(200, 200));

        Mat img_gray;
        cvtColor(input_image, img_gray, CV_BGR2GRAY);
        blur(img_gray, img_gray, Size(5,5));
        Mat p= img_gray.reshape(1, img_gray.channels()*img_gray.size().area());
        p.convertTo(p, CV_32F);

        int response = (int)SVM_params->predict( p );
        if(response==1) cout<<"Detected";
        imshow("Plate Detected", input_image);

        int c = waitKey(10);
        if (c == 27)
            break;
    }
    return 0;
}

I am sure I am missing something, but i can't figure out what.Please suggest what else shall i include.

Update

Here is the code that I am using for training my images

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cv.h>
#include <highgui.h>
#include <cvaux.h>
#include <iostream>
#include <vector>
#include<string.h>
using namespace std;
using namespace cv;

int main ( int argc, char** argv )
{
    cout << "OpenCV Training SVM Automatic Number Plate Recognition\n";
    cout << "\n";

    char* path_Plates;
    char* path_NoPlates;
    int numPlates;
    int numNoPlates;
    int imageWidth=150;
    int imageHeight=150;

    //Check if user specify image to process
    if(1)
    {
        numPlates= 12;
        numNoPlates= 90 ;
        path_Plates= "/home/kaushik/opencv_work/Manas6/Pics/Positive_Images/";
        path_NoPlates= "/home/kaushik/opencv_work/Manas6/Pics/Negative_Images/i";

    }else{
        cout << "Usage:\n" << argv[0] << " <num Plate Files> <num Non Plate Files> <path to plate folder files> <path to non plate files> \n";
        return 0;
    }

    Mat classes;//(numPlates+numNoPlates, 1, CV_32FC1);
    Mat trainingData;//(numPlates+numNoPlates, imageWidth*imageHeight, CV_32FC1 );

    Mat trainingImages;
    vector<int> trainingLabels;

    for(int i=1; i<= numPlates; i++)
    {

        stringstream ss(stringstream::in | stringstream::out);
        ss<<path_Plates<<i<<".jpg";
        try{

            const char* a = ss.str().c_str();
            printf("\n%s\n",a);
            Mat img = imread(ss.str(), CV_LOAD_IMAGE_UNCHANGED);
            img= img.clone().reshape(1, 1);
            //imshow("Window",img);
            //cout<<ss.str();
            trainingImages.push_back(img);
            trainingLabels.push_back(1);
        }
        catch(Exception e){;}
    }

    for(int i=0; i< numNoPlates; i++)
    {
        stringstream ss(stringstream::in | stringstream::out);
        ss << path_NoPlates<<i << ".jpg";
        try
        {
            const char* a = ss.str().c_str();
            printf("\n%s\n",a);
            Mat img=imread(ss.str(),CV_LOAD_IMAGE_UNCHANGED);
            //imshow("Win",img);
            img= img.clone().reshape(1, 1);
            trainingImages.push_back(img);
            trainingLabels.push_back(0);
            //cout<<ss.str();
        }
        catch(Exception e){;}
    }

    Mat(trainingImages).copyTo(trainingData);
    //trainingData = trainingData.reshape(1,trainingData.rows);
    trainingData.convertTo(trainingData, CV_32FC1);
    Mat(trainingLabels).copyTo(classes);

    FileStorage fs ...
(more)
2015-12-11 23:32:39 -0600 asked a question object detection using trained xml file

I am trying to use SVM for object detection. I have a set of a positive and negative images. Using this code I have trained the files into an xml file.

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cv.h>
#include <highgui.h>
#include <cvaux.h>
#include <iostream>
#include <vector>
#include<string.h>
using namespace std;
using namespace cv;

int main ( int argc, char** argv )
{
    cout << "OpenCV Training SVM Automatic Number Plate Recognition\n";
    cout << "\n";

    char* path_Plates;
    char* path_NoPlates;
    int numPlates;
    int numNoPlates;
    int imageWidth=150;
    int imageHeight=150;

    //Check if user specify image to process
    if(1)
    {
        numPlates= 12;
        numNoPlates= 67 ;
        path_Plates= "/home/kaushik/opencv_work/Manas6/Pics/Positive_Images/";
        path_NoPlates= "/home/kaushik/opencv_work/Manas6/Pics/Negative_Images/i";

    }else{
        cout << "Usage:\n" << argv[0] << " <num Plate Files> <num Non Plate Files> <path to plate folder files> <path to non plate files> \n";
        return 0;
    }

    Mat classes;//(numPlates+numNoPlates, 1, CV_32FC1);
    Mat trainingData;//(numPlates+numNoPlates, imageWidth*imageHeight, CV_32FC1 );

    Mat trainingImages;
    vector<int> trainingLabels;

    for(int i=1; i<= numPlates; i++)
    {

        stringstream ss(stringstream::in | stringstream::out);
        ss<<path_Plates<<i<<".jpg";
        try{

            const char* a = ss.str().c_str();
            printf("\n%s\n",a);
            Mat img = imread(ss.str(), CV_LOAD_IMAGE_UNCHANGED);
            img= img.clone().reshape(1, 1);
            //imshow("Window",img);
            //cout<<ss.str();
            trainingImages.push_back(img);
            trainingLabels.push_back(1);
        }
        catch(Exception e){;}
    }

    for(int i=0; i< numNoPlates; i++)
    {
        stringstream ss(stringstream::in | stringstream::out);
        ss << path_NoPlates<<i << ".jpg";
        try
        {
            const char* a = ss.str().c_str();
            printf("\n%s\n",a);
            Mat img=imread(ss.str(),CV_LOAD_IMAGE_UNCHANGED);
            //imshow("Win",img);
            img= img.clone().reshape(1, 1);
            trainingImages.push_back(img);
            trainingLabels.push_back(0);
            //cout<<ss.str();
        }
        catch(Exception e){;}
    }

    Mat(trainingImages).copyTo(trainingData);
    //trainingData = trainingData.reshape(1,trainingData.rows);
    trainingData.convertTo(trainingData, CV_32FC1);
    Mat(trainingLabels).copyTo(classes);

    FileStorage fs("SVM.xml", FileStorage::WRITE);
    fs << "TrainingData" << trainingData;
    fs << "classes" << classes;
    fs.release();

    return 0;
}

Now i have edited the code (that uses that xml file) to as per my need as followes. But it does not seem to work. The code provided in the link is not supported in opencv 3.0. So i edited it a bit.

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<opencv2/ml.hpp>
#include <iostream>
#include <vector>

#include "DetectRegions.h"


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

int main ( int argc, char** argv )
{
    cout << "Manas\n";
    char* filename;
    Mat input_image;
    VideoCapture capture(0);
    while (true)
    {
        capture.read(input_image);
        resize(input_image, input_image, Size(200, 200));

        DetectRegions detectRegions;
        detectRegions.setFilename("manas");
        detectRegions.saveRegions=false;
        detectRegions.showSteps=false;
        vector<Plate> posible_regions= detectRegions.run( input_image );
        FileStorage fs;
        fs.open("SVM.xml", FileStorage::READ);
        Mat SVM_TrainingData;
        Mat SVM_Classes;
        fs["TrainingData"] >> SVM_TrainingData;
        fs["classes"] >> SVM_Classes;
        Ptr<SVM> SVM_params = SVM::create();
        SVM_params->setType(SVM::C_SVC);
        SVM_params->setKernel(SVM::LINEAR);
        SVM_params->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 0.01));
        Ptr<TrainData> td = TrainData::create(SVM_TrainingData, ROW_SAMPLE, SVM_Classes);
        SVM_params->train(td);

        vector<Plate> plates;
        for(int i=0; i< posible_regions ...
(more)
2015-12-11 12:29:22 -0600 answered a question How to crop an image using C/C++ ?

If you want it to write it to another file, then you can use something like this inside the loop

    stringstream ssfn;
    ssfn << filenumber << ".png";
    filename = ssfn.str();
    filenumber++;

and for writing it to that file

crop  = frame(roi);
imwrite(filename,crop);

If you want to write to the same file name

try
{
    remove("fileName.ext");
}
catch(Exception e){}
crop  = frame(roi);
imwrite("filename.ext",crop);
2015-12-11 12:09:53 -0600 received badge  Enthusiast
2015-12-10 01:12:22 -0600 commented question using svm xml file for object detection

Sorry Berak , but I am totally new to svm.I wanted to know what do i do next?

2015-12-10 00:44:55 -0600 asked a question using svm xml file for object detection

I am trying to use SVM for object detection. I have a set of a positive and negative images. Using this code I have trained the files into an xml file. Now I want a code that can use that file for object detection.

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cv.h>
#include <highgui.h>
#include <cvaux.h>
#include <iostream>
#include <vector>
#include<string.h>
using namespace std;
using namespace cv;

int main ( int argc, char** argv )
{
    cout << "OpenCV Training SVM Automatic Number Plate Recognition\n";
    cout << "\n";

    char* path_Plates;
    char* path_NoPlates;
    int numPlates;
    int numNoPlates;
    int imageWidth=150;
    int imageHeight=150;

    //Check if user specify image to process
    if(1)
    {
        numPlates= 12;
        numNoPlates= 67 ;
        path_Plates= "/home/kaushik/opencv_work/Manas6/Pics/Positive_Images/";
        path_NoPlates= "/home/kaushik/opencv_work/Manas6/Pics/Negative_Images/i";

    }else{
        cout << "Usage:\n" << argv[0] << " <num Plate Files> <num Non Plate Files> <path to plate folder files> <path to non plate files> \n";
        return 0;
    }

    Mat classes;//(numPlates+numNoPlates, 1, CV_32FC1);
    Mat trainingData;//(numPlates+numNoPlates, imageWidth*imageHeight, CV_32FC1 );

    Mat trainingImages;
    vector<int> trainingLabels;

    for(int i=1; i<= numPlates; i++)
    {

        stringstream ss(stringstream::in | stringstream::out);
        ss<<path_Plates<<i<<".jpg";
        try{

            const char* a = ss.str().c_str();
            printf("\n%s\n",a);
            Mat img = imread(ss.str(), CV_LOAD_IMAGE_UNCHANGED);
            img= img.clone().reshape(1, 1);
            //imshow("Window",img);
            //cout<<ss.str();
            trainingImages.push_back(img);
            trainingLabels.push_back(1);
        }
        catch(Exception e){;}
    }

    for(int i=0; i< numNoPlates; i++)
    {
        stringstream ss(stringstream::in | stringstream::out);
        ss << path_NoPlates<<i << ".jpg";
        try
        {
            const char* a = ss.str().c_str();
            printf("\n%s\n",a);
            Mat img=imread(ss.str(),CV_LOAD_IMAGE_UNCHANGED);
            //imshow("Win",img);
            img= img.clone().reshape(1, 1);
            trainingImages.push_back(img);
            trainingLabels.push_back(0);
            //cout<<ss.str();
        }
        catch(Exception e){;}
    }

    Mat(trainingImages).copyTo(trainingData);
    //trainingData = trainingData.reshape(1,trainingData.rows);
    trainingData.convertTo(trainingData, CV_32FC1);
    Mat(trainingLabels).copyTo(classes);

    FileStorage fs("SVM.xml", FileStorage::WRITE);
    fs << "TrainingData" << trainingData;
    fs << "classes" << classes;
    fs.release();

    return 0;
}

Also I have downloaded the code given in documentation but i am unable to use it. Any help would be greatly appreciatd.

2015-12-08 22:59:12 -0600 asked a question Object Detection using SVM

I am new to SVM. I used to do object detection using HAAR Cascading. Now I am trying to implement SVM for object detection. I searched online and got to know about the basics. I wanted to use libsvm while coding for c++. I am getting lots of problems. Can anyone please explain step by step process of using it for object detection. BTW I looked into opencv documentation of svm. But I am not able to do anything further.

Also I got this code for training my SVM and saving it into an xml file. Now I want a code which can take this xml and detect objects in test cases.

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cv.h>
#include <highgui.h>
#include <cvaux.h>
#include <iostream>
#include <vector>
#include<string.h>
using namespace std;
using namespace cv;

int main ( int argc, char** argv )
{
    cout << "OpenCV Training SVM Automatic Number Plate Recognition\n";
    cout << "\n";

    char* path_Plates;
    char* path_NoPlates;
    int numPlates;
    int numNoPlates;
    int imageWidth=150;
    int imageHeight=150;

    //Check if user specify image to process
    if(1)
    {
        numPlates= 11;
        numNoPlates= 90 ;
        path_Plates= "/home/kaushik/opencv_work/Manas6/Pics/Positive_Images/";
        path_NoPlates= "/home/kaushik/opencv_work/Manas6/Pics/Negative_Images/i";

    }else{
        cout << "Usage:\n" << argv[0] << " <num Plate Files> <num Non Plate Files> <path to plate folder files> <path to non plate files> \n";
        return 0;
    }

    Mat classes;//(numPlates+numNoPlates, 1, CV_32FC1);
    Mat trainingData;//(numPlates+numNoPlates, imageWidth*imageHeight, CV_32FC1 );

    Mat trainingImages;
    vector<int> trainingLabels;

    for(int i=1; i<= numPlates; i++)
    {

        stringstream ss(stringstream::in | stringstream::out);
        ss<<path_Plates<<i<<".jpg";
        try{

            const char* a = ss.str().c_str();
            printf("\n%s\n",a);
            Mat img = imread(ss.str(), CV_LOAD_IMAGE_UNCHANGED);
            img= img.clone().reshape(1, 1);
            //imshow("Window",img);
            //cout<<ss.str();
            trainingImages.push_back(img);
            trainingLabels.push_back(1);
        }
        catch(Exception e){;}
    }

    for(int i=0; i< numNoPlates; i++)
    {
        stringstream ss(stringstream::in | stringstream::out);
        ss << path_NoPlates<<i << ".jpg";
        try
        {
            const char* a = ss.str().c_str();
            printf("\n%s\n",a);
            Mat img=imread(ss.str(), 0);
            //imshow("Win",img);
            img= img.clone().reshape(1, 1);
            trainingImages.push_back(img);
            trainingLabels.push_back(0);
            //cout<<ss.str();
        }
        catch(Exception e){;}
    }

    Mat(trainingImages).copyTo(trainingData);
    //trainingData = trainingData.reshape(1,trainingData.rows);
    trainingData.convertTo(trainingData, CV_32FC1);
    Mat(trainingLabels).copyTo(classes);

    FileStorage fs("SVM.xml", FileStorage::WRITE);
    fs << "TrainingData" << trainingData;
    fs << "classes" << classes;
    fs.release();

    return 0;
}

Any help would be greatly appreciated.

Also I would love to have suggestions on how to implement libsvm for object detection.

2015-12-08 12:16:29 -0600 commented question Training the SVM with positive and negative pictures

Thanx Berak I didnot notice that. It's a very silly mistake Now it is rectified

2015-12-08 11:53:37 -0600 received badge  Editor (source)
2015-12-08 10:33:05 -0600 commented question Training the SVM with positive and negative pictures

I have all the images of 150*150. I don't have any problem for positive images. I am getting error for negative images.

Also can this be the reason that I had all the negative images in a different format(bmp) and all the positive images in jpg?

2015-12-08 07:43:10 -0600 asked a question Training the SVM with positive and negative pictures

I was following this code.

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cv.h>
#include <highgui.h>
#include <cvaux.h>
#include <iostream>
#include <vector>
#include<string.h>
using namespace std;
using namespace cv;

int main ( int argc, char** argv )
{
    cout << "OpenCV Training SVM Automatic Number Plate Recognition\n";
    cout << "\n";

    char* path_Plates;
    char* path_NoPlates;
    int numPlates;
    int numNoPlates;
    int imageWidth=150;
    int imageHeight=150;

    //Check if user specify image to process
    if(1)
    {
        numPlates= 11;
        numNoPlates= 90 ;
        path_Plates= "/home/kaushik/opencv_work/Manas6/Pics/Positive_Images/";
        path_NoPlates= "/home/kaushik/opencv_work/Manas6/Pics/Negative_Images/i";

    }else{
        cout << "Usage:\n" << argv[0] << " <num Plate Files> <num Non Plate Files> <path to plate folder files> <path to non plate files> \n";
        return 0;
    }

    Mat classes;//(numPlates+numNoPlates, 1, CV_32FC1);
    Mat trainingData;//(numPlates+numNoPlates, imageWidth*imageHeight, CV_32FC1 );

    Mat trainingImages;
    vector<int> trainingLabels;

    for(int i=1; i<= numPlates; i++)
    {

        stringstream ss(stringstream::in | stringstream::out);
        ss<<path_Plates<<i<<".jpg";
        try{

            const char* a = ss.str().c_str();
            printf("\n%s\n",a);
            Mat img = imread(ss.str(), CV_LOAD_IMAGE_UNCHANGED);
            img= img.clone().reshape(1, 1);
            //imshow("Window",img);
            //cout<<ss.str();
            trainingImages.push_back(img);
            trainingLabels.push_back(1);
        }
        catch(Exception e){;}
    }

    for(int i=0; i< numNoPlates; i++)
    {
        stringstream ss(stringstream::in | stringstream::out);
        ss << path_NoPlates<<i << ".jpg";
        try
        {
            const char* a = ss.str().c_str();
            printf("\n%s\n",a);
            Mat img=imread(ss.str(), 0);
            //imshow("Win",img);
            img= img.clone().reshape(1, 1);
            trainingImages.push_back(img);
            trainingLabels.push_back(0);
            //cout<<ss.str();
        }
        catch(Exception e){;}
    }

    Mat(trainingImages).copyTo(trainingData);
    //trainingData = trainingData.reshape(1,trainingData.rows);
    trainingData.convertTo(trainingData, CV_32FC1);
    Mat(trainingLabels).copyTo(classes);

    FileStorage fs("SVM.xml", FileStorage::WRITE);
    fs << "TrainingData" << trainingData;
    fs << "classes" << classes;
    fs.release();

    return 0;
}

I am getting error in pushback() function.The error is as followes I want to know the reason for this error. Size of input arguments do not match in push_back(), /-/-/src/matrix.cpp line821

UPDATE

I have edited the code a bit. Now both positive and negative images are 150*150. The problem is out the two for loops(one for positive and one for negative) only one of them is runnung corrctly. The forloop involving negative images is runnung without error only when i comment out the first for loop.

Can anyone please help with this. PS. all images pos and neg are of same size and format

2015-08-03 14:00:24 -0600 asked a question How can I draw a boundary across a particular colour in opencv?
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include<stdio.h>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
    VideoCapture cap(0);
    while(true)
    {
    Mat img;
    cap.read(img);
    Mat dst;
    Mat imghsv;
    cvtColor(img, imghsv, COLOR_BGR2HSV);
    inRange(imghsv,
           Scalar(0, 30, 60),
           Scalar(20, 150, 255),
           dst
           );
    imshow("name",dst);
    if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
       {
            cout << "esc key is pressed by user" << endl;
            break;
       }
}

My inrange functions work fine. But I am unable to find a method with which i can draw a boundary to across all the pixels satisfying my threshold range