<commit_before>//
//  ofxFeatureFinder.cpp
//
//  Created by joe on 12/18/13.
//
//

#include "ofxFeatureFinder.h"

ofxFeatureFinder::ofxFeatureFinder() {
    
    ofRegisterMouseEvents(this);
    
    bDrawingRegion = false;

    hessianThreshold = 800;
    octaves = 3;
    octaveLayers = 4;

    bBlur = true;
    blurLevel = 3;
    
    minMatchCount = 8;
}



ofxFeatureFinder::~ofxFeatureFinder() {
}

void ofxFeatureFinder::setDisplayRect(int x, int y, int width, int height) {
    
    displayRect = ofRectangle(x, y, width, height);
    
}

void ofxFeatureFinder::setCropRect(int x, int y, int width, int height) {
    
    cropRect = ofRectangle(x, y, width, height);

    processImage.allocate(width, height);
    rawImageCropped.allocate(width, height);
}



void ofxFeatureFinder::clearRegions() {
    regions.clear();
}


void ofxFeatureFinder::updateSourceImage(ofxCvGrayscaleImage image) {
    updateSourceImage(image);
}
void ofxFeatureFinder::updateSourceImage(ofxCvColorImage image) {

    rawImage = image;
    rawImage.setROI(cropRect);
    
    rawImageCropped.setFromPixels(rawImage.getRoiPixels(), cropRect.width, cropRect.height);
    
    rawImage.setROI(0, 0, rawImage.width, rawImage.height);

    processImage = rawImageCropped;
    
    processImage.contrastStretch();
    
    if (bBlur) {
        processImage.blurGaussian(blurLevel);
    }
    
    if (bEqualizeHistogram) {
        cv::Mat source = cv::cvarrToMat(processImage.getCvImage());
        
        equalizeHist( source, processImageMat );
    }
    else {
        processImageMat = cv::cvarrToMat(processImage.getCvImage());
    }
}



void ofxFeatureFinder::findKeypoints() {
    
    detector = new cv::SurfFeatureDetector(hessianThreshold, octaves, octaveLayers);
    detector->detect(processImageMat, imageKeypoints);
    delete detector;

    extractor = new cv::SurfDescriptorExtractor();
    extractor->compute(processImageMat, imageKeypoints, imageDescriptors);
    delete extractor;
}


ofxFeatureFinderObject ofxFeatureFinder::createObject() {
    
    std::vector<cv::KeyPoint> selectedKeypoints;
    cv::Mat selectedDescriptors;
    
    vector<ofPolyline>::iterator it;
    int i;
    // collect the selected keypoints
    for(i = 0; i < imageKeypoints.size(); i++ )
    {
        cv::KeyPoint keypt = imageKeypoints.at(i);
        
        for(it = regions.begin(); it != regions.end(); ++it){
            if ((*it).inside(keypt.pt.x, keypt.pt.y)) {
                
                selectedKeypoints.push_back(keypt);
                
                break;
            }
        }
    }
    
    if (selectedKeypoints.size() == 0) {
        return;
    }

    cv::Mat mat = cv::cvarrToMat(processImage.getCvImage());
    
    extractor = new cv::SurfDescriptorExtractor();
    extractor->compute(mat, selectedKeypoints, selectedDescriptors);
    delete extractor;
    
    ofxFeatureFinderObject object = ofxFeatureFinderObject(regions, selectedKeypoints, selectedDescriptors);
    objects.push_back(object);
    
    cout << "added object with " << selectedKeypoints.size() << " keypoints." << endl;

    this->clearRegions();
    
    return object;
}

void ofxFeatureFinder::createAndSaveObject(string filepath) {
    ofxFeatureFinderObject object = createObject();
    saveObject(object, filepath);
}

void ofxFeatureFinder::saveObject(ofxFeatureFinderObject object, string filepath) {
    object.save(filepath);<commit_msg>removed extra conversion to mat during createObject<commit_after>//
//  ofxFeatureFinder.cpp
//
//  Created by joe on 12/18/13.
//
//

#include "ofxFeatureFinder.h"

ofxFeatureFinder::ofxFeatureFinder() {
    
    ofRegisterMouseEvents(this);
    
    bDrawingRegion = false;

    hessianThreshold = 800;
    octaves = 3;
    octaveLayers = 4;

    bBlur = true;
    blurLevel = 3;
    
    minMatchCount = 8;
}



ofxFeatureFinder::~ofxFeatureFinder() {
}

void ofxFeatureFinder::setDisplayRect(int x, int y, int width, int height) {
    
    displayRect = ofRectangle(x, y, width, height);
    
}

void ofxFeatureFinder::setCropRect(int x, int y, int width, int height) {
    
    cropRect = ofRectangle(x, y, width, height);

    processImage.allocate(width, height);
    rawImageCropped.allocate(width, height);
}



void ofxFeatureFinder::clearRegions() {
    regions.clear();
}


void ofxFeatureFinder::updateSourceImage(ofxCvGrayscaleImage image) {
    updateSourceImage(image);
}
void ofxFeatureFinder::updateSourceImage(ofxCvColorImage image) {

    rawImage = image;
    rawImage.setROI(cropRect);
    
    rawImageCropped.setFromPixels(rawImage.getRoiPixels(), cropRect.width, cropRect.height);
    
    rawImage.setROI(0, 0, rawImage.width, rawImage.height);

    processImage = rawImageCropped;
    
    processImage.contrastStretch();
    
    if (bBlur) {
        processImage.blurGaussian(blurLevel);
    }
    
    if (bEqualizeHistogram) {
        cv::Mat source = cv::cvarrToMat(processImage.getCvImage());
        
        equalizeHist( source, processImageMat );
    }
    else {
        processImageMat = cv::cvarrToMat(processImage.getCvImage());
    }
}



void ofxFeatureFinder::findKeypoints() {
    
    detector = new cv::SurfFeatureDetector(hessianThreshold, octaves, octaveLayers);
    detector->detect(processImageMat, imageKeypoints);
    delete detector;

    extractor = new cv::SurfDescriptorExtractor();
    extractor->compute(processImageMat, imageKeypoints, imageDescriptors);
    delete extractor;
}


ofxFeatureFinderObject ofxFeatureFinder::createObject() {
    
    std::vector<cv::KeyPoint> selectedKeypoints;
    cv::Mat selectedDescriptors;
    
    vector<ofPolyline>::iterator it;
    int i;
    // collect the selected keypoints
    for(i = 0; i < imageKeypoints.size(); i++ )
    {
        cv::KeyPoint keypt = imageKeypoints.at(i);
        
        for(it = regions.begin(); it != regions.end(); ++it){
            if ((*it).inside(keypt.pt.x, keypt.pt.y)) {
                
                selectedKeypoints.push_back(keypt);
                
                break;
            }
        }
    }
    
    if (selectedKeypoints.size() == 0) {
        return;
    }

    extractor = new cv::SurfDescriptorExtractor();
    extractor->compute(processImageMat, selectedKeypoints, selectedDescriptors);
    delete extractor;
    
    ofxFeatureFinderObject object = ofxFeatureFinderObject(regions, selectedKeypoints, selectedDescriptors);
    objects.push_back(object);
    
    cout << "added object with " << selectedKeypoints.size() << " keypoints." << endl;

    this->clearRegions();
    
    return object;
}

void ofxFeatureFinder::createAndSaveObject(string filepath) {
    ofxFeatureFinderObject object = createObject();
    saveObject(object, filepath);
}

void ofxFeatureFinder::saveObject(ofxFeatureFinderObject object, string filepath) {
    object.save(filepath);
{'fraction_non_alphanumeric': 0.11439813465461965, 'fraction_numerical': 0.004954823666569514, 'mean_word_length': 3.1568746214415504, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2610348', 'n_tokens_mistral': 2270, 'n_tokens_neox': 2181, 'n_words': 384}