2015-10-29 8 views
5

मेरा लक्ष्य एक छवि के रूप में एक छवि लेना है और छवियों की एक पुस्तकालय में अपना सर्वश्रेष्ठ मैच ढूंढना है। मैं ओपनसीवी 3.0.0 में एसयूआरएफ फीचर्स का उपयोग कर रहा हूं, और एक मैच खोजने के लिए शब्द का थैला दृष्टिकोण। मुझे यह जानने के लिए एक तरीका चाहिए कि क्या लाइब्रेरी में क्वेरी छवि का मिलान है या नहीं। अगर ऐसा होता है, तो मैं उस छवि की अनुक्रमणिका जानना चाहता हूं जो निकटतम मैच है।सी ++/ओपनसीवी: यह निर्धारित करने के लिए कि कौन से क्लस्टर शब्दावली में छवियों से संबंधित हैं, BOWImgDescriptorExtractor का उपयोग कैसे करें?

Mat training_descriptors(1, extractor->descriptorSize(), extractor->descriptorType()); 
//read in all images and set to binary 
char filepath[1000]; 
for (int i = 1; i < trainingSetSize; i++){ 
    cout << "in for loop, iteration: " << i << endl; 
    _snprintf_s(filepath, 100, "C:/Users/Randal/Desktop/TestCase1Training/%d.bmp", i); 
    Mat temp = imread(filepath, CV_LOAD_IMAGE_GRAYSCALE); 
    Mat tempBW; 
    adaptiveThreshold(temp, tempBW, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 11, 2); 
    detector->detect(tempBW, keypoints1); 
    extractor->compute(tempBW, keypoints1, descriptors1); 
    training_descriptors.push_back(descriptors1); 
    cout << "descriptors added" << endl; 

} 
cout << "Total descriptors: " << training_descriptors.rows << endl; 
trainer.add(training_descriptors); 

Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased"); 
BOWImgDescriptorExtractor BOW(extractor, matcher); 
Mat library = trainer.cluster(); 
BOW.setVocabulary(library); 

मैं कोई मेल मिलता है करने की कोशिश में निम्नलिखित कोड लिखा है:

यहाँ (छवियों के पुस्तकालय में कुल 300) सभी छवियों में पढ़ रहे हैं और निकालने और क्लस्टरिंग सुविधाओं के लिए मेरे कोड है। समस्या यह है कि BOW.compute केवल क्लस्टर (शब्दों) के सूचकांक देता है जो छवियों और छवियों की लाइब्रेरी दोनों में मौजूद हैं। imgQ क्वेरी छवि है।

Mat output; 
Mat imgQBW; 
adaptiveThreshold(imgQ, imgQBW, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 11, 2); 
imshow("query image", imgQBW); 
detector->detect(imgQBW, keypoints2); 
extractor->compute(imgQBW, keypoints2, descriptors2); 

BOW.compute(imgQBW, keypoints1, output); 
cout << output.row(0) << endl; 

मुझे पता है कि जो धनुष में समूहों जो छवियों के अनुरूप की जरूरत है। मेरा आउटपुट अभी - output.row (0) - लाइब्रेरी में पाए गए क्लस्टर के सभी सूचकांक के साथ सिर्फ एक सरणी है। क्या मैं इस आउटपुट को गलत समझ रहा हूं? क्या यह निर्धारित करने का कोई तरीका है कि किस छवि में सबसे मेल खाने वाले क्लस्टर हैं?

उत्तर

0

मैं भी कुछ इसी तरह इस कोड के आधार पर किया था:

https://github.com/royshil/FoodcamClassifier/blob/master/training_common.cpp

लेकिन ऊपर भाग के बाद क्लस्टरिंग समाप्त हो गया है है। आपको अपने एमएल (मैंने एसवीएम का उपयोग किया) और आपके क्लस्टर सेंटर, आपके पास शब्दों के दृश्य बैग का उपयोग करने के लिए ट्रेन करना है। अधिक, आपको अपने क्लस्टर पॉइंट्स के सभी "निकटतम" अंक ढूंढने और हिस्टोग्राम का उपयोग करके उन्हें प्रशिक्षित करने की आवश्यकता है। इसके बाद, आपके पास आवृत्तियों का एक हिस्टोग्राम होगा (प्रमुख बिंदुओं का बैग) जिसे आपको प्रशिक्षित करने की आवश्यकता है।

Ptr<ifstream> ifs(new ifstream("training.txt")); 
int total_samples_in_file = 0; 
vector<string> classes_names; 
vector<string> lines; 

//read from the file - ifs and put into a vector 
for(int i=0;i<lines.size();i++) { 

    vector<KeyPoint> keypoints; 
    Mat response_hist; 
    Mat img; 
    string filepath; 

    string line(lines[i]); 
    istringstream iss(line); 

    iss >> filepath; 

    string class_to_train; 
    iss >> class_to_train; 
    class_ml = "class_" + class_to_train; 
    if(class_ml.size() == 0) continue; 

    img = imread(filepath); 

    detector->detect(img,keypoints); 
    bowide.compute(img, keypoints, response_hist); 

    cout << "."; cout.flush(); 
    //here create the logic for the class to train(class_0, e.g) and the data you need to train. 
} 

अधिक आप इस Git परियोजना पर पा सकते हैं:
https://github.com/royshil/FoodcamClassifier
यहाँ प्रलेखन:
http://www.morethantechnical.com/2011/08/25/a-simple-object-classifier-with-bag-of-words-using-opencv-2-3-w-code/

+0

अपने चर class_ml के प्रकार क्या है? एक स्ट्रिंग? साथ ही, आपके ऊपर जो कोड है, वह सभी क्लस्टरिंग के बाद आना चाहिए, है ना? – Phazoozoo

+0

क्लास_एमएल वह वर्ग है जो एमएल एल्गोरिदम प्रशिक्षण के लिए प्राप्त करता है। नामकरण शायद इतना अच्छा नहीं था। यह एक स्ट्रिंग हाँ है। कोड हां क्लस्टरिंग के बाद आता है। –

संबंधित मुद्दे

 संबंधित मुद्दे