Python Forum

Full Version: Segmentation fault (core dumped)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Any idea what is wrong with this code here and gives me Segmentation fault (core dumped)??? I got the code from here:
https://docs.opencv.org/3.0-beta/doc/py_...homography

This is the code:

import numpy as np
import cv2
from matplotlib import pyplot as plt
print("1")
MIN_MATCH_COUNT = 10
print("2")
img1 = cv2.imread("/Homography/car.jpg",0)          # queryImage
print("3")
img2 = cv2.imread("/Homography/scenery.jpg",0) # trainImage
print("4")
# Initiate SIFT detector
sift = cv2.SIFT()
print("5")
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
print("6")
kp2, des2 = sift.detectAndCompute(img2,None)
print("7")
FLANN_INDEX_KDTREE = 0
print("8")
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
print("9")
search_params = dict(checks = 50)
print("10")

flann = cv2.FlannBasedMatcher(index_params, search_params)
print("11")
matches = flann.knnMatch(des1,des2,k=2)
print("12")
# store all the good matches as per Lowe's ratio test.
good = []
print("13")
for m,n in matches:
    print("14")
    if m.distance < 0.7*n.distance:
        print("15")
        good.append(m)
        
if len(good)>MIN_MATCH_COUNT:
    print("16")
    src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
    print("17")
    dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)
    print("18")
    M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
    print("19")
    matchesMask = mask.ravel().tolist()
    print("20")
    h,w = img1.shape
    print("21")
    pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
    print("22")
    dst = cv2.perspectiveTransform(pts,M)
    print("23")
    img2 = cv2.polylines(img2,[np.int32(dst)],True,255,3, cv2.LINE_AA)
    print("24")
else:
    print("Not enough matches are found - %d/%d" % (len(good),MIN_MATCH_COUNT))
    print("25")
    matchesMask = None
    print("26")
    
draw_params = dict(matchColor = (0,255,0), # draw matches in green color
                   singlePointColor = None,
                   matchesMask = matchesMask, # draw only inliers
                   flags = 2)
print("27")
img3 = cv2.drawMatches(img1,kp1,img2,kp2,good,None,**draw_params)
print("28")
plt.imshow(img3, 'gray'),plt.show()
print("29")
The error I get is this:

Error:
1 2 3 4 5 Segmentation fault (core dumped)
This is a bug in Python or a library. The whole point of Python is to avoid any possibility of getting a segmentation fault. If you get one, you have discovered a bug. Python cannot generate seg faults based on your program; if there is one, it is a mistake in the implementation of Python or the library that you are calling, which may have been written in C for performance. Not your problem. The bug appears to be in sift.detectAndCompute() based on the output produced. It failed between lines 13 (successful printout) and 16 so the only possibility is line 15. File a bug report. Probably for the cv2 library.