2

I'm working with OpenCV on Jupyter notebooks. The problem is the kernel crashes when the user calls functions like cv2.imshow(). There are couple ways to deal with this one is redirecting the image to matplotlibs and another one is using waitKey(0) to wait input and close the windows with pressing any key.

In my case, in order to calculate homography first I have to select four points.

Mouse Handler:

def mouse_handler(event, x, y, flags, data):
    if event == cv2.EVENT_LBUTTONDOWN :
        cv2.circle(data['im'], (x,y),3, (0,0,255), 5, 16);
        cv2.imshow("Image", data['im']);
        if len(data['points']) < 4 :
            data['points'].append([x,y])

Data collector:

def get_four_points(im):
    
    # Set up data to send to mouse handler
    data = {}
    data['im'] = im.copy()
    data['points'] = []
    
    #Set the callback function for any mouse event
    cv2.imshow("Image",im)
    cv2.setMouseCallback("Image", mouse_handler, data)
    cv2.waitKey(0)
    
    # Convert array to np.array
    points = np.vstack(data['points']).astype(float)
    
    return points

Eventually I'll have to click on the image. So I can't solve this problem with the methods mentioned above. How can I solve this?

Source: https://github.com/spmallick/learnopencv/blob/master/Homography/utils.py

(Note: I'm using virtual environment.)

patoglu
  • 401
  • 4
  • 16

1 Answers1

0

You should check this: opencv.imshow will cause jupyter notebook crash

Also, you should apply %matplotlib notebook as stated in: Save click events for Jupyter Notebook

rober_dinero
  • 311
  • 2
  • 7