Home > Net >  I can't run open CV in python on visual studio voce, but if I run in other text editor(Atom,sub
I can't run open CV in python on visual studio voce, but if I run in other text editor(Atom,sub

Time:09-28

I can't run open CV in Python on Visual Studio voce, but if I run in other text editor (Atom, sublime) it works fine.

import cv2
img = cv2.imread("starry_night.jpg",-1)
cv2.imshow('Paul', img)
cv2.waitKey()

cv2.error:

OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-1i5nllza\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

enter image description here

Same code and same file.

CodePudding user response:

Reason:

Can not find the picture with the path you provided. Because the path depends on the cwd(you can get it with os.getcwd()).

With your code, the path will be like this: {path to PYTHON project}/starry_night.jpg, but it should be this: {path to PYTHON project}/Kuliah/starry_night.jpg,

Solution:

Change

img = cv2.imread("starry_night.jpg",-1)

to

img = cv2.imread("Kuliah\\starry_night.jpg",-1)

Or you can use the absolute path as eyllanesc suggested.

  • Related