コード例 #1
0
void Build_Vocabulary::extract(String filename, double threshold ,double thresholdCorner){
    
    clock_t start, finish;   
    double elapsed_time;   

    string dir = "Validation/" + filename, filepath;
	DIR *dp;
	struct dirent *dirp;
	struct stat filestat;
	
	dp = opendir( dir.c_str() );
	
	// detecting keypoints
	SiftFeatureDetector detector(threshold, thresholdCorner);
	vector<KeyPoint> keypoints;	
	
	// computing descriptors
    //SiftDescriptorExtractor() extractor;
    Ptr<DescriptorExtractor > extractor(
                                        new OpponentColorDescriptorExtractor(
                                                                             Ptr<DescriptorExtractor>(new SiftDescriptorExtractor())
                                                                             )
                                        );
	Mat descriptors;
	Mat training_descriptors;
	Mat img;
	
	cout << "------- Build Vocabulary ---------\n";
	int count = 0;
	cout << "Extract descriptors.."<<endl;
    start=time(NULL);       
	while ((dirp = readdir( dp )))
    {
		filepath = dir + "/" + dirp->d_name;
		
		// If the file is a directory (or is in some way invalid) we'll skip it 
		if (stat( filepath.c_str(), &filestat )) continue;
		if (S_ISDIR( filestat.st_mode ))         continue;
		
		img = imread(filepath);
		if (!img.data) {
			continue;
		}

		detector.detect(img, keypoints);
        {
            Mat out; img.copyTo(out);
            drawKeypoints(img, keypoints, out, Scalar(255));
            imshow("fg",img);
            imshow("keypoints", out);
            finish=time(NULL);
            elapsed_time = finish-start;
            int hours = (int) elapsed_time / 3600;
            int minutes = (int) (elapsed_time - hours * 3600) / 60;
            int seconds = (int) elapsed_time - hours * 3600 - minutes * 60;
            cout << "Elapsed Time: " << hours << ":" << minutes << ":" << seconds << endl;
            waitKey(0);
        }
		extractor->compute(img, keypoints, descriptors);
		training_descriptors.push_back(descriptors);
		cout << ".";
        count++;
    }
	cout << endl;
	closedir( dp );

    cout << "Total Images Input: " << count << endl;
	cout << "Total descriptors: " << training_descriptors.rows << endl;
    

    finish=time(NULL);
    elapsed_time = finish-start;
    int hours = (int) elapsed_time / 3600;
    int minutes = (int) (elapsed_time - hours * 3600) / 60;
    int seconds = (int) elapsed_time - hours * 3600 - minutes * 60;
    cout << "Elapsed Time for Extracting: " << hours << ":" << minutes << ":" << seconds << endl << endl;
	
	FileStorage fs("training_descriptors_" + filename + "_color.txt", FileStorage::WRITE);
	fs << "training_descriptors" << training_descriptors;
	fs.release();
}
コード例 #2
0
size_t CodeRefactoring::VerifyResult(const TokenIdxSet& targetResult, const wxString& targetText,
                                     bool isLocalVariable)
{
    EditorManager* edMan = Manager::Get()->GetEditorManager();
    cbEditor* editor = edMan->GetBuiltinActiveEditor();
    if (!editor)
        return 0;

    Token* parentOfLocalVariable = _nullptr;
    if (isLocalVariable)
    {
        TRACK_THREAD_LOCKER(s_TokensTreeCritical);
        wxCriticalSectionLocker locker(s_TokensTreeCritical);
        THREAD_LOCKER_SUCCESS(s_TokensTreeCritical);

        TokensTree* tree = m_NativeParser.GetParser().GetTokensTree();
        Token* token = tree->at(*targetResult.begin());
        parentOfLocalVariable = tree->at(token->m_ParentIndex);
    }

    // now that list is filled, we'll search
    cbStyledTextCtrl* control = new cbStyledTextCtrl(editor->GetParent(), wxID_ANY, wxDefaultPosition,
                                                     wxSize(0, 0));
    control->Show(false);

    // styled the text to support control->GetStyleAt()
    cbEditor::ApplyStyles(control);
    EditorColourSet edColSet;

    size_t totalCount = 0;
    for (SearchDataMap::iterator it = m_SearchDataMap.begin(); it != m_SearchDataMap.end(); ++it)
        totalCount += it->second.size();

    // let's create a progress dialog because it might take some time depending on the files count
    wxProgressDialog* progress = new wxProgressDialog(_("Code Refactoring"),
                                                      _("Please wait while verifying result..."),
                                                      totalCount,
                                                      Manager::Get()->GetAppWindow(),
                                                      wxPD_AUTO_HIDE | wxPD_APP_MODAL | wxPD_CAN_ABORT);
    PlaceWindow(progress);

    size_t task = totalCount;
    TokenIdxSet result;
    bool userBreak = false;
    for (SearchDataMap::iterator it = m_SearchDataMap.begin(); it != m_SearchDataMap.end();)
    {
        // check if the file is already opened in built-in editor and do search in it
        cbEditor* ed = edMan->IsBuiltinOpen(it->first);
        if (ed)
            control->SetText(ed->GetControl()->GetText());
        else // else load the file in the control
        {
            EncodingDetector detector(it->first);
            if (!detector.IsOK())
            {
                task -= it->second.size();
                m_SearchDataMap.erase(it++);
                continue; // failed
            }
            control->SetText(detector.GetWxStr());
        }

        // apply the corlor setting
        edColSet.Apply(editor->GetLanguage(), control);

        ccSearchData searchData = { control, it->first };
        for (SearchDataList::iterator itList = it->second.begin(); itList != it->second.end();)
        {
            // update the progress bar
            if (!progress->Update(totalCount - (--task)))
            {
                userBreak = true;
                break; // user pressed "Cancel"
            }

            // skip string or comment
            const int style = control->GetStyleAt(itList->pos);
            if (control->IsString(style) || control->IsComment(style))
            {
                it->second.erase(itList++);
                continue;
            }

            // do cc search
            const int endOfWord = itList->pos + targetText.Len();
            control->GotoPos(endOfWord);
            m_NativeParser.MarkItemsByAI(&searchData, result, true, false, true, endOfWord);
            if (result.empty())
            {
                it->second.erase(itList++);
                continue;
            }

            // verify result
            TokenIdxSet::iterator findIter = targetResult.begin();
            for (; findIter != targetResult.end(); ++findIter)
            {
                if (result.find(*findIter) != result.end())
                    break;
            }

            if (findIter == targetResult.end()) // not found
                it->second.erase(itList++);
            else
            {
                // handle for local variable
                if (isLocalVariable)
                {
                    TRACK_THREAD_LOCKER(s_TokensTreeCritical);
                    wxCriticalSectionLocker locker(s_TokensTreeCritical);
                    THREAD_LOCKER_SUCCESS(s_TokensTreeCritical);

                    TokensTree* tree = m_NativeParser.GetParser().GetTokensTree();
                    Token* token = tree->at(*findIter);
                    if (token)
                    {
                        Token* parent = tree->at(token->m_ParentIndex);
                        if (parent != parentOfLocalVariable)
                        {
                            it->second.erase(itList++);
                            continue;
                        }
                    }
                }

                ++itList;
            }
        }

        if (it->second.empty())
            m_SearchDataMap.erase(it++);
        else
            ++it;

        if (userBreak)
            break;
    }

    delete control; // done with it
    delete progress; // done here too

    return m_SearchDataMap.size();
}