void BOW::calc(cv::Mat & img, Feature & feat){ Mat rimg; img.copyTo(rimg); // if (img.rows > 100 && img.cols > 100) // rimg = Mat(img, Range(img.rows/5, img.rows*4/5), Range(img.cols/5, img.cols*4/5)); // else // rimg = img; if (rimg.cols > 300 || rimg.rows > 300) { double fr = min(300.0/rimg.rows, 300.0/rimg.cols); resize(rimg, rimg, Size(), fr, fr, INTER_AREA); } ImgBGReplace(rimg, rimg); //GaussianBlur(img, img, Size(5, 5), 1.5, 1.5, BORDER_REPLICATE); vector<KeyPoint> kps; m_fd->detect(rimg, kps); Mat res; m_ext->compute(rimg, kps, res); if (kps.size()) { feat.clear(); for (int i = 0; i < m_ext->descriptorSize(); ++i) feat.push_back((double) res.at<uchar>(0, i) / max(kps.size(), 1U)); } else { feat.clear(); for (int i = 0; i < m_ext->descriptorSize(); ++i) feat.push_back(0); } }
void Labeler::extractFeature(Feature& feat, const Instance* pInstance, int idx) { feat.clear(); const vector<string>& words = pInstance->words; int sentsize = words.size(); string curWord = idx >= 0 && idx < sentsize ? normalize_to_lowerwithdigit(words[idx]) : nullkey; // words int unknownId = m_wordAlphabet.from_string(unknownkey); int curWordId = m_wordAlphabet.from_string(curWord); if (curWordId >= 0) feat.words.push_back(curWordId); else feat.words.push_back(unknownId); const vector<string>& linear_features = pInstance->sparsefeatures[idx]; for (int i = 0; i < linear_features.size(); i++) { int curFeatId = m_featAlphabet.from_string(linear_features[i]); if (curFeatId >= 0) feat.linear_features.push_back(curFeatId); } }
void Labeler::extractFeature(Feature& feat, const Instance* pInstance, int idx) { feat.clear(); const vector<string>& words = pInstance->words; int sentsize = words.size(); string curWord = idx >= 0 && idx < sentsize ? normalize_to_lowerwithdigit(words[idx]) : nullkey; // word features int unknownId = m_wordAlphabet.from_string(unknownkey); int curWordId = m_wordAlphabet.from_string(curWord); if (curWordId >= 0) feat.words.push_back(curWordId); else feat.words.push_back(unknownId); // tag features const vector<vector<string> > &tagfeatures = pInstance->tagfeatures; int tagNum = tagfeatures[idx].size(); for (int i = 0; i < tagNum; i++) { unknownId = m_tagAlphabets[i].from_string(unknownkey); int curTagId = m_tagAlphabets[i].from_string(tagfeatures[idx][i]); if (curTagId >= 0) feat.tags.push_back(curTagId); else feat.tags.push_back(unknownId); } // char features unknownId = m_charAlphabet.from_string(unknownkey); const vector<vector<string> > &charfeatures = pInstance->charfeatures; const vector<string>& cur_chars = charfeatures[idx]; int cur_char_size = cur_chars.size(); // actually we support a max window of m_options.charcontext = 2 for (int i = 0; i < cur_char_size; i++) { string curChar = cur_chars[i]; int curCharId = m_charAlphabet.from_string(curChar); if (curCharId >= 0) feat.chars.push_back(curCharId); else feat.chars.push_back(unknownId); } int nullkeyId = m_charAlphabet.from_string(nullkey); if (feat.chars.empty()) { feat.chars.push_back(nullkeyId); } const vector<string>& linear_features = pInstance->sparsefeatures[idx]; for (int i = 0; i < linear_features.size(); i++) { int curFeatId = m_featAlphabet.from_string(linear_features[i]); if (curFeatId >= 0) feat.linear_features.push_back(curFeatId); } }
void ColorMean::calc(Mat& im, Feature& res) { Mat ras; resize(im, ras, Size(3, 3), 0, 0, INTER_AREA); res.clear(); for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) { Vec3b r = ras.at<Vec3b>(i, j); for (int k = 0; k < 3; ++k) { double t = r[k]; t /= 256; res.push_back(t); } } }
int FeatureSet::LoadFeature(const char* const fileName, Feature& feature) { feature.clear(); FileBuffer buffer(fileName); int rtn = 0; int len; rtn = buffer.GetNextData(len); CHECK_RTN(rtn); for (int i = 0; i < len; ++i) { int key; double value; rtn = buffer.GetNextData(key); CHECK_RTN(rtn); rtn = buffer.GetNextData(value); CHECK_RTN(rtn); feature[key] = value; } return 0; }
void Labeler::extractFeature(Feature& feat, const Instance* pInstance, int idx) { feat.clear(); const vector<vector<string> >& words = pInstance->words; const vector<vector<vector<string> > > &chars = pInstance->chars; static vector<int> curChars; int sentsize = words.size(); if (idx < 0 || idx >= sentsize) return; int wordnumber = words[idx].size(); int unknownWordId = m_wordAlphabet.from_string(unknownkey); int unknownCharId = m_charAlphabet.from_string(unknownkey); for (int i = 0; i < wordnumber; i++) { string curWord = normalize_to_lowerwithdigit(words[idx][i]); int curWordId = m_wordAlphabet.from_string(curWord); if (curWordId >= 0) feat.words.push_back(curWordId); else feat.words.push_back(unknownWordId); int wordlength = chars[idx][i].size(); curChars.clear(); for (int j = 0; j < wordlength; j++) { string curChar = chars[idx][i][j]; int curCharId = m_charAlphabet.from_string(curChar); if (curCharId >= 0) curChars.push_back(curCharId); else curChars.push_back(unknownCharId); } feat.chars.push_back(curChars); } }