Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Initializing the rois for tracking the hand.
- */
- @Override
- public void initHandRois(Mat rgbFrame) {
- roiArray = new Vector<HandRoi>(NSAMPLES);
- mGray = new Mat();
- m_hRbga = new Mat();
- mHierarchy = new Mat();
- roiArray.add(new HandRoi(new Point(rgbFrame.cols() / 2.7,
- rgbFrame.rows() / 5),
- new Point(rgbFrame.cols() / 2.7 + square_len,
- rgbFrame.rows() / 5 + square_len),
- rgbFrame));
- roiArray.add(new HandRoi(new Point(rgbFrame.cols() / 3.5,
- rgbFrame.rows() / 3.5),
- new Point(rgbFrame.cols() / 3.5 + square_len,
- rgbFrame.rows() / 3.5 + square_len),
- rgbFrame));
- roiArray.add(new HandRoi(new Point(rgbFrame.cols() / 2.7,
- rgbFrame.rows() / 2.5),
- new Point(rgbFrame.cols() / 2.7 + square_len,
- rgbFrame.rows() / 2.5 + square_len),
- rgbFrame));
- roiArray.add(new HandRoi(new Point(rgbFrame.cols() / 5,
- rgbFrame.rows() / 2.5),
- new Point(rgbFrame.cols() / 5 + square_len,
- rgbFrame.rows() / 2.5 + square_len),
- rgbFrame));
- roiArray.add(new HandRoi(new Point(rgbFrame.cols() / 3.5,
- rgbFrame.rows() / 2),
- new Point(rgbFrame.cols() / 3.5 + square_len,
- rgbFrame.rows() / 2 + square_len),
- rgbFrame));
- roiArray.add(new HandRoi(new Point(rgbFrame.cols() / 2,
- rgbFrame.rows() / 2),
- new Point(rgbFrame.cols() / 2 + square_len,
- rgbFrame.rows() / 2 + square_len),
- rgbFrame));
- roiArray.add(new HandRoi(new Point(rgbFrame.cols() / 2.7,
- rgbFrame.rows() / 1.7),
- new Point(rgbFrame.cols() / 2.7 + square_len,
- rgbFrame.rows() / 1.7 + square_len),
- rgbFrame));
- }
- /**
- * normalize the colors.
- *
- */
- @Override
- public void normalizeColors() {
- for (int i = 1; i < NSAMPLES; i++) {
- for (int j = 0; j < 3; j++) {
- c_lower[i][j] = c_lower[0][j];
- c_upper[i][j] = c_upper[0][j];
- }
- }
- // normalize all boundaries so that
- // threshold is whithin 0-255
- for (int i = 0; i < NSAMPLES; i++) {
- if ((averageColors[i][0] - c_lower[i][0]) < 0) {
- c_lower[i][0] = averageColors[i][0];
- }
- if ((averageColors[i][1] - c_lower[i][1]) < 0) {
- c_lower[i][1] = averageColors[i][1];
- }
- if ((averageColors[i][2] - c_lower[i][2]) < 0) {
- c_lower[i][2] = averageColors[i][2];
- }
- if ((averageColors[i][0] + c_upper[i][0]) > 255) {
- c_upper[i][0] = 255 - averageColors[i][0];
- }
- if ((averageColors[i][1] + c_upper[i][1]) > 255) {
- c_upper[i][1] = 255 - averageColors[i][1];
- }
- if ((averageColors[i][2] + c_upper[i][2]) > 255) {
- c_upper[i][2] = 255 - averageColors[i][2];
- }
- }
- }
- protected void getHSVChannels(HandRoi roi, Mat rgbFrame, int k) {
- Rect roiRect = roi.getROIRect();
- Mat roiRegionRgb = rgbFrame.submat(roiRect);
- Mat roiRegionHsv = new Mat();
- Imgproc.cvtColor(roiRegionRgb, roiRegionHsv, Imgproc.COLOR_RGB2HSV_FULL);
- Scalar mBlob = Core.sumElems(roiRegionHsv);
- int pointCount = roiRect.width * roiRect.height;
- for (int i = 0; i < mBlob.val.length; i++) {
- mBlob.val[i] /= pointCount;
- }
- averageColors[k][0] = Double.valueOf(mBlob.val[0]) != null ? (int) mBlob.val[0] : 0;
- averageColors[k][1] = Double.valueOf(mBlob.val[1]) != null ? (int) mBlob.val[1] : 0;
- averageColors[k][2] = Double.valueOf(mBlob.val[2]) != null ? (int) mBlob.val[2] : 0;
- }
- @Override
- public void computeBinaries(Mat mRgba) {
- // matList.clear();
- for (int i = 0; i < NSAMPLES; i++) {
- // normalizeColors();
- setLowerUpperBounds(i);
- matList.add(new Mat(m_hRbga.rows(),
- m_hRbga.cols(),
- CvType.CV_8U));
- Core.inRange(m_hRbga, lowerBound, upperBound, matList.get(i));
- }
- matList.get(0).copyTo(mGray);
- for (int i = 1; i < NSAMPLES; i++) {
- // Core.add
- Core.add(matList.get(i), mGray, mGray);
- }
- Imgproc.cvtColor(m_hRbga, m_hRbga, Imgproc.COLOR_HSV2RGB_FULL);
- Imgproc.medianBlur(mGray, mGray, 7);
- Imgproc.pyrUp(mGray, mGray);
- }
- @Override
- public void findContours(Mat rgbImg) {
- // Imgproc.pyrUp(mGray, mGray);
- Mat mGrayCopy = new Mat();
- mGray.copyTo(mGrayCopy);
- List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
- MatOfInt hullI = new MatOfInt();
- List<MatOfPoint> hullPoints = new ArrayList<MatOfPoint>();
- // Imgproc.find
- MatOfInt4 convexDefect = new MatOfInt4();
- Imgproc.findContours(mGrayCopy, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
- if (contours != null && contours.size() > 0) {
- index = findBiggestContour(contours);
- if (index != -1) {
- MatOfPoint contour = contours.get(index);
- mRect = Imgproc.boundingRect(contour);
- Imgproc.convexHull(contour, hullI);
- // Imgproc.convexHull(, hull);
- // Imgproc.con
- Imgproc.approxPolyDP(new MatOfPoint2f(contour.toArray()), new MatOfPoint2f(), 3, true);
- if (contour.toArray().length > 3) {
- // It is computed(seems) for reasons of drawing the contour based
- // on these hull points.
- hullPoints = computeHullPoints(hullI, contour);
- Imgproc.convexityDefects(contour, hullI, convexDefect);
- if (!convexDefect.empty()) {
- List<MatOfInt4> newConvexDefects = computeAndEliminateDefects(contour, hullI, convexDefect);
- // drawContours(rgbImg, contours);
- getFingerTips(rgbImg, newConvexDefects, contour);
- }
- }
- boolean isHand = detectIfHandContour();
- if (isHand) {
- drawContours(rgbImg, contours);
- drawFingerTips(rgbImg);
- }
- }
- }
- }
- private void setLowerUpperBounds(int i) {
- lowerBound = new Scalar(averageColors[i][0] - c_lower[i][0],
- averageColors[i][1] - c_lower[i][1],
- averageColors[i][2] - c_lower[i][2]);
- upperBound = new Scalar(averageColors[i][0] + c_upper[i][0],
- averageColors[i][1] + c_upper[i][1],
- averageColors[i][2] + c_upper[i][2]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement