Description
System Information
I reproduce this issue on the following platforms:
// C++
OpenCV version: 4.8.1 (top of 4.x branch)
Operating System / Platform: macOS 13.6 Ventura
Compiler & compiler version: Apple clang version 15.0.0 (clang-1500.0.40.1)
// Python
OpenCV python version: opencv-python==4.8.1.78
Operating System / Platform: macOS 13.6 Ventura
Python version: 3.11.5
Other platforms should reproduce this too.
Detailed description
If I try to use the SimpleBlobDetector
like this:
import sys
import cv2
image = cv2.imread(sys.argv[1], cv2.IMREAD_GRAYSCALE)
parameters = cv2.SimpleBlobDetector_Params()
parameters.filterByArea = False
parameters.blobColor = 255 # Use 255 and 0 for initial and inverse respectively.
detector = cv2.SimpleBlobDetector_create(parameters)
keypoints = detector.detect(image)
for kp in keypoints:
print(kp.pt, kp.size)
the results will be different.
For the initial image:
(122.56725311279297, 121.07057189941406) 49.52946853637695
and for the inverse image:
(116.74179077148438, 119.02967071533203) 51.787166595458984
The difference seems to come from this call to findContours()
:
which results in different contours for the above cases.
Here is the one for the initial image:
and for the inverted image:
The extra contour in the inverted image is because findContours()
is hardcoded to use 0 (black) as background and 255 (white) as foreground.
The solution here is to consider the value params.blobColor
when calling findContours()
and to make the behaviour symmetric with respect to initial and inverted cases
I can create a patch/PR with a proposed solution and unit test for direct and inverse cases.
Steps to reproduce
Use the code provided in detailed description on provided images.
Issue submission checklist
- I report the issue, it's not a question
- I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- I updated to the latest OpenCV version and the issue is still there
- There is reproducer code and related data files (videos, images, onnx, etc)