Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Get fingertips from binary img (Reference:http://www.javaadvent.com/2012/12/hand-and-finger-detection-using-javacv.html)
- String finger2SVM(Mat img, int fingers)
- {
- String result = null;
- defectMat.fromList(defectPoints);
- List<Integer> dList = defects.toList();
- Point[] contPts = contours.get(cMaxId).toArray();
- Point pastDefVec = null;
- int i;
- for (i = 0; i < nextDefectId.size(); i++)
- {
- int currentId = nextDefectId.get(i);
- int curId = dList.get(currentId);
- Point curDefectPt = contPts[curId];
- Point curDefVec = new Point();
- curDefVec.x = curDefectPt.x - inCircle.x;
- curDefVec.y = curDefectPt.y - inCircle.y;
- if (pastDefVec != null) {
- double dotProduct = curDefVec.x*pastDefVec.x +
- curDefVec.y*pastDefVec.y;
- double crossProduct = curDefVec.x*pastDefVec.y -
- pastDefVec.x*curDefVec.y;
- if (crossProduct <= 0)
- break;
- }
- pastDefVec = curDefVec;
- }
- int beginId = i;
- int currtId = 0;
- ArrayList<Point> fingerTips = new ArrayList<Point>();
- if (nextDefectId.size() > 0) {
- boolean end = false;
- for (int j = beginId; ; j++)
- {
- if (j == nextDefectId.size())
- {
- if (end == false) {
- j = 0;
- end = true;
- }
- else
- break;
- }
- if ((j == beginId) && (end == true))
- break;
- int currentId = nextDefectId.get(j);
- int curId = dList.get(currentId);
- Point resultPts = contPts[curId];
- Point fin0 = contPts[dList.get(currentId-2)];
- Point fin1 = contPts[dList.get(currentId-1)];
- fingerTips.add(fin0);
- fingerTips.add(fin1);
- Core.circle(img, resultPts, 2, new Scalar(0, 0, 255), -5);//Good defect in resultPts
- currtId++;
- }
- }
- int k = 0;
- features.clear();
- for (int j = 0; j < fingerTips.size(); )
- {
- if (k > 5) // only 5 fingers
- break;
- Point resultFingers = fingerTips.get(j);
- if ((j%2 == 0)) {
- if (j != 0) {
- Point prevFinPoint = fingerTips.get(j-1);
- resultFingers.x = (resultFingers.x + prevFinPoint.x)/2;
- resultFingers.y = (resultFingers.y + prevFinPoint.y)/2;
- }
- if (j == (fingerTips.size() - 2) )
- j++;
- else
- j += 2;
- } else
- j++;
- Point disFinger = new Point(resultFingers.x-inCircle.x, resultFingers.y-inCircle.y);
- double dis = Math.sqrt(disFinger.x*disFinger.x+disFinger.y*disFinger.y);
- Double f1 = (disFinger.x)/inCircleRadius;
- Double f2 = (disFinger.y)/inCircleRadius;
- features.add(f1);
- features.add(f2);
- Core.line(img, inCircle, resultFingers, new Scalar(24, 77, 9), 2);
- Core.circle(img, resultFingers, 2, Scalar.all(0), -5);
- Core.putText(img, Integer.toString(count), new Point(resultFingers.x - 10, resultFingers.y - 10), Core.FONT_SIMPLEX, 0.5, Scalar.all(0));
- k++;
- }
- result = fingerSVMInput(fingers);
- return result;
- }
- //Convert to SVM input file
- String fingerSVMInput(int label)
- {
- String result = Integer.toString(label) + " ";
- int i;
- for (i = 0; i < features.size(); i++)
- {
- int label = i + 1;
- result = result + label + ":" + features.get(i) + " ";
- }
- result = result + "\n";
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement