Advertisement
Guest User

Untitled

a guest
Feb 6th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /**
  2.      * Initializing the rois for tracking the hand.
  3.      */
  4.     @Override
  5.     public void initHandRois(Mat rgbFrame) {
  6.  
  7.         roiArray = new Vector<HandRoi>(NSAMPLES);
  8.  
  9.         mGray = new Mat();
  10.         m_hRbga = new Mat();
  11.         mHierarchy = new Mat();
  12.  
  13.         roiArray.add(new HandRoi(new Point(rgbFrame.cols() / 2.7,
  14.                                            rgbFrame.rows() / 5),
  15.                                  new Point(rgbFrame.cols() / 2.7 + square_len,
  16.                                            rgbFrame.rows() / 5 + square_len),
  17.                                  rgbFrame));
  18.  
  19.         roiArray.add(new HandRoi(new Point(rgbFrame.cols() / 3.5,
  20.                                            rgbFrame.rows() / 3.5),
  21.                                  new Point(rgbFrame.cols() / 3.5 + square_len,
  22.                                            rgbFrame.rows() / 3.5 + square_len),
  23.                                  rgbFrame));
  24.  
  25.         roiArray.add(new HandRoi(new Point(rgbFrame.cols() / 2.7,
  26.                                            rgbFrame.rows() / 2.5),
  27.                                  new Point(rgbFrame.cols() / 2.7 + square_len,
  28.                                            rgbFrame.rows() / 2.5 + square_len),
  29.                                  rgbFrame));
  30.  
  31.         roiArray.add(new HandRoi(new Point(rgbFrame.cols() / 5,
  32.                                            rgbFrame.rows() / 2.5),
  33.                                  new Point(rgbFrame.cols() / 5 + square_len,
  34.                                            rgbFrame.rows() / 2.5 + square_len),
  35.                                  rgbFrame));
  36.  
  37.         roiArray.add(new HandRoi(new Point(rgbFrame.cols() / 3.5,
  38.                                            rgbFrame.rows() / 2),
  39.                                  new Point(rgbFrame.cols() / 3.5 + square_len,
  40.                                            rgbFrame.rows() / 2 + square_len),
  41.                                  rgbFrame));
  42.  
  43.         roiArray.add(new HandRoi(new Point(rgbFrame.cols() / 2,
  44.                                            rgbFrame.rows() / 2),
  45.                                  new Point(rgbFrame.cols() / 2 + square_len,
  46.                                            rgbFrame.rows() / 2 + square_len),
  47.                                  rgbFrame));
  48.  
  49.         roiArray.add(new HandRoi(new Point(rgbFrame.cols() / 2.7,
  50.                                            rgbFrame.rows() / 1.7),
  51.                                  new Point(rgbFrame.cols() / 2.7 + square_len,
  52.                                            rgbFrame.rows() / 1.7 + square_len),
  53.                                  rgbFrame));
  54.     }
  55.  
  56.     /**
  57.      * normalize the colors.
  58.      *
  59.      */
  60.     @Override
  61.     public void normalizeColors() {
  62.  
  63.         for (int i = 1; i < NSAMPLES; i++) {
  64.             for (int j = 0; j < 3; j++) {
  65.                 c_lower[i][j] = c_lower[0][j];
  66.                 c_upper[i][j] = c_upper[0][j];
  67.             }
  68.         }
  69.         // normalize all boundaries so that
  70.         // threshold is whithin 0-255
  71.         for (int i = 0; i < NSAMPLES; i++) {
  72.             if ((averageColors[i][0] - c_lower[i][0]) < 0) {
  73.                 c_lower[i][0] = averageColors[i][0];
  74.             }
  75.             if ((averageColors[i][1] - c_lower[i][1]) < 0) {
  76.                 c_lower[i][1] = averageColors[i][1];
  77.             }
  78.             if ((averageColors[i][2] - c_lower[i][2]) < 0) {
  79.                 c_lower[i][2] = averageColors[i][2];
  80.             }
  81.             if ((averageColors[i][0] + c_upper[i][0]) > 255) {
  82.                 c_upper[i][0] = 255 - averageColors[i][0];
  83.             }
  84.             if ((averageColors[i][1] + c_upper[i][1]) > 255) {
  85.                 c_upper[i][1] = 255 - averageColors[i][1];
  86.             }
  87.             if ((averageColors[i][2] + c_upper[i][2]) > 255) {
  88.                 c_upper[i][2] = 255 - averageColors[i][2];
  89.             }
  90.         }
  91.     }
  92.    
  93.     protected void getHSVChannels(HandRoi roi, Mat rgbFrame, int k) {
  94.  
  95.         Rect roiRect = roi.getROIRect();
  96.         Mat roiRegionRgb = rgbFrame.submat(roiRect);
  97.         Mat roiRegionHsv = new Mat();
  98.  
  99.         Imgproc.cvtColor(roiRegionRgb, roiRegionHsv, Imgproc.COLOR_RGB2HSV_FULL);
  100.         Scalar mBlob = Core.sumElems(roiRegionHsv);
  101.         int pointCount = roiRect.width * roiRect.height;
  102.         for (int i = 0; i < mBlob.val.length; i++) {
  103.             mBlob.val[i] /= pointCount;
  104.         }
  105.  
  106.         averageColors[k][0] = Double.valueOf(mBlob.val[0]) != null ? (int) mBlob.val[0] : 0;
  107.         averageColors[k][1] = Double.valueOf(mBlob.val[1]) != null ? (int) mBlob.val[1] : 0;
  108.         averageColors[k][2] = Double.valueOf(mBlob.val[2]) != null ? (int) mBlob.val[2] : 0;
  109.     }
  110.  
  111.     @Override
  112.     public void computeBinaries(Mat mRgba) {
  113.  
  114.         // matList.clear();
  115.         for (int i = 0; i < NSAMPLES; i++) {
  116.             // normalizeColors();
  117.             setLowerUpperBounds(i);
  118.             matList.add(new Mat(m_hRbga.rows(),
  119.                                 m_hRbga.cols(),
  120.                                 CvType.CV_8U));
  121.             Core.inRange(m_hRbga, lowerBound, upperBound, matList.get(i));
  122.         }
  123.  
  124.         matList.get(0).copyTo(mGray);
  125.  
  126.         for (int i = 1; i < NSAMPLES; i++) {
  127.             // Core.add
  128.             Core.add(matList.get(i), mGray, mGray);
  129.         }
  130.         Imgproc.cvtColor(m_hRbga, m_hRbga, Imgproc.COLOR_HSV2RGB_FULL);
  131.         Imgproc.medianBlur(mGray, mGray, 7);
  132.         Imgproc.pyrUp(mGray, mGray);
  133.  
  134.     }
  135.  
  136.     @Override
  137.     public void findContours(Mat rgbImg) {
  138.  
  139.         // Imgproc.pyrUp(mGray, mGray);
  140.         Mat mGrayCopy = new Mat();
  141.         mGray.copyTo(mGrayCopy);
  142.         List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
  143.         MatOfInt hullI = new MatOfInt();
  144.         List<MatOfPoint> hullPoints = new ArrayList<MatOfPoint>();
  145.         // Imgproc.find
  146.         MatOfInt4 convexDefect = new MatOfInt4();
  147.         Imgproc.findContours(mGrayCopy, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
  148.         if (contours != null && contours.size() > 0) {
  149.             index = findBiggestContour(contours);
  150.             if (index != -1) {
  151.                 MatOfPoint contour = contours.get(index);
  152.                 mRect = Imgproc.boundingRect(contour);
  153.                 Imgproc.convexHull(contour, hullI);
  154.                 // Imgproc.convexHull(, hull);
  155.                 // Imgproc.con
  156.                 Imgproc.approxPolyDP(new MatOfPoint2f(contour.toArray()), new MatOfPoint2f(), 3, true);
  157.                 if (contour.toArray().length > 3) {
  158.                     // It is computed(seems) for reasons of drawing the contour based
  159.                     // on these hull points.
  160.                     hullPoints = computeHullPoints(hullI, contour);
  161.                     Imgproc.convexityDefects(contour, hullI, convexDefect);
  162.                     if (!convexDefect.empty()) {
  163.                         List<MatOfInt4> newConvexDefects = computeAndEliminateDefects(contour, hullI, convexDefect);
  164.                         // drawContours(rgbImg, contours);
  165.                         getFingerTips(rgbImg, newConvexDefects, contour);
  166.                     }
  167.                 }
  168.                 boolean isHand = detectIfHandContour();
  169.                 if (isHand) {
  170.                     drawContours(rgbImg, contours);
  171.                     drawFingerTips(rgbImg);
  172.                 }
  173.             }
  174.         }
  175.     }
  176.  
  177.     private void setLowerUpperBounds(int i) {
  178.  
  179.         lowerBound = new Scalar(averageColors[i][0] - c_lower[i][0],
  180.                                 averageColors[i][1] - c_lower[i][1],
  181.                                 averageColors[i][2] - c_lower[i][2]);
  182.         upperBound = new Scalar(averageColors[i][0] + c_upper[i][0],
  183.                                 averageColors[i][1] + c_upper[i][1],
  184.                                 averageColors[i][2] + c_upper[i][2]);
  185.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement