[Python introductory tutorial] CV2 error: cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\s

        As a powerful computer vision library, OpenCV is widely used in various fields. Today I will share the error messages and solutions I encountered during my programming.

1 Error message

[ WARN:[email protected]] global grfmt_tiff.cpp:716 cv::TiffDecoder::readData OpenCV TIFF: TIFFRGBAImageOK: Sorry, can not handle images with 64-bit samples

cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

        This contains a warning and an error message.

        Warning message: It is related to the problem encountered by the OpenCV library when processing TIFF images. Judging from the warning message, your TIFF image may use 64-bit samples, and OpenCV's TiffDecoder cannot handle this situation

        Error message:OpenCV throws when trying to perform color space conversion on an empty image (or the path is invalid and cannot be read as an image). cv2.cvtColorThe function is used to change the color space of the image, such as converting from RGB to grayscale image or HSV, etc. The error message indicates that you attempted to perform a color space conversion on an empty image (_src.empty() is true). In other words, the image file path you gave may be wrong, or the file does not exist, or although the file exists, it cannot be read correctly.

2 solutions

2.1 Resolve warning messages

        Use the cv2.normalize, cv2.convertScaleAbs function or the GDAL library to convert tif to 8 bits. (I didn’t succeed with the two functions of cv2. I succeeded with the GDAL library and will share it with you in the next article)

image_scaled = cv2.convertScaleAbs(image, alpha=0, beta=255) 
image_normalized = cv2.normalize(image, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)

2.2 Solve error messages

  1. Check that your image file path is correct. Make sure the path is correct and the corresponding file actually exists. (Don’t use Chinese paths)
  2. Make sure your image file can be read correctly by OpenCV. Some image files may be corrupted or in a format not supported by OpenCV.
  3. Before trying to read the image, make sure the file path is correct and the file exists. You can check this using the os.path.exists() function of the os module.
  4. At the same time, it is also possible that your picture/tif is a 64-bit picture, causing the cv2 library to be unable to understand and read the image information normally. For solutions, please refer to 2.1 or the next article.

Guess you like

Origin blog.csdn.net/m0_56729804/article/details/133899054