1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 23:33:05 +04:00

Merge pull request #28818 from jeevan6996:fix-charuco-detectdiamonds-clear-stale-output

objdetect: clear stale outputs in CharucoDetector::detectDiamonds #28818

## Summary
- clear `diamondCorners`/`diamondIds` outputs at the beginning of `CharucoDetector::detectDiamonds`
- prevent stale detections from previous calls when the current call has fewer than 4 markers or finds no diamonds
- add a regression test that pre-fills outputs, calls `detectDiamonds` with 3 markers, and verifies outputs are empty

Fixes #28783.

## Testing
- built `opencv_test_objdetect` locally
- ran `opencv_test_objdetect --gtest_filter=Charuco.detectDiamondsClearsOutputsWithLessThanFourMarkers`
- result: PASS
This commit is contained in:
Jeevan Mohan Pawar
2026-04-22 13:42:37 +01:00
committed by GitHub
parent 1e61a2283d
commit 273e52ce8d
2 changed files with 44 additions and 7 deletions
@@ -414,7 +414,12 @@ void CharucoDetector::detectDiamonds(InputArray image, OutputArrayOfArrays _diam
// stores if the detected markers have been assigned or not to a diamond
vector<bool> assigned(_markerIds.total(), false);
if(_markerIds.total() < 4ull) return; // a diamond need at least 4 markers
if(_markerIds.total() < 4ull)
{
if (_diamondCorners.needed()) _diamondCorners.release();
if (_diamondIds.needed()) _diamondIds.release();
return; // a diamond need at least 4 markers
}
// convert input image to grey
Mat grey;
@@ -518,16 +523,27 @@ void CharucoDetector::detectDiamonds(InputArray image, OutputArrayOfArrays _diam
if(diamondIds.size() > 0ull) {
// parse output
Mat(diamondIds).copyTo(_diamondIds);
if (_diamondIds.needed())
{
Mat(diamondIds).copyTo(_diamondIds);
}
_diamondCorners.create((int)diamondCorners.size(), 1, CV_32FC2);
for(unsigned int i = 0; i < diamondCorners.size(); i++) {
_diamondCorners.create(4, 1, CV_32FC2, i, true);
for(int j = 0; j < 4; j++) {
_diamondCorners.getMat(i).at<Point2f>(j) = diamondCorners[i][j];
if (_diamondCorners.needed())
{
_diamondCorners.create((int)diamondCorners.size(), 1, CV_32FC2);
for(unsigned int i = 0; i < diamondCorners.size(); i++) {
_diamondCorners.create(4, 1, CV_32FC2, i, true);
for(int j = 0; j < 4; j++) {
_diamondCorners.getMat(i).at<Point2f>(j) = diamondCorners[i][j];
}
}
}
}
else
{
if (_diamondCorners.needed()) _diamondCorners.release();
if (_diamondIds.needed()) _diamondIds.release();
}
}
void drawDetectedCornersCharuco(InputOutputArray _image, InputArray _charucoCorners,