-
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 all 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Camera Focusing | ||
|
|
||
| ## Prepare Camera | ||
| :::{warning} | ||
| Refocusing your camera **will** make your calibration inaccurate, make sure to recalibrate after focusing. | ||
| ::: | ||
| To ensure that your camera is focused properly, mount it to a secure surface and ensure it does not move drastically. Point your camera at a detailed surface like a calibration board, and make sure that it not too close to the camera. | ||
|
|
||
| ## Using Focus Mode | ||
| :::{important} | ||
| When you enable Focus Mode, it will assign a *Score* to the current focus, this score depends on your environment and the lighting. This score cannot be compared to a focus score collected from other environments. | ||
| ::: | ||
| - In the Cameras tab, turn on Focus Mode. | ||
| - Rotate the lens on your camera to try and get the focus score as high as possible. | ||
| - Once you cannot get a higher score, this indicates that your camera is fully focused and can be set in place using glue if desired. | ||
|
|
||
| ```{image} images/focusModeExample.png | ||
| :scale: 50% | ||
| ``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
|---|---|---|
|
|
@@ -9,5 +9,6 @@ wiring | |
| networking | ||
| camera-matching | ||
| camera-calibration | ||
| camera-focusing | ||
| quick-configure | ||
| ``` | ||
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
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
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
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
56 changes: 56 additions & 0 deletions
56
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,56 @@ | ||
| /* | ||
| * 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.Core; | ||
| import org.opencv.core.CvType; | ||
| import org.opencv.core.Mat; | ||
| import org.opencv.core.MatOfDouble; | ||
| import org.opencv.imgproc.Imgproc; | ||
| import org.photonvision.vision.pipe.CVPipe; | ||
|
|
||
| public class FocusPipe extends CVPipe<Mat, FocusPipe.FocusResult, FocusPipe.FocusParams> { | ||
| private double maxVariance = 0.0; | ||
|
|
||
| @Override | ||
| protected FocusResult process(Mat in) { | ||
| var outputMat = new Mat(); | ||
|
|
||
| Imgproc.Laplacian(in, outputMat, CvType.CV_64F, 3); | ||
|
|
||
| var mean = new MatOfDouble(); | ||
| var stddev = new MatOfDouble(); | ||
| Core.meanStdDev(outputMat, mean, stddev); | ||
| var sd = stddev.get(0, 0)[0]; | ||
| var variance = sd * sd; | ||
|
|
||
| return new FocusResult(outputMat, variance); | ||
| } | ||
|
|
||
| public static class FocusResult { | ||
| public final Mat frame; | ||
| public final double variance; | ||
|
|
||
| public FocusResult(Mat frame, double variance) { | ||
| this.frame = frame; | ||
| this.variance = variance; | ||
| } | ||
| } | ||
|
|
||
| public static class FocusParams {} | ||
| } |
94 changes: 94 additions & 0 deletions
94
photon-core/src/main/java/org/photonvision/vision/pipeline/FocusPipeline.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,94 @@ | ||
| /* | ||
| * 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 org.opencv.core.Mat; | ||
| import org.photonvision.common.util.math.MathUtils; | ||
| import org.photonvision.vision.frame.Frame; | ||
| import org.photonvision.vision.frame.FrameThresholdType; | ||
| import org.photonvision.vision.opencv.CVMat; | ||
| import org.photonvision.vision.pipe.impl.CalculateFPSPipe; | ||
| import org.photonvision.vision.pipe.impl.FocusPipe; | ||
| import org.photonvision.vision.pipe.impl.ResizeImagePipe; | ||
| import org.photonvision.vision.pipeline.result.FocusPipelineResult; | ||
|
|
||
| public class FocusPipeline extends CVPipeline<FocusPipelineResult, FocusPipelineSettings> { | ||
| 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 FocusPipeline() { | ||
| super(PROCESSING_TYPE); | ||
| settings = new FocusPipelineSettings(); | ||
| } | ||
|
|
||
| public FocusPipeline(FocusPipelineSettings settings) { | ||
| super(PROCESSING_TYPE); | ||
| this.settings = settings; | ||
| } | ||
|
|
||
| @Override | ||
| protected void setPipeParamsImpl() { | ||
| resizeImagePipe.setParams( | ||
| new ResizeImagePipe.ResizeImageParams(settings.streamingFrameDivisor)); | ||
| } | ||
|
|
||
| @Override | ||
| public FocusPipelineResult process(Frame frame, FocusPipelineSettings settings) { | ||
| long totalNanos = 0; | ||
|
|
||
| var inputMat = frame.colorImage.getMat(); | ||
| boolean emptyIn = inputMat.empty(); | ||
| Mat displayMat = new Mat(); | ||
| double variance = 0.0; | ||
|
|
||
| if (!emptyIn) { | ||
| totalNanos += resizeImagePipe.run(inputMat).nanosElapsed; | ||
|
|
||
| var focusResult = focusPipe.run(inputMat); | ||
| totalNanos += focusResult.nanosElapsed; | ||
| variance = focusResult.output.variance; | ||
| displayMat = focusResult.output.frame; | ||
| } | ||
|
|
||
| var fpsResult = calculateFPSPipe.run(null); | ||
| var fps = fpsResult.output; | ||
|
|
||
| var processedCVMat = new CVMat(displayMat); | ||
|
|
||
| return new FocusPipelineResult( | ||
| frame.sequenceID, | ||
| MathUtils.nanosToMillis(totalNanos), | ||
| fps, | ||
| new Frame( | ||
| frame.sequenceID, | ||
| frame.colorImage, | ||
| processedCVMat, | ||
| frame.type, | ||
| frame.frameStaticProperties), | ||
| variance); | ||
| } | ||
|
|
||
| @Override | ||
| public void release() { | ||
| // we never actually need to give resources up since pipelinemanager only makes | ||
| // one of us | ||
| } | ||
| } |
Oops, something went wrong.
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.