cv2.SimpleBlobDetector_create() not accept parameters in OpenCV 4.10.0

Hello everyone,

I’m having an error when I want to specify values ​​for parameters to create a Blob detector in Python 3.12.7
Follow this code:


params = cv2.SimpleBlobDetector_Params

params.filterByArea = True #Ativa
params.minArea = 100 #Área minima de 100

params.filterByCircularity = True #Ativa
params.minCircularity = 0.9 #Testar e ver qual se adequa mais a forma de interesse
# 0 a 1, sendo 1, o circulo perfeito


params.filterByConvexity = False #Desativa o parametro
params.minConvexity = 0.2
    
params.filterByInertia = True #Ativa
params.minInertiaRatio = 0.01 # 1 para full simétrico e abaixo de 1, imperfeições.

"""
MinInertiaRatio - 0.8 a 1 (Circulos ou quase circulos)
MinInertiaRatio - 0.2 a 0.5 (Objetos alongados e elípticos)
MinInertiaRatio - < 0.2 (Qualquer coisa)
"""
#Recriando o detector com os parametros novos
detector = cv2.SimpleBlobDetector_create(params)

ERROR: TypeError: Expected cv::SimpleBlobDetector::Params for argument 'parameters'

The create function does not accept the parameter variable. Does anyone know how to fix it?

When i use

params = cv2.SimpleBlobDetector_Params()

Also gives error (AttributeError: ‘cv2.SimpleBlobDetector.Params’ object attribute ‘filterByArea’ is read-only)

take a look at learnopencv/BlobDetector at master · spmallick/learnopencv

you have to call the constructor. there is no call there.

Not work with version verification.

With Constructor (params = cv2.SimpleBlobDetector_Params()), also not work.
My current code :

# Carregando a imagem
image = cv2.imread("images/blobs.jpg", 0)
imshow('Original Image',image)

# Inicializa o detector de blobs
detector = cv2.SimpleBlobDetector_create()
 
# Pegando os blobs
keypoints = detector.detect(image)
 
# Desenhando os blobs. 
blank = np.zeros((1,1)) #Cria uma matriz pequena de zeros (só para passar uma imagem vazia como parametro)
blobs = cv2.drawKeypoints(image, keypoints, blank, (0,0,255),
                                      cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

number_of_blobs = len(keypoints)
text = "Total de Blobs: " + str(len(keypoints))
cv2.putText(blobs, text, (20, 550), cv2.FONT_HERSHEY_SIMPLEX, 1, (100, 0, 255), 2)

# Display image with blob keypoints
imshow("Blobs usando os parâmetros padrões", blobs)

# Setar filtros de parametros
# Vamos pegar os parametros base da função cv2.SimpleBlobDetector_Params
params = cv2.SimpleBlobDetector_Params

# PARÂMETRO DO ASPECTO ÁREA (TAMANHO DO OBJETO)
params.filterByArea = True #Ativa
params.minArea = 100 #Área minima de 100

# PARÂMETRO DA CIRCULARIDADE (O QUANTO A FORMA É PRÓXIMA A UM CIRCULO PERFEITO)
params.filterByCircularity = True #Ativa
params.minCircularity = 0.9 #Testar e ver qual se adequa mais a forma de interesse
# 0 a 1, sendo 1, o circulo perfeito

# PARÂMETRO DA CONVEXIDADE
params.filterByConvexity = False #Desativa o parametro
params.minConvexity = 0.2
    
# PARÂMETRO DA INERCIA
params.filterByInertia = True #Ativa
params.minInertiaRatio = 0.01 # 1 para full simétrico e abaixo de 1, imperfeições.

"""
MinInertiaRatio - 0.8 a 1 (Circulos ou quase circulos)
MinInertiaRatio - 0.2 a 0.5 (Objetos alongados e elípticos)
MinInertiaRatio - < 0.2 (Qualquer coisa)
"""
#Recriando o detector com os parametros novos
# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
    detector = cv2.SimpleBlobDetector(params)
else: 
    detector = cv2.SimpleBlobDetector_create(params)
    
# Detect blobs
keypoints = detector.detect(image)

# Draw blobs on our image as red circles
blank = np.zeros((1,1)) 
blobs = cv2.drawKeypoints(image, keypoints, blank, (0,255,0),
                                      cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

number_of_blobs = len(keypoints)
text = "Number of Circular Blobs: " + str(len(keypoints))
cv2.putText(blobs, text, (20, 550), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 100, 255), 2)

# Show blobs
imshow("Filtering Circular Blobs Only", blobs)

My error: TypeError: Expected cv::SimpleBlobDetector::Params for argument 'parameters' in line detector = cv2.SimpleBlobDetector.create(params)

In OpenCV 4.7.0 also not work.
Still atributtes can’t are change.

if you keep ignoring the advice, you won’t get anywhere.

>>> p = cv.SimpleBlobDetector_Params()
>>> p.filterByArea = True # works
>>> p.filterByArea = False # works too

that happens because you altered the SimpleBlobDetector_Params class before you made an instance of the class.

>>> p = cv.SimpleBlobDetector_Params
>>> p.filterByArea = True # alters the class, fatally
>>> p = cv.SimpleBlobDetector_Params()
>>> p.filterByArea = True # tries to alter an attribute of an instance
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'cv2.SimpleBlobDetector.Params' object attribute 'filterByArea' is read-only

My code is correct.
Solve error TypeError: Expected cv::SimpleBlobDetector::Params for argument 'parameters' restart the VSCode. The Cache generate bug.
My env with: OpenCV 4.10.82 (contrib also) with numpy 1.26