List of usage examples for org.opencv.imgproc Imgproc line
public static void line(Mat img, Point pt1, Point pt2, Scalar color)
From source file:org.firstinspires.ftc.teamcode.libraries.VuforiaBallLib.java
protected BallColor getBallColor() { //get frame from vuforia try {//from w w w. jav a 2 s .c o m Mat out = getFrame(); //get vuforia's real position matrix Matrix34F goodCodeWritten = ((VuforiaDefaultListenerShim) relicTemplate.getListener()).getRealPose(); if (goodCodeWritten == null) return BallColor.Undefined; this.tempMark = RelicRecoveryVuMark.from(relicTemplate); final float[][] ballPoints = new float[4][2]; //the actual color determination for (int i = 8; i < point.length; i++) ballPoints[i - 8] = Tool.projectPoint(camCal, goodCodeWritten, point[i]).getData(); Point imagePoints[] = new Point[ballPoints.length]; //convert points to opencv language for (int i = 0; i < ballPoints.length; i++) imagePoints[i] = new Point((int) ballPoints[i][0], (int) ballPoints[i][1]); //calculate points from projection //find the midpoint between the two points int leftXPoint = (int) ((imagePoints[0].x + imagePoints[1].x) / 2.0); int leftYPoint = (int) ((imagePoints[0].y + imagePoints[1].y) / 2.0); //find the y distande between the two int leftDist = (int) (Math.abs(imagePoints[0].y - imagePoints[1].y) / 2.0); int[] leftBall = new int[] { leftXPoint - (leftDist / 2), leftYPoint - (leftDist / 2) }; //find the midpoint between the two points int rightXPoint = (int) ((imagePoints[2].x + imagePoints[3].x) / 2.0); int rightYPoint = (int) ((imagePoints[2].y + imagePoints[3].y) / 2.0); //find the y distande between the two int rightDist = (int) (Math.abs(imagePoints[2].y - imagePoints[3].y) / 2.0); int[] rightBall = new int[] { rightXPoint - (rightDist / 2), rightYPoint - (rightDist / 2) }; //operation: subsquare //take a square mat we are 100% sure will have a ball in it //sum it up and find the average color Scalar leftColor = drawSquare(out, leftBall, leftDist); Scalar rightColor = drawSquare(out, rightBall, rightDist); if (displayData) { float[][] squarePoints = new float[8][2]; for (int i = 0; i < 8; i++) squarePoints[i] = Tool.projectPoint(camCal, goodCodeWritten, point[i]).getData(); Point[] squarePointRay = new Point[squarePoints.length]; for (int i = 0; i < squarePoints.length; i++) squarePointRay[i] = new Point((int) squarePoints[i][0], (int) squarePoints[i][1]); Scalar green = new Scalar(0, 255, 0); for (int i = 0; i < 2; i++) for (int o = 0; o < 4; o++) Imgproc.line(out, squarePointRay[o == 0 ? 3 + i * 4 : i * 4 + o - 1], squarePointRay[i * 4 + o], green); //connect the rectangles for (int i = 0; i < 4; i++) Imgproc.line(out, squarePointRay[i], squarePointRay[i + 4], green); for (int i = 0; i < imagePoints.length; i++) Imgproc.drawMarker(out, imagePoints[i], green); //flip it for display Core.flip(out, out, -1); drawFrame(out); } if (leftColor != null && rightColor != null) { if (leftColor.val[0] < leftColor.val[1] && rightColor.val[0] > rightColor.val[1]) return BallColor.LeftBlue; else if (leftColor.val[0] > leftColor.val[1] && rightColor.val[0] < rightColor.val[1]) return BallColor.LeftRed; else return BallColor.Indeterminate; } else return BallColor.Undefined; } catch (Exception e) { Log.e("OPENCV", e.getMessage()); return BallColor.Undefined; } }
From source file:org.firstinspires.ftc.teamcode.opmodes.demo.VumarkHiJackVideo.java
@Override public void init_loop() { RelicRecoveryVuMark vuMark = RelicRecoveryVuMark.from(relicTemplate); if (vuMark != RelicRecoveryVuMark.UNKNOWN) { /* Found an instance of the template. In the actual game, you will probably * loop until this condition occurs, then move on to act accordingly depending * on which VuMark was visible. */ telemetry.addData("VuMark", "%s visible", vuMark); /* For fun, we also exhibit the navigational pose. In the Relic Recovery game, * it is perhaps unlikely that you will actually need to act on this pose information, but * we illustrate it nevertheless, for completeness. */ //qualcomm is stupid and didn't let me see the real vuforia matrix, so I shimmed it //heres where is gets good VuforiaDefaultListenerShim writeGoodCode = (VuforiaDefaultListenerShim) relicTemplate.getListener(); OpenGLMatrix pose = writeGoodCode.getRawPose(); telemetry.addData("Pose", format(pose)); /* We further illustrate how to decompose the pose into useful rotational and * translational components */ if (pose != null) { //alternate projection! //get vuforia's real position matrix Matrix34F goodCodeWritten = writeGoodCode.getRealPose(); //reset imagePoints final float[][] vufPoints = new float[point.length][2]; //use vuforias projectPoints method to project all those box points for (int i = 0; i < point.length; i++) { //project vufPoints[i] = Tool.projectPoint(camCal, goodCodeWritten, point[i]).getData(); //convert to opencv language //telemetry.addData("point", "num: %d, x: %f.2, y: %f.2", i, vufPoints[i][0], vufPoints[i][1]); }/*from w ww.j a va 2 s . c o m*/ //telemetry.addData("Camera Size", "w: %f.2, h: %f.2", camCal.getSize().getData()[0], camCal.getSize().getData()[1]); //get frame from vuforia try { VuforiaLocalizer.CloseableFrame frame = ray.take(); int img = 0; for (; img < frame.getNumImages(); img++) { //telemetry.addData("Image format " + img, frame.getImage(img).getFormat()); if (frame.getImage(img).getFormat() == PIXEL_FORMAT.RGB888) break; } Image mImage = frame.getImage(img); //stick it in a Mat for "display purposes" Mat out = new Mat(mImage.getHeight(), mImage.getWidth(), CV_8UC3); java.nio.ByteBuffer color = mImage.getPixels(); byte[] ray = new byte[color.limit()]; color.rewind(); color.get(ray); out.put(0, 0, ray); frame.close(); //convert points, halfing distances b/c vuforia does that internally so we gotta fix it for (int i = 0; i < vufPoints.length; i++) imagePoints[i] = new Point((int) vufPoints[i][0], (int) vufPoints[i][1]); Scalar green = new Scalar(0, 255, 0); for (int i = 0; i < 2; i++) for (int o = 0; o < 4; o++) Imgproc.line(out, imagePoints[o == 0 ? 3 + i * 4 : i * 4 + o - 1], imagePoints[i * 4 + o], green); //connect the rectangles for (int i = 0; i < 4; i++) Imgproc.line(out, imagePoints[i], imagePoints[i + 4], green); for (int i = 8; i < imagePoints.length; i++) Imgproc.drawMarker(out, imagePoints[i], green); //calculate points from projection //find the midpoint between the two points int leftXPoint = (int) ((imagePoints[8].x + imagePoints[9].x) / 2.0); int leftYPoint = (int) ((imagePoints[8].y + imagePoints[9].y) / 2.0); //find the y distande between the two leftDist = (int) (Math.abs(imagePoints[8].y - imagePoints[9].y) / 2.0); leftBall = new int[] { leftXPoint - (leftDist / 2), leftYPoint - (leftDist / 2) }; //find the midpoint between the two points int rightXPoint = (int) ((imagePoints[10].x + imagePoints[11].x) / 2.0); int rightYPoint = (int) ((imagePoints[10].y + imagePoints[11].y) / 2.0); //find the y distande between the two rightDist = (int) (Math.abs(imagePoints[10].y - imagePoints[11].y) / 2.0); rightBall = new int[] { rightXPoint - (rightDist / 2), rightYPoint - (rightDist / 2) }; //operation: subsquare //take a square mat we are 100% sure will have a ball in it //sum it up and find the average color leftColor = drawSquare(out, leftBall, leftDist); rightColor = drawSquare(out, rightBall, rightDist); //flip it for display Core.flip(out, out, -1); matQueue.add(out); //display! mView.getHandler().post(new Runnable() { @Override public void run() { try { Mat frame = matQueue.take(); //convert to bitmap Utils.matToBitmap(frame, bm); frame.release(); mView.invalidate(); } catch (InterruptedException e) { //huh } } }); } catch (Exception e) { Log.e("OPENCV", e.getLocalizedMessage()); } } } else { telemetry.addData("VuMark", "not visible"); } }
From source file:org.firstinspires.ftc.teamcode.opmodes.demo.VumarkOpenCV.java
@Override public void init_loop() { RelicRecoveryVuMark vuMark = RelicRecoveryVuMark.from(relicTemplate); if (vuMark != RelicRecoveryVuMark.UNKNOWN) { /* Found an instance of the template. In the actual game, you will probably * loop until this condition occurs, then move on to act accordingly depending * on which VuMark was visible. */ telemetry.addData("VuMark", "%s visible", vuMark); /* For fun, we also exhibit the navigational pose. In the Relic Recovery game, * it is perhaps unlikely that you will actually need to act on this pose information, but * we illustrate it nevertheless, for completeness. */ //qualcomm is stupid and didn't let me see the real vuforia matrix, so I shimmed it //heres where is gets good VuforiaDefaultListenerShim writeGoodCode = (VuforiaDefaultListenerShim) relicTemplate.getListener(); OpenGLMatrix pose = writeGoodCode.getRawPose(); telemetry.addData("Pose", format(pose)); /* We further illustrate how to decompose the pose into useful rotational and * translational components */ if (pose != null) { //alternate projection! //get vuforia's real position matrix Matrix34F goodCodeWritten = writeGoodCode.getRealPose(); //reset imagePoints final float[][] vufPoints = new float[point.length][2]; //use vuforias projectPoints method to project all those box points for (int i = 0; i < point.length; i++) { //project vufPoints[i] = Tool.projectPoint(camCal, goodCodeWritten, point[i]).getData(); //convert to opencv language //telemetry.addData("point", "num: %d, x: %f.2, y: %f.2", i, vufPoints[i][0], vufPoints[i][1]); }/*w w w. j av a2s . c o m*/ //telemetry.addData("Camera Size", "w: %f.2, h: %f.2", camCal.getSize().getData()[0], camCal.getSize().getData()[1]); //get frame from vuforia try { VuforiaLocalizer.CloseableFrame frame = ray.take(); int img = 0; for (; img < frame.getNumImages(); img++) { //Log.i("OPENCV", "Image " + img + " " + frame.getImage(img).getWidth()); if (frame.getImage(img).getWidth() == size[0]) break; } final Mat out; if (img < frame.getNumImages()) out = getMatFromImage(frame.getImage(img)); else throw new IllegalArgumentException("No frame with matching width!"); frame.close(); //convert points, halfing distances b/c vuforia does that internally so we gotta fix it for (int i = 0; i < vufPoints.length; i++) imagePoints[i] = new Point((int) vufPoints[i][0] / 2, (int) vufPoints[i][1] / 2); Scalar color = new Scalar(0, 255, 0); for (int i = 0; i < 2; i++) for (int o = 0; o < 4; o++) Imgproc.line(out, 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(out, imagePoints[i], imagePoints[i + 4], color); for (int i = 8; i < imagePoints.length; i++) Imgproc.drawMarker(out, imagePoints[i], color); //flip it for display Core.flip(out, out, -1); matQueue.add(out); //display! mView.getHandler().post(new Runnable() { @Override public void run() { try { Mat frame = matQueue.take(); //convert to bitmap Utils.matToBitmap(frame, bm); frame.release(); mView.invalidate(); } catch (InterruptedException e) { //huh } } }); } catch (Exception e) { Log.e("OPENCV", e.getLocalizedMessage()); } } } else { telemetry.addData("VuMark", "not visible"); } }
From source file:org.firstinspires.ftc.teamcode.opmodes.demo.VumarkOpenCV.java
@Override public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame frame) { Mat currentFrame = frame.rgba();/*from www .j a va 2 s.c o m*/ //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; 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:samples.LWF.java
private static void save_meshed_images(double[][] puntos, File carpetaalmacen, File image, Mat mat, int[][] delaunay_triangles) { Mat mat_copy = mat.clone();/*from w ww . j av a2 s . com*/ int radii = 1000; for (double[] punto : puntos) { Imgproc.ellipse(mat_copy, new Point(punto), new Size(radii, radii), 0, 0, 0, new Scalar(0, 255, 0)); // Imgproc.line(mat_copy, null, null, null); } for (int[] tri : faceTemplateTriangles) { Imgproc.line(mat_copy, new Point(puntos[tri[0] - 1]), new Point(puntos[tri[1] - 1]), new Scalar(0, 255, 0)); Imgproc.line(mat_copy, new Point(puntos[tri[1] - 1]), new Point(puntos[tri[2] - 1]), new Scalar(0, 255, 0)); Imgproc.line(mat_copy, new Point(puntos[tri[2] - 1]), new Point(puntos[tri[0] - 1]), new Scalar(0, 255, 0)); } Imgcodecs.imwrite(carpetaalmacen.getAbsolutePath() + "\\" + image.getName(), mat_copy); }
From source file:samples.LWF.java
private static void save_mesh_images(double[][] puntos, File carpetaalmacen, File image, Mat mat, int[][] delaunay_triangles) { Mat lienzo = new Mat(300, 300, CV_8UC3, new Scalar(0, 0, 0)); Mat lienzo2 = new Mat(300, 300, CV_8UC3, new Scalar(0, 0, 0)); double escala = 128; for (int[] tri : faceTemplateTriangles) { Imgproc.line(lienzo, new Point(escala * Shape3D[tri[0] - 1][0], escala * Shape3D[tri[0] - 1][1]), new Point(escala * Shape3D[tri[1] - 1][0], escala * Shape3D[tri[1] - 1][1]), new Scalar(0, 255, 0)); Imgproc.line(lienzo, new Point(escala * Shape3D[tri[1] - 1][0], escala * Shape3D[tri[1] - 1][1]), new Point(escala * Shape3D[tri[2] - 1][0], escala * Shape3D[tri[2] - 1][1]), new Scalar(0, 255, 0)); Imgproc.line(lienzo, new Point(escala * Shape3D[tri[2] - 1][0], escala * Shape3D[tri[2] - 1][1]), new Point(escala * Shape3D[tri[0] - 1][0], escala * Shape3D[tri[0] - 1][1]), new Scalar(0, 255, 0)); Imgproc.line(lienzo2, new Point(escala * Shape3D[tri[0] - 1][2], escala * Shape3D[tri[0] - 1][1]), new Point(escala * Shape3D[tri[1] - 1][2], escala * Shape3D[tri[1] - 1][1]), new Scalar(0, 255, 0)); Imgproc.line(lienzo2, new Point(escala * Shape3D[tri[1] - 1][2], escala * Shape3D[tri[1] - 1][1]), new Point(escala * Shape3D[tri[2] - 1][2], escala * Shape3D[tri[2] - 1][1]), new Scalar(0, 255, 0)); Imgproc.line(lienzo2, new Point(escala * Shape3D[tri[2] - 1][2], escala * Shape3D[tri[2] - 1][1]), new Point(escala * Shape3D[tri[0] - 1][2], escala * Shape3D[tri[0] - 1][1]), new Scalar(0, 255, 0)); }/*from ww w. ja va 2 s. co m*/ Imgcodecs.imwrite(carpetaalmacen.getAbsolutePath() + "\\frontal_" + image.getName(), lienzo); Imgcodecs.imwrite(carpetaalmacen.getAbsolutePath() + "\\lateral_" + image.getName(), lienzo2); }