2

I am using Kaggle Python and I am trying to edit images with OpenCV. I am simply trying to crop the image.I am able to do it with Matplotlib but I want to use OpenCv. When I excecute the code, it does not give me any message and it deletes all variables. It is like restarting the whole kernel. The varialbe img is not created and even variables created previously are deleted. Any idea is very appreciated.

import cv2
img = cv2.imread("/kaggle/input/global-wheat-detection/train/07479da31.jpg")
crop_img = img[715:834, 108:176]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)
Luis Vivas
  • 95
  • 3
  • 9

1 Answers1

2

You cannot use cv2.imshow in a Kaggle notebook. It requires a Qt backend which the Kaggle notebook is not set up for which is why your notebook is crashing. In addition, cv2.imshow opens up a separate window which of course the notebook environment is also not set up for. Therefore, you unfortunately cannot use OpenCV windowing or interactive functionality in the notebook. Since Matplotlib is working for you, you need to use that.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • 1
    Oh, Ray, thanks for that answer, I was crazy trying to find why the notebook was behaving like that. I thougth it was a diabolic soul deleting all the viarables. Understood, I will use Matplotlib. – Luis Vivas Jun 27 '20 at 19:16
  • 1
    If you're interested, Google Colab is able to use `cv2.imshow`: https://stackoverflow.com/questions/57090598/using-cv2-imshow-in-google-colab - However, this is not implemented in Kaggle. – rayryeng Jun 29 '20 at 06:28
  • Good to know that Ray. I think I will be switching. – Luis Vivas Jun 30 '20 at 09:05