You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenCV报错(-215):imshow函数提示size.width>0&&size.height>0求助

Fixing OpenCV Error (-215) in imshow()

Hey there, let's get to the bottom of that frustrating (-215) error you're seeing! That message boils down to one key issue: the image you're trying to display is empty—meaning cv2.imread() didn't successfully load your file. Let's walk through the fixes step by step:

Common Causes & Solutions

  • Incorrect File Path
    Your current path C:/Users/Desktop/x.jpg is missing your Windows username! The full path needs to include your user folder, like C:/Users/YourActualUsername/Desktop/x.jpg. You can also format the path in these safe ways to avoid escape character issues:

    • Double backslashes: C:\\Users\\YourActualUsername\\Desktop\\x.jpg
    • Raw string: r'C:\Users\YourActualUsername\Desktop\x.jpg'
  • Missing/Corrupted Image File
    Double-check these details:

    • x.jpg is actually saved on your Desktop
    • The filename is spelled perfectly (watch out for typos like X.JPG or x.jpeg)
    • The image isn't corrupted—try opening it with a regular image viewer to confirm it works.
  • Redundant Code (Optional Cleanup)
    You have two identical cv2.namedWindow() calls in your code. While this doesn't cause the error, you only need one instance to create the display window.

Corrected Code Example

Here's a revised version with a safety check to confirm the image loaded successfully:

import numpy as np
import cv2

# Replace "YourActualUsername" with your real Windows username
img = cv2.imread('C:/Users/YourActualUsername/Desktop/x.jpg', 0)

# Verify image loaded before trying to display it
if img is not None:
    cv2.namedWindow('image', cv2.WINDOW_NORMAL)
    cv2.imshow('image', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
else:
    print("Error: Couldn't load the image! Check your file path or file integrity.")

Adding the if img is not None check helps you catch loading issues early instead of hitting the confusing (-215) error directly.

内容的提问来源于stack exchange,提问作者Mohit Agarwal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:01:05