-
Notifications
You must be signed in to change notification settings - Fork 268
Add Camera Focus Mode #2180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Gold856
merged 36 commits into
PhotonVision:main
from
ElectricTurtle32:camera-focus-tool
Nov 17, 2025
Merged
Add Camera Focus Mode #2180
Changes from 2 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
bce2c40
added focus camera pipe
ElectricTurtle32 ee727d8
Made it into a real pipline
ElectricTurtle32 c2431b7
Added ui
ElectricTurtle32 f175ab3
Focus mode now outputs to UI, general cleanup.
ElectricTurtle32 ef4fb0b
Lots of formatting
ElectricTurtle32 95429a7
fix a few things
ElectricTurtle32 0e162af
added focus camera pipe
ElectricTurtle32 bcef45f
Made it into a real pipline
ElectricTurtle32 64f6731
Added ui
ElectricTurtle32 ac132fe
Focus mode now outputs to UI, general cleanup.
ElectricTurtle32 06765cc
Lots of formatting
ElectricTurtle32 315fc49
fix a few things
ElectricTurtle32 132be8e
Update docs/source/docs/quick-start/camera-focusing.md
ElectricTurtle32 fec2e9e
Fixed a lot of typos and formating. Reworked ui for focus mode.
ElectricTurtle32 aeffa0a
Merge branch 'camera-focus-tool' of https://github.com/ElectricTurtle…
ElectricTurtle32 b0fb7d5
Git merge things
ElectricTurtle32 56ea7c3
Hopefully fix build errors
ElectricTurtle32 1a0d0f2
Moved focus indicatorn to camera view
ElectricTurtle32 f492a77
Fix more merge things
ElectricTurtle32 ddb57db
Fix more merge things
ElectricTurtle32 ece7b42
Merge branch 'camera-focus-tool' of https://github.com/ElectricTurtle…
ElectricTurtle32 de5bf45
Fix typo
ElectricTurtle32 736347e
Merge branch 'main' into camera-focus-tool
ElectricTurtle32 99f3f41
Small requested changes
ElectricTurtle32 ac32a43
Fixxed padding in camera settings
ElectricTurtle32 c0d81ef
Update blank lines, fix docs.
ElectricTurtle32 e363744
Added warning to recalibrate camera after focusing & added warning to…
ElectricTurtle32 a6b36bd
Re-lint everthing
ElectricTurtle32 932dc9c
Added screenshot
ElectricTurtle32 cc1a523
Compress image
Gold856 5034c94
Changed wording in docs
ElectricTurtle32 6b33f3f
nitpicks
ElectricTurtle32 bdd5001
Merge branch 'camera-focus-tool' of https://github.com/ElectricTurtle…
ElectricTurtle32 001fa23
Typo
ElectricTurtle32 8937685
Update docs/source/docs/quick-start/camera-focusing.md
ElectricTurtle32 6da2c5c
Update docs/source/docs/quick-start/camera-focusing.md
ElectricTurtle32 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
photon-core/src/main/java/org/photonvision/vision/pipe/impl/FocusPipe.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Copyright (C) Photon Vision. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package org.photonvision.vision.pipe.impl; | ||
|
|
||
| import org.opencv.core.Mat; | ||
| import org.opencv.core.CvType; | ||
| import org.opencv.core.Core; | ||
| import org.opencv.imgproc.Imgproc; | ||
| import org.photonvision.vision.pipe.CVPipe; | ||
|
|
||
| public class FocusPipe extends CVPipe<Mat, Mat, FocusPipe.FocusParams> { | ||
| @Override | ||
| protected Mat process(Mat in) { | ||
| var outputMat = new Mat(); | ||
| // We can save a copy here by sending the output of cvtcolor to outputMat directly | ||
| // rather than copying. Free performance! | ||
| //Imgproc.cvtColor(in, outputMat, Imgproc.COLOR_BGR2GRAY, 3); | ||
| // Compute the Laplacian in double precision (like cv2.CV_64F) | ||
| Imgproc.Laplacian(in, outputMat, CvType.CV_64F, 3); | ||
|
|
||
| // Compute standard deviation (and square it for variance) on the double Laplacian | ||
| var mean = new org.opencv.core.MatOfDouble(); | ||
| var stddev = new org.opencv.core.MatOfDouble(); | ||
| Core.meanStdDev(outputMat, mean, stddev); | ||
| var sd = stddev.get(0, 0)[0]; | ||
| var variance = sd * sd; | ||
|
|
||
|
|
||
|
|
||
| // Draw the variance on the image (displayMat is 8-bit) | ||
| Imgproc.putText( | ||
| outputMat, | ||
| String.format("%.2f", variance), | ||
| new org.opencv.core.Point(10, 30), | ||
| Imgproc.FONT_HERSHEY_SIMPLEX, | ||
| 0.8, | ||
| new org.opencv.core.Scalar(255, 255, 255), | ||
| 2 | ||
| ); | ||
|
|
||
| return outputMat; | ||
| } | ||
|
|
||
| public static class FocusParams {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
photon-core/src/main/java/org/photonvision/vision/pipeline/FocusCameraPipeline.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * Copyright (C) Photon Vision. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package org.photonvision.vision.pipeline; | ||
|
|
||
| import edu.wpi.first.math.Pair; | ||
| import java.util.List; | ||
| import org.photonvision.common.util.math.MathUtils; | ||
| import org.photonvision.vision.frame.Frame; | ||
| import org.photonvision.vision.frame.FrameThresholdType; | ||
| import org.photonvision.vision.pipe.impl.CalculateFPSPipe; | ||
| import org.photonvision.vision.pipe.impl.Draw2dCrosshairPipe; | ||
| import org.photonvision.vision.pipe.impl.ResizeImagePipe; | ||
| import org.photonvision.vision.pipeline.result.DriverModePipelineResult; | ||
|
|
||
| public class FocusCameraPipeline | ||
| extends CVPipeline<DriverModePipelineResult, DriverModePipelineSettings> { | ||
| private final Draw2dCrosshairPipe draw2dCrosshairPipe = new Draw2dCrosshairPipe(); | ||
| private final FocusPipe focusPipe = new FocusPipe(); | ||
| private final CalculateFPSPipe calculateFPSPipe = new CalculateFPSPipe(); | ||
| private final ResizeImagePipe resizeImagePipe = new ResizeImagePipe(); | ||
|
|
||
| private static final FrameThresholdType PROCESSING_TYPE = FrameThresholdType.NONE; | ||
|
|
||
| public FocusCameraPipeline() { | ||
| super(PROCESSING_TYPE); | ||
| settings = new FocusCameraPipelineSettings(); | ||
| } | ||
|
|
||
| public FocusCameraPipeline(DriverModePipelineSettings settings) { | ||
| super(PROCESSING_TYPE); | ||
| this.settings = settings; | ||
| } | ||
| public DoubleCouple offsetPoint = new DoubleCouple(); | ||
| @Override | ||
| protected void setPipeParamsImpl() { | ||
| draw2dCrosshairPipe.setParams( | ||
| new Draw2dCrosshairPipe.Draw2dCrosshairParams( | ||
| frameStaticProperties, | ||
| settings.streamingFrameDivisor, | ||
| settings.inputImageRotationMode)); | ||
|
|
||
| resizeImagePipe.setParams( | ||
| new ResizeImagePipe.ResizeImageParams(settings.streamingFrameDivisor)); | ||
| } | ||
|
|
||
| @Override | ||
| public FocusCameraPipelineResult process(Frame frame, FocusCameraPipelineSettings settings) { | ||
| long totalNanos = 0; | ||
|
|
||
| // apply pipes | ||
| var inputMat = frame.colorImage.getMat(); | ||
|
|
||
| boolean emptyIn = inputMat.empty(); | ||
|
|
||
|
|
||
| if (!emptyIn) { | ||
| totalNanos += resizeImagePipe.run(inputMat).nanosElapsed; | ||
| var focusResult = focusPipe.run(inputMat); | ||
| inputMat = focusResult.output; | ||
|
|
||
| if (settings.crosshair) { | ||
| var draw2dCrosshairResult = draw2dCrosshairPipe.run(Pair.of(inputMat, List.of())); | ||
|
|
||
| // calculate elapsed nanoseconds | ||
| totalNanos += draw2dCrosshairResult.nanosElapsed; | ||
| } | ||
| } | ||
|
|
||
| var fpsResult = calculateFPSPipe.run(null); | ||
| var fps = fpsResult.output; | ||
|
|
||
| // Flip-flop input and output in the Frame | ||
| return new DriverModePipelineResult( | ||
| frame.sequenceID, | ||
| MathUtils.nanosToMillis(totalNanos), | ||
| fps, | ||
| new Frame( | ||
| frame.sequenceID, | ||
| frame.processedImage, | ||
| frame.colorImage, | ||
| frame.type, | ||
| frame.frameStaticProperties)); | ||
| } | ||
|
|
||
| @Override | ||
| public void release() { | ||
| // we never actually need to give resources up since pipelinemanager only makes | ||
| // one of us | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
photon-core/src/main/java/org/photonvision/vision/pipeline/FocusCameraPipelineSettings.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Copyright (C) Photon Vision. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package org.photonvision.vision.pipeline; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonTypeName; | ||
| import org.photonvision.common.util.numbers.DoubleCouple; | ||
| import org.photonvision.vision.processes.PipelineManager; | ||
|
|
||
| @JsonTypeName("FocusCameraPipelineSettings") | ||
| public class FocusCameraPipelineSettings extends CVPipelineSettings { | ||
|
|
||
|
|
||
| public FocusCameraPipelineSettings() { | ||
| super(); | ||
| pipelineNickname = "Focus Camera"; | ||
| pipelineIndex = PipelineManager.FOCUS_CAMERA_INDEX; | ||
| pipelineType = PipelineType.FocusCamera; | ||
| inputShouldShow = true; | ||
| cameraAutoExposure = true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.