Skip to content

SimpleBlobDetector finds incorrect blob center for inverted image #24388

Open
@devbanu

Description

@devbanu

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

Consider an image:
1input

and its inverse:
inverse_1input

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
3keypoint

and for the inverse image:
(116.74179077148438, 119.02967071533203) 51.787166595458984
inverse_3keypoint

The difference seems to come from this call to findContours():

findContours(binaryImage, contours, RETR_LIST, CHAIN_APPROX_NONE);

which results in different contours for the above cases.

Here is the one for the initial image:
2contours
and for the inverted image:
inverse_2contours

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)

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy