> For the complete documentation index, see [llms.txt](https://docs.cb21829.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cb21829.org/ftc-java-advanced-guide/computer-vision-apriltag-and-opencv.md).

# Computer vision: AprilTag and OpenCV

Vision lets the robot see the field, detect game elements, and localize from AprilTags.

### AprilTag with VisionPortal

The SDK includes **VisionPortal** and **AprilTagProcessor**.

```java
import org.firstinspires.ftc.vision.VisionPortal;
import org.firstinspires.ftc.vision.apriltag.AprilTagProcessor;
import org.firstinspires.ftc.vision.apriltag.AprilTagDetection;

AprilTagProcessor aprilTag;
VisionPortal visionPortal;

aprilTag = AprilTagProcessor.easyCreateWithDefaults();

visionPortal = new VisionPortal.Builder()
        .setCamera(hardwareMap.get(WebcamName.class, "Webcam 1"))
        .addProcessor(aprilTag)
        .build();

for (AprilTagDetection detection : aprilTag.getDetections()) {
    if (detection.metadata != null) {
        telemetry.addData("Tag ID", detection.id);
        telemetry.addData("Range (in)", detection.ftcPose.range);
        telemetry.addData("Bearing (deg)", detection.ftcPose.bearing);
        telemetry.addData("Yaw (deg)", detection.ftcPose.yaw);
    }
}
```

AprilTag alignment is one of the highest-value autonomous techniques available.

### Custom pipelines with EasyOpenCV

Use EasyOpenCV when you need to detect colors or shapes that are not AprilTags.

```java
import org.openftc.easyopencv.OpenCvPipeline;
import org.opencv.core.*;
import org.opencv.imgproc.Imgproc;

public class ColorDetectionPipeline extends OpenCvPipeline {
    private volatile String detectedSide = "UNKNOWN";

    @Override
    public Mat processFrame(Mat input) {
        Mat hsv = new Mat();
        Imgproc.cvtColor(input, hsv, Imgproc.COLOR_RGB2HSV);

        Mat mask = new Mat();
        Core.inRange(hsv, new Scalar(0, 100, 100), new Scalar(15, 255, 255), mask);

        Rect leftROI = new Rect(0, 0, input.width() / 3, input.height());
        double leftAmount = Core.sumElems(mask.submat(leftROI)).val[0];

        detectedSide = (leftAmount > 1_000_000) ? "LEFT" : "CENTER_OR_RIGHT";

        Imgproc.rectangle(input, leftROI, new Scalar(0, 255, 0), 4);

        hsv.release();
        mask.release();
        return input;
    }

    public String getDetectedSide() { return detectedSide; }
}
```

### Practical tips

* use HSV instead of RGB for color thresholding
* always release `Mat` objects
* tune on the real field under real lighting
* read the latest result each loop instead of blocking


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.cb21829.org/ftc-java-advanced-guide/computer-vision-apriltag-and-opencv.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
