opencv.imshow will cause jupyter notebook crash opencv.imshow will cause jupyter notebook crash python python

opencv.imshow will cause jupyter notebook crash


%matplotlib inline#The line above is necesary to show Matplotlib's plots inside a Jupyter Notebookimport cv2from matplotlib import pyplot as plt#Import imageimage = cv2.imread("input_path")#Show the image with matplotlibplt.imshow(image)plt.show()


I was having a similar problem, and could not come to a good solution with cv2.imshow() in the Jupyter Notebook. I followed this stackoverflow answer, just using matplotlib to display the image.

import matplotlib.pyplot as plt# load image using cv2....and do processing.plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))# as opencv loads in BGR format by default, we want to show it in RGB.plt.show()


The API documentation for cv2.waitKey() notes the following:

This function is the only method in HighGUI that can fetch and handle events, so it needs to be called periodically for normal event processing unless HighGUI is used within an environment that takes care of event processing.

So perhaps calling the function in an endless loop would make the window responsive? I haven't tested this, but maybe you would like to try the following:

import cv2cvim2disp = cv2.imread('data/home.jpg')cv2.imshow('img', cvim2disp)while(True):    k = cv2.waitKey(33)    if k == -1:  # if no key was pressed, -1 is returned        continue    else:        breakcv2.destroyWindow('img')