2

I'm trying to run the example from here

import cv2

def viewImage(image):
    cv2.namedWindow('Display', cv2.WINDOW_NORMAL)
    cv2.imshow('Display', image)
    print('test')
    cv2.waitKey(0)
    cv2.destroyAllWindows()
def grayscale_17_levels (image):
    high = 255
    while(1):  
        low = high - 15
        col_to_be_changed_low = np.array([low])
        col_to_be_changed_high = np.array([high])
        curr_mask = cv2.inRange(gray, col_to_be_changed_low,col_to_be_changed_high)
        gray[curr_mask > 0] = (high)
        high -= 15
        if(low == 0 ):
            break

image = cv2.imread('ombre_circle.png')
viewImage(image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
grayscale_17_levels(gray)
viewImage(gray)

Whenever I run the code I get the error:

Kernel Restarting
The kernel for main.ipynb appears to have died. It will restart automatically.

I when I comment out these lines:

#cv2.namedWindow('Display', cv2.WINDOW_NORMAL)
#cv2.imshow('Display', image)

the core runs and prints out 'test' and I don't get an error.

I'm using:

  • Ubuntu-server 18.04
  • Jupyter lab 1.1.3
  • opencv-python 4.1.1.26

I run this on a server not on my local environment

I found a workaround for this issue by displaying it with Matplotlib:

def viewImage(image):
    plt.subplot(122)
    plt.title("RGB")
    plt.imshow(image)
    plt.show()

image = cv2.imread('img/ombre_circle.png')
viewImage(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
grayscale_17_levels(gray)
viewImage3(cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB))

However this doesn't solve the issue and takes extra transformations/code so I would like to find a solution to display with opencv.

X11 forwarding is enabled.

I tried opening an SSH connection with the -Y and -C flag (via this question) but this doesn't fix it.

Any ideas what could be the issue?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3605780
  • 6,542
  • 13
  • 42
  • 67

4 Answers4

0

IPython Github Issue

What I understand from the discussion is that it's a C-level linking or runtime error from the openCV code being run. I faced the same problem, got solved by using matplotlib SO jupyternb crash

Aakash Gupta
  • 716
  • 6
  • 11
0

image=cv2.imread("the file")
cv2.imshow("test file", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

code-freeze
  • 465
  • 8
  • 8
0

I encountered similar issue when I'm loading large tiff files. Same code works for small tiff files. You can look at this post to compress your image then display.

Chloe
  • 71
  • 1
  • 7
0

cv2.imshow() would cause Jupyter sessions to crash: this post of the issue.

As a substitution, please consider using from google.colab.patches import cv2_imshow on Google Colab.

Cloud Cho
  • 1,594
  • 19
  • 22