Ask Your Question

ipunished's profile - activity

2016-12-05 05:22:46 -0500 received badge  Famous Question (source)
2015-03-21 13:28:52 -0500 received badge  Notable Question (source)
2014-07-03 03:44:01 -0500 received badge  Self-Learner (source)
2014-06-09 08:22:02 -0500 received badge  Nice Question (source)
2014-06-09 08:21:55 -0500 received badge  Popular Question (source)
2012-12-10 15:51:23 -0500 answered a question Unhandled exception while machine learning using NormalBayesClassifier

I managed to figure it out, the problem lay here: float label = 1.0; as all the images being trained cannot have the same label. The system must be able to distinguish between the images given, thus its best to arrange the images in groups and give the groups the float values.

2012-12-08 11:34:49 -0500 asked a question Unhandled exception while machine learning using NormalBayesClassifier

Hi,

Im trying to implement the bag of words approach using opencv. After making the dictionary I am using the NormalBayesClassifier to train and predict the system.

The code I am using is below:

int _tmain(int argc, _TCHAR* argv[])
{
initModule_nonfree();

Ptr<FeatureDetector> features = FeatureDetector::create("SIFT");
Ptr<DescriptorExtractor> descriptor = DescriptorExtractor::create("SIFT");
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");

//defining terms for bowkmeans trainer
TermCriteria tc(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 10, 0.001);
int dictionarySize = 100;
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);

BOWImgDescriptorExtractor bowDE(descriptor, matcher);

//**creating dictionary**//

Mat features1, features2;
Mat img = imread("c:\\1.jpg", 0);
Mat img2 = imread("c:\\2.jpg", 0);
vector<KeyPoint> keypoints, keypoints2;
features->detect(img, keypoints);
features->detect(img2,keypoints2);
descriptor->compute(img, keypoints, features1);
descriptor->compute(img2, keypoints2, features2);
bowTrainer.add(features1);
bowTrainer.add(features2);

Mat dictionary = bowTrainer.cluster();
bowDE.setVocabulary(dictionary);

//**dictionary made**//

//**now training the classifier**//

Mat trainme(0, dictionarySize, CV_32FC1); 
Mat labels(0, 1, CV_32FC1); //1d matrix with 32fc1 is requirement of normalbayesclassifier class

Mat bowDescriptor, bowDescriptor2;
bowDE.compute(img, keypoints, bowDescriptor);
trainme.push_back(bowDescriptor);
float label = 1.0;
labels.push_back(label);
bowDE.compute(img2, keypoints2, bowDescriptor2);
trainme.push_back(bowDescriptor2);
labels.push_back(label);

NormalBayesClassifier classifier;
classifier.train(trainme, labels);

//**classifier trained**//

//**now trying to predict using the same trained classifier, it should return 1.0**//

Mat tryme(0, dictionarySize, CV_32FC1);
Mat tryDescriptor;
Mat img3 = imread("2.jpg", 0);
vector<KeyPoint> keypoints3;
features->detect(img3, keypoints3);
bowDE.compute(img3, keypoints3, tryDescriptor);
tryme.push_back(tryDescriptor);

cout<<classifier.predict(tryme);
waitKey(0);



return 0;
}

I have prepared the trainme matrix as per the documentation as in each sample in each row. But the problem is that it gives an unhandled exception at this line: classifier.train(trainme, labels);

I tried to find out the cause but could not narrow it down. Any guidance would be greatly appreciated.

Thank you

2012-12-03 19:24:41 -0500 asked a question How to use bag of words to predict an image?

Hello,

Can anyone please tell me how to use the bag of words function in opencv? I want to use it on a folder of images; to train it such that if I use a sample image from that folder it should be able to recognize it.

While reading the only example online of this I was able to make a library / vocabulary. The code I used for that is below:

Ptr<FeatureDetector> features = FeatureDetector::create("SIFT");
Ptr<DescriptorExtractor> descriptors = DescriptorExtractor::create("SIFT");
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");

//defining terms for bowkmeans trainer
TermCriteria tc(MAX_ITER + EPS, 10, 0.001);
int dictionarySize = 1000;
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);

BOWImgDescriptorExtractor bowDE(descriptors, matcher);

//training data now
Mat features;
Mat img = imread("c:\\1.jpg", 0);
Mat img2 = imread("c:\\2.jpg", 0);
vector<KeyPoint> keypoints, keypoints2;
features->detect(img, keypoints);
features->detect(img2,keypoints2);
descriptor->compute(img, keypoints, features);
Mat features2;
descripto->compute(img2, keypoints2, features2);
bowTrainer.add(features);
bowTrainer.add(features2);

Mat dictionary = bowTrainer.cluster();
bowDE.setVocabulary(dictionary);

Now I believe this would create the dictionary. So is the system now trained? or will I have to use one of the machine learning functions to train it? If so can some one please give me a brief example of it?

Thank you

2012-12-02 14:09:42 -0500 marked best answer The detect feature of SIFT/SURF always breaks code

Hi,

Ive been trying out SIFT/SURF from online resources and wanted to test it out myself.

I first tried without the non-free libraries using this code:

int _tmain(int argc, _TCHAR* argv[])
{
Mat img = imread("c:\\car.jpg", 0);
Ptr<FeatureDetector> feature_detector = FeatureDetector::create("SIFT");
vector<KeyPoint> keypoints;

feature_detector->detect(img, keypoints);

Mat output;

drawKeypoints(img, keypoints, output, Scalar(255, 0, 0));

namedWindow("meh", CV_WINDOW_AUTOSIZE);
imshow("meh", output);
waitKey(0);



return 0;

}

Here if I do a step by step debugging it breaks at feature_detector->detect(img, keypoints);

Then I tried using the non-free library and tried this code:

int main(int argc, char** argv) 
{
    const Mat input = cv::imread("/tmp/image.jpg", 0); //Load as grayscale

    SiftFeatureDetector detector;
    vector<KeyPoint> keypoints;
    detector.detect(input, keypoints);

    // Add results to image and save.
    Mat output;
    drawKeypoints(input, keypoints, output);
    imwrite("/tmp/SIFT_RESULT.jpg", output);

    return 0;

 }

This again compiles without errors but when ran, breaks at this step: detector.detect(input, keypoints);

I cannot find the reason why. Can some one please help me out here.

Thank you

2012-11-22 16:41:30 -0500 commented answer FeatureDetector giving conversion error on image set

Thank you. How could I have missed that..

2012-11-22 04:54:08 -0500 asked a question FeatureDetector giving conversion error on image set

I was able to successfully load a number of images into a vector, vector<Mat>. The images once loaded in can be displayed with the imread function.

The problem is that I want to apply SIFT on this set of images using the second variant, as mentioned in the documentation:

void FeatureDetector::detect(const vector<Mat>& images, vector<vector<KeyPoint>>& keypoints, const vector<Mat>& masks=vector<Mat>() ) const

This is producing the following error:

error C2664: 'void cv::FeatureDetector::detect(const cv::Mat &,std::vector<_Ty> &,const cv::Mat &) const' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'const cv::Mat &'

The code I am using:

vector<Mat> images;

/* code to add all images to vector not shown as its messy but it was performed with FindFirstFile of windows.h. All images loaded correctly as they can be read by imread*/

initModule_nonfree();

Ptr<FeatureDetector> get_keypoints = FeatureDetector::create("SIFT");
vector<KeyPoint> keypoints;
get_keypoints->detect(images , keypoints);

The error is detected at get_keypoints->detect(images , keypoints);

2012-11-11 10:38:51 -0500 commented answer To save vector "KeyPoint" to file using builtin write function

Thank you. That was very helpful. I wish I could upvote this more than once.

2012-11-11 09:47:35 -0500 asked a question To save vector "KeyPoint" to file using builtin write function

Hi,

I want to write a vector of detected Keypoints to file and while I was told it can be done through operator overloading, I found out a function in the keypoint class documentation.

The documentation here shows a function called write..

// reading/writing a vector of keypoints to a file storage
void write(FileStorage& fs, const string& name, const vector<KeyPoint>& keypoints);
void read(const FileNode& node, vector<KeyPoint>& keypoints);

So basically there is a function to write the vector to file.. can some one please explain this and also, an example on how to use this would be great.

Thank you