void ExtractFeaturesFromEachFrame(const std::string & video_source, std::vector<Features> features_collection, const cv::Size & window_size, bool scale) { std::srand(std::time(0)); cv::VideoCapture video(video_source); if(!video.isOpened()) return; cv::HOGDescriptor hog; hog.winSize=window_size; cv::Mat frame; cv::Mat extracted_frame; Features features; while(video.read(frame)) { if(scale) cv::resize(frame, extracted_frame, window_size); else { // Extract frame cv::Rect patch; patch.width=window_size.width; patch.height=window_size.height; patch.x=std::rand()%(frame.cols-window_size.width); patch.y=std::rand()%(frame.rows-window_size.height); extracted_frame=((frame)(patch)).clone(); features_collection.push_back(Features((features_collection.size()==0?0:features_collection.front().size()))); ComputeFeatures(extracted_frame, features_collection.back(), window_size); } features.clear(); } }
Example(int a = 0) : ans(a) { feat.clear(); feat.resize(MAX, 0); }
/** * Extracts the features from the buffers for the given sensor code. * If useSpecSize is set to true only the last specSize frames in the buffer will be taken into account. */ Features GestureManagement::extractFeatures(int sensor, int specSize, bool useSpecSize) { if (config->getInt("verbosity") > 2) { cout << "* FEATURE EXTRACTION *************" << endl; cout << "Sensor: " << sensor << endl; } Features f; f.clear(); // for every measurement (accX, accY, ...) vector<int> indices = config->getInts("sensor data indices"); int fullsize = buffers.at(sensor)->size(); int size = fullsize; if (useSpecSize && fullsize > specSize) { size = specSize; } int offset = 0; if (useSpecSize) { offset = fullsize - size; } vector<int> meanPositions; meanPositions.clear(); if (config->getInt("use mean") == 1) { int chunks = config->getInt("number of mean window chunks"); for (int i=0; i<chunks; ++i) { meanPositions.push_back(offset+i*size/chunks); } meanPositions.push_back(fullsize-1); } vector<int> sdPositions; sdPositions.clear(); if (config->getInt("use standard deviation") == 1) { int chunks = config->getInt("number of sd window chunks"); for (int i=0; i<chunks; ++i) { sdPositions.push_back(offset+i*size/chunks); } sdPositions.push_back(fullsize-1); } vector<int> mcrPositions; mcrPositions.clear(); if (config->getInt("use mean crossing rate") == 1) { int chunks = config->getInt("number of mcr window chunks"); for (int i=0; i<chunks; ++i) { mcrPositions.push_back(offset+i*size/chunks); } mcrPositions.push_back(fullsize-1); } for (vector<int>::iterator it=indices.begin(); it!=indices.end(); ++it) { if (config->getInt("verbosity") > 2) { cout << "Index: " << *it << endl; } if (config->getInt("use mean") == 1) { for (int i=1; i<meanPositions.size(); ++i) { float mean = extr.at(sensor)->getMean(*it,meanPositions.at(i-1),meanPositions.at(i)); if (config->getInt("verbosity") > 2) { cout << "Mean: " << mean << endl; } f.push_back(mean); } } if (config->getInt("use standard deviation") == 1) { for (int i=1; i<sdPositions.size(); ++i) { float sd = extr.at(sensor)->getStandardDeviation(*it,sdPositions.at(i-1),sdPositions.at(i)); if (config->getInt("verbosity") > 2) { cout << "SD: " << sd << endl; } f.push_back(sd); } } if (config->getInt("use mean crossing rate") == 1) { for (int i=1; i<mcrPositions.size(); ++i) { float mcr = extr.at(sensor)->getMeanCrossingRate(*it,mcrPositions.at(i-1),mcrPositions.at(i)); if (config->getInt("verbosity") > 2) { cout << "MCR: " << mcr << endl; } f.push_back(mcr); } } } // for every measurement index (accX, ...) if (config->getInt("verbosity") > 2) { cout << "**********************************" << endl; } return f; }
int Alpha(TPoint *ray){ /* 0. Subinitialization - clear auxiliary variables and empty and nonsignificant input axes */ properties.resize(d); for (unsigned int i = 0; i < d; i++){properties[i] = i;} // initialize properties: all available features.clear(); outMatrix(x); /* 1. Null-cycle */ if (numStartFeatures == 2){ // start with two features? Feature optFeatureX; Feature optFeatureY; for (unsigned int i = 0; i < properties.size() - 1; i++){ for (unsigned int j = i + 1; j < properties.size(); j++){ /* Calculating minimal error on the plane of the i-th and the j-th properties */ Feature tmpFeature; curFeature = x[properties[i]]; unsigned int error = DGetMinError(properties[j], &tmpFeature); #ifdef DEF_OUT_ALPHA if (OUT_ALPHA){ Rcout << properties[i] << ", " << properties[j] << ", " << tmpFeature.angle << ", " << error << ", " << endl; } #endif if (error < optFeatureY.error){optFeatureX.number = properties[i]; optFeatureY = tmpFeature;} } } features.push_back(optFeatureX); features.push_back(optFeatureY); for (unsigned int i = 0; i < properties.size(); i++){ // delete recently found X and Y properties if (properties[i] == optFeatureX.number){properties.erase(properties.begin() + i);} if (properties[i] == optFeatureY.number){properties.erase(properties.begin() + i);} } curFeature = x[features[0].number]; UpdateCurFeature(); outString("Feature 1:"); outVector(curFeature); } /* 2. Main cycle */ /* Searching an optimal feature space while empirical error rate decreases */ while(features[features.size() - 1].error > 0 && properties.size() > 0){ Feature optFeature; for (unsigned int i = 0; i < properties.size(); i++){ /* Calculating minimal error on the plane of the curFeature and the j-th properties */ Feature tmpFeature; unsigned int error = DGetMinError(properties[i], &tmpFeature); #ifdef DEF_OUT_ALPHA if (OUT_ALPHA){ Rcout << properties[i] << ", " << tmpFeature.angle << ", " << error << ", " << endl; } #endif if (error < optFeature.error){optFeature = tmpFeature;} } if (optFeature.error < features[features.size() - 1].error){ features.push_back(optFeature); for (unsigned int i = 0; i < properties.size(); i++){ // delete recently found property if (properties[i] == optFeature.number){properties.erase(properties.begin() + i);} } UpdateCurFeature(); outString("Feature :"); outVector(curFeature); }else{break;} } outString("Features:"); outFeatures(features); /* Restoring the projection vector */ GetRay(ray); return features[features.size() - 1].error; }