List of usage examples for org.opencv.core MatOfInt toArray
public int[] toArray()
From source file:logic.featurepointextractor.MouthFPE.java
/** * Detect mouth feature points//www . ja v a 2 s. c om * Algorithm: Equalize histogram of mouth rect * Implement Sobel horizontal filter * Find corners * Invert color + Binarization * Find lip up and down points * @param mc * @return */ @Override public Point[] detect(MatContainer mc) { /**Algorithm * find pix(i) = (R-G)/R * normalize: 2arctan(pix(i))/pi */ //find pix(i) = (R-G)/R Mat mouthRGBMat = mc.origFrame.submat(mc.mouthRect); List mouthSplitChannelsList = new ArrayList<Mat>(); Core.split(mouthRGBMat, mouthSplitChannelsList); //extract R-channel Mat mouthR = (Mat) mouthSplitChannelsList.get(2); mouthR.convertTo(mouthR, CvType.CV_64FC1); //extract G-channel Mat mouthG = (Mat) mouthSplitChannelsList.get(1); mouthG.convertTo(mouthG, CvType.CV_64FC1); //calculate (R-G)/R Mat dst = new Mat(mouthR.rows(), mouthR.cols(), CvType.CV_64FC1); mc.mouthProcessedMat = new Mat(mouthR.rows(), mouthR.cols(), CvType.CV_64FC1); Core.absdiff(mouthR, mouthG, dst); // Core.divide(dst, mouthR, mc.mouthProcessedMat); mc.mouthProcessedMat = dst; mc.mouthProcessedMat.convertTo(mc.mouthProcessedMat, CvType.CV_8UC1); Imgproc.equalizeHist(mc.mouthProcessedMat, mc.mouthProcessedMat); // Imgproc.blur(mc.mouthProcessedMat, mc.mouthProcessedMat, new Size(4,4)); // Imgproc.morphologyEx(mc.mouthProcessedMat, mc.mouthProcessedMat, Imgproc.MORPH_OPEN, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(4,4))); Imgproc.threshold(mc.mouthProcessedMat, mc.mouthProcessedMat, 230, 255, THRESH_BINARY); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(mc.mouthProcessedMat, contours, new Mat(), Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); //find the biggest contour int maxSize = -1; int tmpSize = -1; int index = -1; Rect centMouthRect = new Rect(mc.mouthRect.x + mc.mouthRect.width / 4, mc.mouthRect.y + mc.mouthRect.height / 4, mc.mouthRect.width / 2, mc.mouthRect.height / 2); if (contours.size() != 0) { maxSize = contours.get(0).toArray().length; tmpSize = 0; index = 0; } //find max contour for (int j = 0; j < contours.size(); ++j) { //if contour is vertical, exclude it Rect boundRect = Imgproc.boundingRect(contours.get(j)); int centX = mc.mouthRect.x + boundRect.x + boundRect.width / 2; int centY = mc.mouthRect.y + boundRect.y + boundRect.height / 2; // LOG.info("Center = " + centX + "; " + centY); // LOG.info("Rect = " + centMouthRect.x + "; " + centMouthRect.y); if (!centMouthRect.contains(new Point(centX, centY))) continue; tmpSize = contours.get(j).toArray().length; LOG.info("Contour " + j + "; size = " + tmpSize); if (tmpSize > maxSize) { maxSize = tmpSize; index = j; } } //appproximate curve Point[] p1 = contours.get(index).toArray(); MatOfPoint2f p2 = new MatOfPoint2f(p1); MatOfPoint2f p3 = new MatOfPoint2f(); Imgproc.approxPolyDP(p2, p3, 1, true); p1 = p3.toArray(); MatOfInt tmpMatOfPoint = new MatOfInt(); Imgproc.convexHull(new MatOfPoint(p1), tmpMatOfPoint); Rect boundRect = Imgproc.boundingRect(new MatOfPoint(p1)); if (boundRect.area() / mc.mouthRect.area() > 0.3) return null; int size = (int) tmpMatOfPoint.size().height; Point[] _p1 = new Point[size]; int[] a = tmpMatOfPoint.toArray(); _p1[0] = new Point(p1[a[0]].x + mc.mouthRect.x, p1[a[0]].y + mc.mouthRect.y); Core.circle(mc.origFrame, _p1[0], 3, new Scalar(0, 0, 255), -1); for (int i = 1; i < size; i++) { _p1[i] = new Point(p1[a[i]].x + mc.mouthRect.x, p1[a[i]].y + mc.mouthRect.y); Core.circle(mc.origFrame, _p1[i], 3, new Scalar(0, 0, 255), -1); Core.line(mc.origFrame, _p1[i - 1], _p1[i], new Scalar(255, 0, 0), 2); } Core.line(mc.origFrame, _p1[size - 1], _p1[0], new Scalar(255, 0, 0), 2); /* contours.set(index, new MatOfPoint(_p1)); mc.mouthProcessedMat.setTo(new Scalar(0)); Imgproc.drawContours(mc.mouthProcessedMat, contours, index, new Scalar(255), -1); */ mc.mouthMatOfPoint = _p1; MatOfPoint matOfPoint = new MatOfPoint(_p1); mc.mouthBoundRect = Imgproc.boundingRect(matOfPoint); mc.features.mouthBoundRect = mc.mouthBoundRect; /**extract feature points: 1 most left * 2 most right * 3,4 up * 5,6 down */ // mc.mouthMatOfPoint = extractFeaturePoints(contours.get(index)); return null; }
From source file:tv.danmaku.ijk.media.example.activities.VideoActivity.java
License:Apache License
public Mat onCameraFrame(CvCameraViewFrame inputFrame) { mRgba = inputFrame.rgba();/*from ww w . j av a2s. co m*/ mGray = inputFrame.gray(); // return mRgba; // iThreshold = 10000; //Imgproc.blur(mRgba, mRgba, new Size(5,5)); Imgproc.GaussianBlur(mRgba, mRgba, new org.opencv.core.Size(3, 3), 1, 1); //Imgproc.medianBlur(mRgba, mRgba, 3); if (!mIsColorSelected) return mRgba; List<MatOfPoint> contours = mDetector.getContours(); mDetector.process(mRgba); Log.d(TAG, "Contours count: " + contours.size()); if (contours.size() <= 0) { return mRgba; } RotatedRect rect = Imgproc.minAreaRect(new MatOfPoint2f(contours.get(0).toArray())); double boundWidth = rect.size.width; double boundHeight = rect.size.height; int boundPos = 0; for (int i = 1; i < contours.size(); i++) { rect = Imgproc.minAreaRect(new MatOfPoint2f(contours.get(i).toArray())); if (rect.size.width * rect.size.height > boundWidth * boundHeight) { boundWidth = rect.size.width; boundHeight = rect.size.height; boundPos = i; } } Rect boundRect = Imgproc.boundingRect(new MatOfPoint(contours.get(boundPos).toArray())); Imgproc.rectangle(mRgba, boundRect.tl(), boundRect.br(), CONTOUR_COLOR_WHITE, 2, 8, 0); Log.d(TAG, " Row start [" + (int) boundRect.tl().y + "] row end [" + (int) boundRect.br().y + "] Col start [" + (int) boundRect.tl().x + "] Col end [" + (int) boundRect.br().x + "]"); int rectHeightThresh = 0; double a = boundRect.br().y - boundRect.tl().y; a = a * 0.7; a = boundRect.tl().y + a; Log.d(TAG, " A [" + a + "] br y - tl y = [" + (boundRect.br().y - boundRect.tl().y) + "]"); //Core.rectangle( mRgba, boundRect.tl(), boundRect.br(), CONTOUR_COLOR, 2, 8, 0 ); Imgproc.rectangle(mRgba, boundRect.tl(), new Point(boundRect.br().x, a), CONTOUR_COLOR, 2, 8, 0); MatOfPoint2f pointMat = new MatOfPoint2f(); Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(boundPos).toArray()), pointMat, 3, true); contours.set(boundPos, new MatOfPoint(pointMat.toArray())); MatOfInt hull = new MatOfInt(); MatOfInt4 convexDefect = new MatOfInt4(); Imgproc.convexHull(new MatOfPoint(contours.get(boundPos).toArray()), hull); if (hull.toArray().length < 3) return mRgba; Imgproc.convexityDefects(new MatOfPoint(contours.get(boundPos).toArray()), hull, convexDefect); List<MatOfPoint> hullPoints = new LinkedList<MatOfPoint>(); List<Point> listPo = new LinkedList<Point>(); for (int j = 0; j < hull.toList().size(); j++) { listPo.add(contours.get(boundPos).toList().get(hull.toList().get(j))); } MatOfPoint e = new MatOfPoint(); e.fromList(listPo); hullPoints.add(e); List<MatOfPoint> defectPoints = new LinkedList<MatOfPoint>(); List<Point> listPoDefect = new LinkedList<Point>(); for (int j = 0; j < convexDefect.toList().size(); j = j + 4) { Point farPoint = contours.get(boundPos).toList().get(convexDefect.toList().get(j + 2)); Integer depth = convexDefect.toList().get(j + 3); if (depth > iThreshold && farPoint.y < a) { listPoDefect.add(contours.get(boundPos).toList().get(convexDefect.toList().get(j + 2))); } Log.d(TAG, "defects [" + j + "] " + convexDefect.toList().get(j + 3)); } MatOfPoint e2 = new MatOfPoint(); e2.fromList(listPo); defectPoints.add(e2); Log.d(TAG, "hull: " + hull.toList()); Log.d(TAG, "defects: " + convexDefect.toList()); Imgproc.drawContours(mRgba, hullPoints, -1, CONTOUR_COLOR, 3); int defectsTotal = (int) convexDefect.total(); Log.d(TAG, "Defect total " + defectsTotal); this.numberOfFingers = listPoDefect.size(); if (this.numberOfFingers > 5) this.numberOfFingers = 5; mHandler.post(mUpdateFingerCountResults); for (Point p : listPoDefect) { Imgproc.circle(mRgba, p, 6, new Scalar(255, 0, 255)); } return mRgba; }