我得到的错误是
“Traceback(最近一次调用最后一次):文件 “C:\ Users \ harit \ Desktop \ red.py”,第6行,in cv2.imshow('img',img)错误:C:\ projects \ opencv-python \ opencv \ modules \ highgui \ src \ window.cpp:304: 错误:(-215)size.width> 0&& size.height> 0 in function cv :: imshow“
相关代码如下:
import cv2
import numpy as np
img = cv2.imread('C:\Users\harit\Desktop\images\12.jpg')
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
答案 0 :(得分:0)
尝试更改
img = cv2.imread('C:\Users\harit\Desktop\images\12.jpg')
到
img = cv2.imread(r'C:\Users\harit\Desktop\images\12.jpg')
反斜杠是一个转义字符,引号前的r告诉python"忽略"它们。
例如:
>>> s = 'C:\Users\harit\Desktop\images\12.jpg'
>>> print s
C:\Users\harit\Desktop\images
.jpg
这是错误的道路!...' r'在字符串前面:
>>> s = r'C:\Users\harit\Desktop\images\12.jpg'
>>> print s
C:\Users\harit\Desktop\images\12.jpg
有关原始字符串的更多信息,请参阅https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals