尝试运行OpenCV项目时出现错误。错误提示:
OpenCV(3.4.1) Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file C:\projects\opencv‑python\opencv\modules\highgui\src\window.cpp, line 356
OpenCV(3.4.1) Error: Assertion failed (scn == 1 && (dcn == 3 || dcn == 4)) in cv::cvtColor, file C:\projects\opencv‑python\opencv\modules\imgproc\src\color.cpp, line 11164
代码如下:
import cv2
num_down = 2 # number of downsampling steps
num_bilateral = 7 # number of bilateral filtering steps
img_rgb = cv2.imread("C:/Users/Ashish Joshi/Pictures/Camera Roll/DSC01070.JPG")
# downsample image using Gaussian pyramid
img_color = img_rgb
for _ in range(num_down):
img_color = cv2.pyrDown(img_color)
# repeatedly apply small bilateral filter instead of
# applying one large filter
for _ in range(num_bilateral):
img_color = cv2.bilateralFilter(img_color, d=9,
sigmaColor=9,
sigmaSpace=7)
# upsample image to original size
for _ in range(num_down):
img_color = cv2.pyrUp(img_color)
# convert to grayscale and apply median blur
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
img_blur = cv2.medianBlur(img_gray, 7)
# detect and enhance edges
img_edge = cv2.adaptiveThreshold(img_blur, 255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,
blockSize=9,
C=2)
# convert back to color, bit-AND with color image
img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
mask_img = cv2.inRange(img_edge, (255, 255, 255), (255, 255, 255))
img_cartoon = cv2.bitwise_and(img_color, img_edge, mask = mask_img)
# display
cv2.imshow("cartoon", img_cartoon)
我已经从有效目录中提取了图像,并且 cartoon 窗口没有响应。我想知道为什么会发生错误。我已经从其他StackOverflow帖子中阅读了可能的错误,但无法纠正该错误。谁能帮我找出错误?在此先感谢:)