Ejemplo n.º 1
0
vector<FindResult> 
Vision::find(ScreenImage simg, Pattern ptn) { 

//	SYSTEMTIME begin, end;
    
//    GetSystemTime(&begin);

   Mat screenImage = simg.getMat();
      
   vector<FindResult> results;
   if (ptn.bAll()){
      
      results = getFindResults(screenImage, ptn, true, ptn.getSimilarity());
      
      
      if (ptn.getOrdering() == TOPDOWN){
         sort(results.begin(), results.end(), sort_by_y_asc);
      }else if (ptn.getOrdering() == BOTTOMUP){
         sort(results.begin(), results.end(), sort_by_y_dsc);
      }else if (ptn.getOrdering() == LEFTRIGHT){
         sort(results.begin(), results.end(), sort_by_x_asc);
      }else if (ptn.getOrdering() == RIGHTLEFT){
         sort(results.begin(), results.end(), sort_by_x_dsc);
      }
   
   
   }else{
      
      if (ptn.where() != ANYWHERE){
      
         results = getFindResults(screenImage, ptn, true, ptn.getSimilarity());      
            
         
         cout << "found " << results.size() << " matches." << endl;
         
         if (!results.empty()){
            // Filter Results
            FindResult r = results[0];
            for (int i=1; i<results.size(); ++i){
               FindResult& ri = results[i];
               if ((ptn.where() == TOPMOST && ri.y < r.y) ||
                   (ptn.where() == BOTTOMMOST && ri.y > r.y) ||
                   (ptn.where() == LEFTMOST && ri.x < r.x) ||
                   (ptn.where() == RIGHTMOST && ri.x > r.x))
                  r = ri;
            }
            
            results.clear();
            results.push_back(r);
         }
      }
      else {

         results = getFindResults(screenImage, ptn, false, ptn.getSimilarity());
            
      }
      
   }
   
   vector<FindResult> final_results;
   int n = min((int)results.size(), (int)ptn.getLimit());
   for (int i=0; i< n; ++i){
      final_results.push_back(results[i]);
   }
   
//    GetSystemTime(&end);
//	WORD elapsed_ms = (end.wSecond - begin.wSecond)*1000+(end.wMilliseconds - begin.wMilliseconds);
//	cout << elapsed_ms << " ms." << endl;

   return final_results;
}
Ejemplo n.º 2
0
vector<Match>
Region::doFind(Pattern target) {

 
   dout << "[Region::doFind] Searching in (" << xo+x << "," << yo+y << ")-(" << xo+x+w << "," << yo+y+h << ")" << endl; 
  
   ScreenImage simg = capture();

   FindInput* fi;
   
   if (target.isText())
      fi = new FindInput(simg.getMat(), TARGET_TYPE_TEXT, target.getText());
   else
      fi = new FindInput(simg.getMat(), TARGET_TYPE_IMAGE, target.getImageURL());   
      

   FindInput& input = *fi;   
   input.setFindAll(target.bAll());
   input.setLimit(target.getLimit());
   input.setSimilarity(target.getSimilarity());
   
   
  if (!target.bAll() && target.where() != ANYWHERE){
     input.setFindAll(true);
  }
   
   vector<FindResult> results = Vision::find(input);
   delete fi;
   
            
   if (target.getOrdering() == TOPDOWN){
      sort(results.begin(), results.end(), sort_by_y_asc);
   }else if (target.getOrdering() == BOTTOMUP){
      sort(results.begin(), results.end(), sort_by_y_dsc);
   }else if (target.getOrdering() == LEFTRIGHT){
      sort(results.begin(), results.end(), sort_by_x_asc);
   }else if (target.getOrdering() == RIGHTLEFT){
      sort(results.begin(), results.end(), sort_by_x_dsc);
   }
   
   if (!target.bAll() && target.where() != ANYWHERE){   
         if (!results.empty()){
            // Filter Results
            FindResult r = results[0];
            for (int i=1; i<results.size(); ++i){
               FindResult& ri = results[i];
               if ((target.where() == TOPMOST && ri.y < r.y) ||
                   (target.where() == BOTTOMMOST && ri.y > r.y) ||
                   (target.where() == LEFTMOST && ri.x < r.x) ||
                   (target.where() == RIGHTMOST && ri.x > r.x))
                  r = ri;
            }
            
            results.clear();
            results.push_back(r);
         }
   }   
   
   vector<Match> matches;
   int n = min((int)results.size(), (int)target.getLimit());
   for (int i=0; i< n; ++i){
      FindResult& r = results[i];
      Match match(inner(r.x,r.y,r.w,r.h),r.score); 
      matches.push_back(match);
   }
   
   
   if (!matches.empty()){
      SikuliUI::sikuliUI->handleMatchFound(*this, target, matches);

      setLastMatch(matches[0]);
      setLastMatches(matches);
   }
   
   // TODO: setTargetOffset 
   //match.setTargetOffset(ptn.getTargetOffset());
   return matches;
}