List of usage examples for org.opencv.core Mat cols
public int cols()
From source file:org.akvo.caddisfly.sensor.colorimetry.strip.util.OpenCVUtil.java
License:Open Source License
@SuppressWarnings("UnusedParameters") public static Mat detectStrip(Mat stripArea, StripTest.Brand brand, double ratioW, double ratioH) { List<Mat> channels = new ArrayList<>(); Mat sArea = stripArea.clone();/* w w w .j a v a2 s . com*/ // Gaussian blur Imgproc.medianBlur(sArea, sArea, 3); Core.split(sArea, channels); // create binary image Mat binary = new Mat(); // determine min and max NOT USED Imgproc.threshold(channels.get(0), binary, 128, MAX_RGB_INT_VALUE, Imgproc.THRESH_BINARY); // compute first approximation of line through length of the strip final WeightedObservedPoints points = new WeightedObservedPoints(); final WeightedObservedPoints corrPoints = new WeightedObservedPoints(); double tot, yTot; for (int i = 0; i < binary.cols(); i++) { // iterate over cols tot = 0; yTot = 0; for (int j = 0; j < binary.rows(); j++) { // iterate over rows if (binary.get(j, i)[0] > 128) { yTot += j; tot++; } } if (tot > 0) { points.add((double) i, yTot / tot); } } // order of coefficients is (b + ax), so [b, a] final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(1); List<WeightedObservedPoint> pointsList = points.toList(); final double[] coefficient = fitter.fit(pointsList); // second pass, remove outliers double estimate, actual; for (int i = 0; i < pointsList.size(); i++) { estimate = coefficient[1] * pointsList.get(i).getX() + coefficient[0]; actual = pointsList.get(i).getY(); if (actual > LOWER_PERCENTAGE_BOUND * estimate && actual < UPPER_PERCENTAGE_BOUND * estimate) { //if the point differs less than +/- 10%, keep the point corrPoints.add(pointsList.get(i).getX(), pointsList.get(i).getY()); } } final double[] coefficientCorr = fitter.fit(corrPoints.toList()); double slope = coefficientCorr[1]; double offset = coefficientCorr[0]; // compute rotation angle double rotAngleDeg = Math.atan(slope) * 180 / Math.PI; //determine a point on the line, in the middle of strip, in the horizontal middle of the whole image int midPointX = binary.cols() / 2; int midPointY = (int) Math.round(midPointX * slope + offset); // rotate around the midpoint, to straighten the binary strip Mat dstBinary = new Mat(binary.rows(), binary.cols(), binary.type()); Point center = new Point(midPointX, midPointY); Mat rotMat = Imgproc.getRotationMatrix2D(center, rotAngleDeg, 1.0); Imgproc.warpAffine(binary, dstBinary, rotMat, binary.size(), Imgproc.INTER_CUBIC + Imgproc.WARP_FILL_OUTLIERS); // also apply rotation to colored strip Mat dstStrip = new Mat(stripArea.rows(), stripArea.cols(), stripArea.type()); Imgproc.warpAffine(stripArea, dstStrip, rotMat, binary.size(), Imgproc.INTER_CUBIC + Imgproc.WARP_FILL_OUTLIERS); // Compute white points in each row double[] rowCount = new double[dstBinary.rows()]; int rowTot; for (int i = 0; i < dstBinary.rows(); i++) { // iterate over rows rowTot = 0; for (int j = 0; j < dstBinary.cols(); j++) { // iterate over cols if (dstBinary.get(i, j)[0] > 128) { rowTot++; } } rowCount[i] = rowTot; } // find width by finding rising and dropping edges // rising edge = largest positive difference // falling edge = largest negative difference int risePos = 0; int fallPos = 0; double riseVal = 0; double fallVal = 0; for (int i = 0; i < dstBinary.rows() - 1; i++) { if (rowCount[i + 1] - rowCount[i] > riseVal) { riseVal = rowCount[i + 1] - rowCount[i]; risePos = i + 1; } if (rowCount[i + 1] - rowCount[i] < fallVal) { fallVal = rowCount[i + 1] - rowCount[i]; fallPos = i; } } // cut out binary strip Point stripTopLeft = new Point(0, risePos); Point stripBottomRight = new Point(dstBinary.cols(), fallPos); org.opencv.core.Rect stripAreaRect = new org.opencv.core.Rect(stripTopLeft, stripBottomRight); Mat binaryStrip = dstBinary.submat(stripAreaRect); // also cut out colored strip Mat colorStrip = dstStrip.submat(stripAreaRect); // now right end of strip // method: first rising edge double[] colCount = new double[binaryStrip.cols()]; int colTotal; for (int i = 0; i < binaryStrip.cols(); i++) { // iterate over cols colTotal = 0; for (int j = 0; j < binaryStrip.rows(); j++) { // iterate over rows if (binaryStrip.get(j, i)[0] > 128) { colTotal++; } } //Log.d("Caddisfly", String.valueOf(colTotal)); colCount[i] = colTotal; } stripAreaRect = getStripRectangle(binaryStrip, colCount, brand.getStripLength(), ratioW); Mat resultStrip = colorStrip.submat(stripAreaRect).clone(); // release Mat objects stripArea.release(); sArea.release(); binary.release(); dstBinary.release(); dstStrip.release(); binaryStrip.release(); colorStrip.release(); return resultStrip; }
From source file:org.akvo.caddisfly.sensor.colorimetry.strip.util.OpenCVUtil.java
License:Open Source License
private static Rect getStripRectangle(Mat binaryStrip, double[] colCount, double stripLength, double ratioW) { // threshold is that half of the rows in a column should be white int threshold = binaryStrip.rows() / 2; boolean found = false; // moving from the left, determine the first point that crosses the threshold double posLeft = 0; while (!found && posLeft < binaryStrip.cols()) { if (colCount[(int) posLeft] > threshold) { found = true;/*ww w . j a va2 s .c o m*/ } else { posLeft++; } } //use known length of strip to determine right side double posRight = posLeft + (stripLength * ratioW); found = false; // moving from the right, determine the first point that crosses the threshold int posRightTemp = binaryStrip.cols() - 1; while (!found && posRightTemp > 0) { if (colCount[posRightTemp] > threshold) { found = true; } else { posRightTemp--; } } // if there is a big difference in the right position determined by the two above methods // then ignore the first method above and determine the left position by second method only if (Math.abs(posRightTemp - posRight) > 5) { // use known length of strip to determine left side posLeft = posRightTemp - (stripLength * ratioW); posRight = posRightTemp; } // cut out final strip Point stripTopLeft = new Point(posLeft, 0); Point stripBottomRight = new Point(posRight, binaryStrip.rows()); return new Rect(stripTopLeft, stripBottomRight); }
From source file:org.akvo.caddisfly.sensor.colorimetry.strip.util.ResultUtil.java
License:Open Source License
@NonNull public static Mat concatenate(@NonNull Mat m1, @NonNull Mat m2) { int width = Math.max(m1.cols(), m2.cols()); int height = m1.rows() + m2.rows(); Mat result = new Mat(height, width, CvType.CV_8UC3, new Scalar(MAX_RGB_INT_VALUE, MAX_RGB_INT_VALUE, MAX_RGB_INT_VALUE)); // rect works with x, y, width, height Rect roi1 = new Rect(0, 0, m1.cols(), m1.rows()); Mat roiMat1 = result.submat(roi1);/*from w ww. j av a 2 s . com*/ m1.copyTo(roiMat1); Rect roi2 = new Rect(0, m1.rows(), m2.cols(), m2.rows()); Mat roiMat2 = result.submat(roi2); m2.copyTo(roiMat2); return result; }
From source file:org.akvo.caddisfly.sensor.colorimetry.strip.util.ResultUtil.java
License:Open Source License
@NonNull public static Mat concatenateHorizontal(@NonNull Mat m1, @NonNull Mat m2) { int width = m1.cols() + m2.cols() + HORIZONTAL_MARGIN; int height = Math.max(m1.rows(), m2.rows()); Mat result = new Mat(height, width, CvType.CV_8UC3, new Scalar(MAX_RGB_INT_VALUE, MAX_RGB_INT_VALUE, MAX_RGB_INT_VALUE)); // rect works with x, y, width, height Rect roi1 = new Rect(0, 0, m1.cols(), m1.rows()); Mat roiMat1 = result.submat(roi1);/* w w w . j a va 2 s. com*/ m1.copyTo(roiMat1); Rect roi2 = new Rect(m1.cols() + HORIZONTAL_MARGIN, 0, m2.cols(), m2.rows()); Mat roiMat2 = result.submat(roi2); m2.copyTo(roiMat2); return result; }
From source file:org.firstinspires.ftc.rmrobotics.histoy.opmodes.feRMilab.experimental.AutoExperimental.java
private void DisplayImage(Mat img) { // Scale down x2 Core.flip(img, img, -1);/*from w w w . java 2s . c o m*/ mImageMap = Bitmap.createBitmap(img.cols(), img.rows(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(img, mImageMap); ((FtcRobotControllerActivity) hardwareMap.appContext).runOnUiThread(new Runnable() { @Override public void run() { mImageView.setImageBitmap(mImageMap); } }); }
From source file:org.firstinspires.ftc.teamcode.AutonomousVuforia.java
public int getBeaconConfig(Image img, VuforiaTrackable beacon, CameraCalibration camCal) { OpenGLMatrix pose = ((VuforiaTrackableDefaultListener) beacon.getListener()).getRawPose(); telemetry.addData("Stuff", pose != null); telemetry.addData("Stuff", img != null); try {/*w w w . j a v a 2 s . c o m*/ telemetry.addData("Stuff", img.getPixels() != null); } catch (Exception e) { telemetry.addData("Stuff", e); } telemetry.update(); if (pose != null && img != null && img.getPixels() != null) { Matrix34F rawPose = new Matrix34F(); float[] poseData = Arrays.copyOfRange(pose.transposed().getData(), 0, 12); rawPose.setData(poseData); float[][] corners = new float[4][2]; corners[0] = Tool.projectPoint(camCal, rawPose, new Vec3F(-127, 276, 0)).getData(); corners[1] = Tool.projectPoint(camCal, rawPose, new Vec3F(127, 276, 0)).getData(); corners[2] = Tool.projectPoint(camCal, rawPose, new Vec3F(127, 92, 0)).getData(); corners[3] = Tool.projectPoint(camCal, rawPose, new Vec3F(-127, 92, 0)).getData(); Bitmap bm = Bitmap.createBitmap(img.getWidth(), img.getHeight(), Bitmap.Config.RGB_565); bm.copyPixelsFromBuffer(img.getPixels()); Mat crop = new Mat(bm.getHeight(), bm.getWidth(), CvType.CV_8UC3); Utils.bitmapToMat(bm, crop); float x = Math.min(Math.min(corners[1][0], corners[3][0]), Math.min(corners[0][0], corners[2][0])); float y = Math.min(Math.min(corners[1][1], corners[3][1]), Math.min(corners[0][1], corners[2][1])); float width = Math.max(Math.abs(corners[0][0] - corners[2][0]), Math.abs(corners[1][0] - corners[3][0])); float height = Math.max(Math.abs(corners[0][1] - corners[2][1]), Math.abs(corners[1][1] - corners[3][1])); x = Math.max(x, 0); y = Math.max(y, 0); width = (x + width > crop.cols()) ? crop.cols() - x : width; height = (y + height > crop.rows()) ? crop.rows() - y : height; Mat cropped = new Mat(crop, new Rect((int) x, (int) y, (int) width, (int) height)); Imgproc.cvtColor(cropped, cropped, Imgproc.COLOR_RGB2HSV_FULL); Mat mask = new Mat(); Core.inRange(cropped, blueLow, blueHigh, mask); Moments mmnts = Imgproc.moments(mask, true); if (mmnts.get_m00() > mask.total() * 0.8) { return BEACON_ALL_BLUE; } else if (mmnts.get_m00() < mask.total() * 0.8) { return BEACON_NO_BLUE; } if ((mmnts.get_m01() / mmnts.get_m00()) < cropped.rows() / 2) { return BEACON_RED_BLUE; } else { return BEACON_BLUERED; } // else } return BEACON_NOT_VISIBLE; }
From source file:org.firstinspires.ftc.teamcode.libraries.VuforiaBallLib.java
protected static Scalar drawSquare(Mat src, int[] ballPoint, int ballDist) { //find average left and right ball square //find the average color for all the pixels in that square if (ballPoint[0] >= 0 && ballPoint[1] >= 0 && ballPoint[0] + ballDist < src.cols() && ballPoint[1] + ballDist < src.rows()) { double total[] = new double[3]; for (int x = 0; x < ballDist; x++) for (int y = 0; y < ballDist; y++) { double[] pixel = src.get(y + ballPoint[1], x + ballPoint[0]); total[0] += pixel[0];/*from w w w . j a v a 2 s . co m*/ total[1] += pixel[1]; total[2] += pixel[2]; } //make average color Scalar color = new Scalar(total[0] / (ballDist * ballDist), total[1] / (ballDist * ballDist), total[2] / (ballDist * ballDist)); Imgproc.rectangle(src, new Point(ballPoint[0], ballPoint[1]), new Point(ballPoint[0] + ballDist, ballPoint[1] + ballDist), color, -1); return color; } else return null; }
From source file:org.firstinspires.ftc.teamcode.opmodes.demo.VumarkHiJackVideo.java
private static Scalar drawSquare(Mat src, int[] ballPoint, int ballDist) { //find average left and right ball square //find the average color for all the pixels in that square if (ballPoint[0] >= 0 && ballPoint[1] >= 0 && ballPoint[0] + ballDist < src.cols() && ballPoint[1] + ballDist < src.rows()) { double total[] = new double[3]; for (int x = 0; x < ballDist; x++) for (int y = 0; y < ballDist; y++) { double[] pixel = src.get(y + ballPoint[1], x + ballPoint[0]); total[0] += pixel[0];//from ww w .j av a2 s.c o m total[1] += pixel[1]; total[2] += pixel[2]; } //make average color Scalar color = new Scalar(total[0] / (ballDist * ballDist), total[1] / (ballDist * ballDist), total[2] / (ballDist * ballDist)); Imgproc.rectangle(src, new Point(ballPoint[0], ballPoint[1]), new Point(ballPoint[0] + ballDist, ballPoint[1] + ballDist), color, -1); return color; } else return null; }
From source file:org.firstinspires.ftc.teamcode.opmodes.demo.VumarkOpenCV.java
@Override public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame frame) { Mat currentFrame = frame.rgba(); //if we haven't figured out how to scale the size, do that if (xOffset == 0 || yOffset == 0) { //find the offset to the sqaure of pixels vuforia looks at xOffset = (int) ((currentFrame.cols() - size[0]) / 2.0); yOffset = (int) ((currentFrame.rows() - size[1]) / 2.0); //add the offset to all points calculated for (Point point : imagePoints) { point.x += xOffset;/* w w w. j a va 2 s . c om*/ point.y += yOffset; } leftBall[0] += xOffset; leftBall[1] += yOffset; rightBall[0] += xOffset; rightBall[1] += yOffset; } //operation: subsquare //take a square mat we are 100% sure will have a ball in it //sum it up and find the average color drawSquare(currentFrame, leftBall, leftDist); drawSquare(currentFrame, rightBall, rightDist); Scalar color = new Scalar(0, 255, 0); for (int i = 0; i < 2; i++) for (int o = 0; o < 4; o++) Imgproc.line(currentFrame, imagePoints[o == 0 ? 3 + i * 4 : i * 4 + o - 1], imagePoints[i * 4 + o], color); //connect the rectangles for (int i = 0; i < 4; i++) Imgproc.line(currentFrame, imagePoints[i], imagePoints[i + 4], color); //flip it for display Core.flip(currentFrame, currentFrame, -1); return currentFrame; }
From source file:org.firstinspires.ftc.teamcode.opmodes.demo.VumarkOpenCV.java
private static void drawSquare(Mat src, int[] ballPoint, int ballDist) { //find average left and right ball square //find the average color for all the pixels in that square if (ballPoint[0] >= 0 && ballPoint[1] >= 0 && ballPoint[0] + ballDist < src.cols() && ballPoint[1] + ballDist < src.rows()) { double total[] = new double[3]; for (int x = 0; x < ballDist; x++) for (int y = 0; y < ballDist; y++) { double[] pixel = src.get(y + ballPoint[1], x + ballPoint[0]); total[0] += pixel[0];/*from w w w.ja va 2 s. co m*/ total[1] += pixel[1]; total[2] += pixel[2]; } //make average color Scalar color = new Scalar(total[0] / (ballDist * ballDist), total[1] / (ballDist * ballDist), total[2] / (ballDist * ballDist)); Imgproc.rectangle(src, new Point(ballPoint[0], ballPoint[1]), new Point(ballPoint[0] + ballDist, ballPoint[1] + ballDist), color, -1); } }