Opencv with Python error occurs "Assertion failed"

Asked

Viewed 77 times

0

I recently started working with Opencv in Python. Well, I’m making a code here and, when compiling, gives the following error:

Ocorreu uma exceção: error
OpenCV(4.4.0) /tmp/pip-req-build-a3np7pha/opencv/modules/core/src/batch_distance.cpp:282: error: (-215:Assertion failed) (type == CV_8U && dtype == CV_32S) || dtype == CV_32F in function 'batchDistance'
  File "/home/vivi/Documentos/GitHub/LAPISCO/PYTHON_OPCV/QUESTÃO51/questão51.py", line 30, in <module>
    matches = bf.match(descriptors_1, descriptors_2)

Could someone with more experience tell me what exactly is going on? I confess that I am half lost.

Follows the code:

# Utilizar o método Scale Invariant Feature Transform (SIFT) para detectar onde 
# está um objeto conhecido à priori em uma imagem através dos keypoints
# detectados #por este método. Desenhar os keypoints em comum e marcar o objeto procurado na imagem.

import cv2

# Read the rgb images
image = cv2.imread('image.jpg')
logo = cv2.imread('logo.jpg')

# Convert the images to grayscale
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
logo = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)

# Create a SIFT detector (Obs.: in order to use sift descriptor you will have to install opencv-contrib
# from scratch. An alternative to that is to use opencv implementation called orb, it has almost the
# same result. To use orb, comment the lines with sift and uncomment the lines with orb.)
sift = cv2.xfeatures2d.SIFT_create()
# orb = cv2.ORB_create()

# Find the keypoints and descriptors with SIFT
keypoints_1, descriptors_1 = sift.detectAndCompute(image, None)
keypoints_2, descriptors_2 = sift.detectAndCompute(logo, None)

# keypoints_1, descriptors_1 = orb.detectAndCompute(image, None)
# keypoints_2, descriptors_2 = orb.detectAndCompute(logo, None)

# Create a BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

# Match descriptors
matches = bf.match(descriptors_1, descriptors_2)

# Draw only the first match
result = cv2.drawMatches(image, keypoints_1, logo, keypoints_2, matches[:2], None, flags=2)

# Show the result
cv2.imshow('Result', result)
cv2.waitKey(0)

# Save the result
cv2.imwrite('sift_result.jpg', result)
  • SIFT was not removed from Opencv in version 3.5?

  • See if this post helping.

  • It appears that SIFT is not installed by default, but can be installed.

  • Hmm, I got it... As the SIFT was giving error, I used the Orb and gave more or less right... Why the correspondence started to get mixed up.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.