Python OpenCV Save Image error: (-215:Assertion failed) !_img.empty()

Detailed description of the error

When using the imwrite function of OpenCV to save pictures, report CV2. Error: opencv (4.2.0) C:: <projects/opencv Python/opencv/modules/imgcodecs/SRC/loadsave cpp:715 : error: (-215:Assertion failed) !_ img.empty() in function ‘cv::imwrite’

Error analysis

Use clip to segment the image. The code is as follows

clip_img = img[top:(top+height),left:(left+width),:]

terms of settlement

img is not none , through clip_ IMG is none can’t be judged. It can be judged by size attribute. The code is as follows

if clip_img.size ==  0:#Do not use imwriter for this case

In addition to not processing, there is another solution, check whether the values of the divided rows and columns are legal , legal conditions:

The minimum value of segmentation cannot be less than 0, the maximum value of segmentation cannot be greater than the width and height of the picture, and the maximum value of segmentation must be greater than the minimum value of segmentation

Similar Posts: