-
Notifications
You must be signed in to change notification settings - Fork 134
Reflector focus - Focus maintenance device based on a camera, shutter and stage #803
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c6b6bc9
Reflection Autofocus device. Previously experimentally in Utilities …
nicost bef5473
ReflectionFocus: changed name to ReflectionFocus from Autofocus every…
nicost 38fa2cb
ReflectionFocus: fix typos and punctuation.
nicost 54b0540
ReflectionFocus: implement most CoPilot suggestions (except for more …
nicost 9fe64a7
ReflectionFocus: adds some return value checks, minor comments and wh…
nicost 8489931
ReflectorFocus: remove wrongly added copyright from license file.
nicost 4fe5871
reflectionFocus: factored out algorithm to make it easier to add othe…
nicost 2418040
ReflectorFocus: Add missing file to Makefile.am
nicost 81d073f
ReflectorFocus: Avoid race condition in accessing calibraion data. A…
nicost f99056f
Merge branch 'main' into ReflectorFocus
nicost 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,225 @@ | ||
|
|
||
| #include "ReflectionFocus.h" | ||
| #include <algorithm> | ||
|
|
||
|
|
||
| int DetectTwoBrightSpots::AnalyzeImage(ImgBuffer img, double& score1, double& x1, double& y1, double& score2, double& x2, double& y2) | ||
| { | ||
| // Find up to two spots in the image, return their (x,y) coordinates and scores | ||
|
|
||
| const unsigned int width = img.Width(); | ||
| const unsigned int height = img.Height(); | ||
| const unsigned int pixDepth = img.Depth(); | ||
| const unsigned char* pixels = img.GetPixels(); | ||
|
|
||
| if (pixels == nullptr || width == 0 || height == 0) | ||
| { | ||
| score1 = score2 = 0.0; | ||
| x1 = y1 = x2 = y2 = 0.0; | ||
| return DEVICE_OK; | ||
| } | ||
|
|
||
| // Calculate threshold using Otsu's method or use a simple percentile-based threshold | ||
| // For bright spots on dark background, we'll use a high percentile threshold | ||
| std::vector<unsigned char> intensities; | ||
| intensities.reserve(width * height); | ||
|
|
||
| if (pixDepth == 1) | ||
| { | ||
| // 8-bit images: copy directly | ||
| for (unsigned int i = 0; i < width * height; ++i) | ||
| { | ||
| intensities.push_back(pixels[i]); | ||
| } | ||
| } | ||
| else if (pixDepth == 2) | ||
| { | ||
| // 16-bit images: find max value and scale to 8-bit range | ||
| const unsigned short* pixels16 = (const unsigned short*)pixels; | ||
| unsigned short maxValue = 0; | ||
|
|
||
| // First pass: find maximum value | ||
| for (unsigned int i = 0; i < width * height; ++i) | ||
| { | ||
| if (pixels16[i] > maxValue) | ||
| maxValue = pixels16[i]; | ||
| } | ||
|
|
||
| // Second pass: scale to 8-bit range | ||
| if (maxValue > 0) | ||
| { | ||
| double scale = 255.0 / maxValue; | ||
| for (unsigned int i = 0; i < width * height; ++i) | ||
| { | ||
| unsigned char scaledValue = static_cast<unsigned char>(pixels16[i] * scale); | ||
| intensities.push_back(scaledValue); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // All pixels are zero - no spots to find | ||
| score1 = score2 = 0.0; | ||
| x1 = y1 = x2 = y2 = 0.0; | ||
| return DEVICE_OK; | ||
| } | ||
| } | ||
|
|
||
| // Find threshold as 95th percentile to isolate bright spots | ||
| std::vector<unsigned char> sorted = intensities; | ||
| std::sort(sorted.begin(), sorted.end()); | ||
| unsigned char threshold = sorted[static_cast<size_t>(sorted.size() * 0.99)]; | ||
|
|
||
| // Label connected components using flood fill | ||
| std::vector<int> labels(width * height, -1); | ||
| int currentLabel = 0; | ||
|
|
||
| struct Spot { | ||
| double sumX = 0.0; | ||
| double sumY = 0.0; | ||
| double sumIntensity = 0.0; | ||
| double sumX2 = 0.0; // For calculating variance/spread | ||
| double sumY2 = 0.0; | ||
| int pixelCount = 0; | ||
| unsigned char maxIntensity = 0; | ||
| }; | ||
|
|
||
| std::vector<Spot> spots; | ||
|
|
||
| // Flood fill to find connected components | ||
| for (unsigned int y = 0; y < height; ++y) | ||
| { | ||
| for (unsigned int x = 0; x < width; ++x) | ||
| { | ||
| unsigned int idx = y * width + x; | ||
| unsigned char intensity = intensities[idx]; | ||
|
|
||
| if (intensity >= threshold && labels[idx] == -1) | ||
| { | ||
| // Start new component | ||
| spots.push_back(Spot()); | ||
| std::vector<std::pair<unsigned int, unsigned int>> stack; | ||
| stack.push_back(std::make_pair(x, y)); | ||
| labels[idx] = currentLabel; | ||
|
|
||
| while (!stack.empty()) | ||
| { | ||
| std::pair<unsigned int, unsigned int> pos = stack.back(); | ||
| stack.pop_back(); | ||
| unsigned int px = pos.first; | ||
| unsigned int py = pos.second; | ||
| unsigned int pidx = py * width + px; | ||
| unsigned char pIntensity = intensities[pidx]; | ||
|
|
||
| // Add to spot statistics | ||
| spots[currentLabel].sumX += px * pIntensity; | ||
| spots[currentLabel].sumY += py * pIntensity; | ||
| spots[currentLabel].sumIntensity += pIntensity; | ||
| spots[currentLabel].sumX2 += px * px * pIntensity; | ||
| spots[currentLabel].sumY2 += py * py * pIntensity; | ||
| spots[currentLabel].pixelCount++; | ||
| if (pIntensity > spots[currentLabel].maxIntensity) | ||
| spots[currentLabel].maxIntensity = pIntensity; | ||
|
|
||
| // Check 8-connected neighbors | ||
| for (int dy = -1; dy <= 1; ++dy) | ||
| { | ||
| for (int dx = -1; dx <= 1; ++dx) | ||
| { | ||
| if (dx == 0 && dy == 0) continue; | ||
|
|
||
| int nx = px + dx; | ||
| int ny = py + dy; | ||
|
|
||
| if (nx >= 0 && nx < (int)width && ny >= 0 && ny < (int)height) | ||
| { | ||
| unsigned int nidx = ny * width + nx; | ||
| if (intensities[nidx] >= threshold && labels[nidx] == -1) | ||
| { | ||
| labels[nidx] = currentLabel; | ||
| stack.push_back(std::make_pair(nx, ny)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| currentLabel++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Calculate centroid, variance, and score for each spot | ||
| struct SpotResult { | ||
| double x = 0.0; | ||
| double y = 0.0; | ||
| double score = 0.0; | ||
| double totalIntensity = 0.0; | ||
| }; | ||
|
|
||
| std::vector<SpotResult> results; | ||
| for (size_t i = 0; i < spots.size(); ++i) | ||
| { | ||
| if (spots[i].sumIntensity > 0) | ||
| { | ||
| SpotResult result; | ||
| result.x = spots[i].sumX / spots[i].sumIntensity; | ||
| result.y = spots[i].sumY / spots[i].sumIntensity; | ||
| result.totalIntensity = spots[i].sumIntensity; | ||
|
|
||
| // Calculate variance (spread) of the spot | ||
| double varX = (spots[i].sumX2 / spots[i].sumIntensity) - (result.x * result.x); | ||
| double varY = (spots[i].sumY2 / spots[i].sumIntensity) - (result.y * result.y); | ||
| double spread = sqrt(varX + varY); | ||
|
|
||
| // Score: smaller spread = better focus = higher score | ||
| // Use inverse of spread, normalized by total intensity | ||
| if (spread > 0.0) | ||
| result.score = spots[i].sumIntensity / (spread * spread); | ||
| else | ||
| result.score = spots[i].sumIntensity * 1000.0; // Very small spot | ||
|
|
||
| results.push_back(result); | ||
| } | ||
| } | ||
|
|
||
| // Sort spots by intensity | ||
| std::sort(results.begin(), results.end(), | ||
| [](const SpotResult& a, const SpotResult& b) { | ||
| return a.totalIntensity > b.totalIntensity; | ||
| }); | ||
|
|
||
| // Return top 2 spots, highest score first | ||
| if (results.size() >= 2) | ||
| { | ||
| if (results[0].score >= results[1].score) | ||
| { | ||
| x1 = results[0].x; | ||
| y1 = results[0].y; | ||
| score1 = results[0].score; | ||
| x2 = results[1].x; | ||
| y2 = results[1].y; | ||
| score2 = results[1].score; | ||
| } | ||
| else | ||
| { | ||
| x1 = results[1].x; | ||
| y1 = results[1].y; | ||
| score1 = results[1].score; | ||
| x2 = results[0].x; | ||
| y2 = results[0].y; | ||
| score2 = results[0].score; | ||
| } | ||
| } | ||
| else if (results.size() == 1) | ||
| { | ||
| x1 = results[0].x; | ||
| y1 = results[0].y; | ||
| score1 = results[0].score; | ||
| x2 = y2 = score2 = 0.0; | ||
| } | ||
| else | ||
| { | ||
| x1 = y1 = score1 = 0.0; | ||
| } | ||
|
|
||
| return DEVICE_OK; | ||
| } |
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,18 @@ | ||
|
|
||
| AM_CXXFLAGS = $(MMDEVAPI_CXXFLAGS) | ||
| deviceadapter_LTLIBRARIES = libmmgr_dal_ReflectionFocus.la | ||
| libmmgr_dal_ReflectionFocus_la_SOURCES = \ | ||
| ImageAnalyzers.cpp \ | ||
| ReflectionFocus.cpp \ | ||
| ReflectionFocusStage.cpp \ | ||
| ReflectionFocusModule.cpp \ | ||
| ReflectionFocus.h | ||
|
|
||
| libmmgr_dal_ReflectionFocus_la_LIBADD = $(MMDEVAPI_LIBADD) | ||
| libmmgr_dal_ReflectionFocus_la_LDFLAGS = $(MMDEVAPI_LDFLAGS) | ||
|
|
||
| EXTRA_DIST = \ | ||
| ReflectionFocus.vcxproj \ | ||
| ReflectionFocus.vcxproj.filters \ | ||
| license.txt | ||
|
|
||
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.