I am trying a really simple OpenCV example in Python, which completely fails to work for me:
import cv2
import matplotlib.pyplot as plt
if __name__ == '__main__':
img1 = plt.imread('../data/input/frame000013.png')
img2 = plt.imread('../data/input/frame000014.png')
img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT()
print(sift)
kp = sift.detect(img1_gray, None)
print(kp)
If I try to run it with PyCharm, I get the following error:
Process finished with exit code -1073741819 (0xC0000005)
If I try to execute it through the terminal, it just does not seem to get past kp = sift.detect(img1_gray, None)
line and does not really get to second print
statement, without providing any error notification whatsoever.
I use: Windows 10, Python 3.8, opencv-python
&opencv-contrib-python
4.4.0.44.
If you look at the documentation, it should be SIFT_create()
.
sift = cv2.SIFT_create()
print(sift)
kp = sift.detect(img1_gray, None)
But, why are you reading images with plt.imread
?
The reason I'm asking, you are converting to the gray-scale using BGR2GRAY
. But plt.imread
returns image as RGB
format. Therefore, if you are going to use plt.imread
:
import cv2
import numpy as np
import matplotlib.pyplot as plt
def rgb2gray(rgb):
return np.dot(rgb[..., :3], [0.2989, 0.5870, 0.1140]).astype(np.uint8)
if __name__ == '__main__':
img1 = plt.imread('../data/input/frame000013.png')
img2 = plt.imread('../data/input/frame000014.png')
img1_gray = rgb2gray(img1)
sift = cv2.SIFT_create()
print(sift)
kp = sift.detect(img1_gray, None)
print(kp)
or if you choose cv2.imread
:
import cv2
import matplotlib.pyplot as plt
if __name__ == '__main__':
img1 = cv2.imread('../data/input/frame000013.png')
img2 = cv2.imread('../data/input/frame000014.png')
img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT_create()
print(sift)
kp = sift.detect(img1_gray, None)
print(kp)
User contributions licensed under CC BY-SA 3.0