Advertisement
Treeloy

Untitled

Mar 13th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. //Get fingertips from binary img (Reference:http://www.javaadvent.com/2012/12/hand-and-finger-detection-using-javacv.html)
  2.     String finger2SVM(Mat img, int fingers)
  3.     {
  4.         String result = null;
  5.        
  6.    
  7.        
  8.         defectMat.fromList(defectPoints);
  9.        
  10.         List<Integer> dList = defects.toList();
  11.         Point[] contPts = contours.get(cMaxId).toArray();
  12.         Point pastDefVec = null;
  13.         int i;
  14.         for (i = 0; i < nextDefectId.size(); i++)
  15.         {
  16.             int currentId = nextDefectId.get(i);
  17.             int curId = dList.get(currentId);
  18.            
  19.             Point curDefectPt = contPts[curId];
  20.             Point curDefVec = new Point();
  21.             curDefVec.x = curDefectPt.x - inCircle.x;
  22.             curDefVec.y = curDefectPt.y - inCircle.y;
  23.            
  24.             if (pastDefVec != null) {
  25.                 double dotProduct = curDefVec.x*pastDefVec.x +
  26.                         curDefVec.y*pastDefVec.y;
  27.                 double crossProduct = curDefVec.x*pastDefVec.y -
  28.                         pastDefVec.x*curDefVec.y;              
  29.                 if (crossProduct <= 0)
  30.                     break;
  31.             }          
  32.             pastDefVec = curDefVec;
  33.            
  34.         }
  35.        
  36.         int beginId = i;
  37.         int currtId = 0;
  38.        
  39.         ArrayList<Point> fingerTips = new ArrayList<Point>();
  40.        
  41.         if (nextDefectId.size() > 0) {
  42.             boolean end = false;
  43.            
  44.             for (int j = beginId; ; j++)
  45.             {
  46.                 if (j == nextDefectId.size())
  47.                         {
  48.                    
  49.                     if (end == false) {
  50.                         j = 0;
  51.                         end = true;
  52.                     }
  53.                     else
  54.                         break;
  55.                 }
  56.            
  57.                 if ((j == beginId) && (end == true))
  58.                     break;
  59.                
  60.                 int currentId = nextDefectId.get(j);
  61.                 int curId = dList.get(currentId);
  62.                
  63.                 Point resultPts = contPts[curId];
  64.                 Point fin0 = contPts[dList.get(currentId-2)];
  65.                 Point fin1 = contPts[dList.get(currentId-1)];
  66.                 fingerTips.add(fin0);
  67.                 fingerTips.add(fin1);          
  68.                
  69.                 Core.circle(img, resultPts, 2, new Scalar(0, 0, 255), -5);//Good defect in resultPts
  70.            
  71.                 currtId++;
  72.             }
  73.        
  74.         }
  75.        
  76.         int k = 0;
  77.         features.clear();
  78.         for (int j = 0; j < fingerTips.size(); )
  79.         {
  80.             if (k > 5) // only 5 fingers
  81.                 break;         
  82.             Point resultFingers = fingerTips.get(j);           
  83.             if ((j%2 == 0)) {              
  84.                 if (j != 0) {
  85.                     Point prevFinPoint = fingerTips.get(j-1);
  86.                     resultFingers.x = (resultFingers.x + prevFinPoint.x)/2;
  87.                     resultFingers.y = (resultFingers.y + prevFinPoint.y)/2;
  88.                 }              
  89.                 if (j == (fingerTips.size() - 2) )
  90.                     j++;
  91.                 else
  92.                     j += 2;
  93.             } else
  94.                 j++;           
  95.             Point disFinger = new Point(resultFingers.x-inCircle.x, resultFingers.y-inCircle.y);
  96.             double dis = Math.sqrt(disFinger.x*disFinger.x+disFinger.y*disFinger.y);
  97.             Double f1 = (disFinger.x)/inCircleRadius;
  98.             Double f2 = (disFinger.y)/inCircleRadius;
  99.             features.add(f1);
  100.             features.add(f2);
  101.            
  102.            
  103.             Core.line(img, inCircle, resultFingers, new Scalar(24, 77, 9), 2);
  104.             Core.circle(img, resultFingers, 2, Scalar.all(0), -5);
  105.            
  106.             Core.putText(img, Integer.toString(count), new Point(resultFingers.x - 10, resultFingers.y - 10), Core.FONT_SIMPLEX, 0.5, Scalar.all(0));
  107.            
  108.             k++;
  109.                        
  110.         }
  111.        
  112.         result = fingerSVMInput(fingers);
  113.            
  114.        
  115.        
  116.        
  117.         return result;
  118.     }
  119.  
  120.  
  121. //Convert to SVM input file
  122.     String fingerSVMInput(int label)
  123.     {
  124.         String result = Integer.toString(label) + " ";
  125.         int i;
  126.         for (i = 0; i < features.size(); i++)
  127.         {
  128.             int label = i + 1;
  129.             result = result + label + ":" + features.get(i) + " ";
  130.         }
  131.         result = result + "\n";
  132.         return result;
  133.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement