cv2.error OpenCV(4.7.0) .cpp182 error (-215Assertion failed) !_src.empty() in function 'cvcvtColor'
时间: 2023-10-10 11:08:58 浏览: 174
opencv_python-4.10.0.84-cp37-abi3.manylinux2014_x86_64.whl
5星 · 资源好评率100%
This error occurs when the input image passed to the cv2.cvtColor() function is empty or null. This function is used to convert the color space of an image from one color space to another.
To solve this error, you should check if the input image is loaded correctly and not empty. You can use the cv2.imread() function to read the image from the specified file path. If the image is not loaded properly, then the function will return a None value.
Here is an example code to read an image and convert it to grayscale:
```
import cv2
# Load image
img = cv2.imread('image.jpg')
# Check if image is loaded properly
if img is None:
print("Error: Could not read image")
else:
# Convert image to grayscale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Display images
cv2.imshow('Original Image', img)
cv2.imshow('Grayscale Image', gray_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
Make sure to replace 'image.jpg' with the correct file path of your image.
阅读全文