1

I am trying to save a part of an image inside a bounding box as follow:

digit = thresh[x:x+w,y:y+h]
cv2.imwrite(str(c)+'.png',digit)

But I got following error.

error: OpenCV(4.1.2) /io/opencv/modules/imgcodecs/src/loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'

Need your assistance.

4
  • it can means that you try to save empty array. First check what you have in digit. Use print() for this. You could also check x,y,w,h - maybe w and/or h is zero. Commented Jan 3, 2020 at 20:02
  • A have already plotted the digit, it showing me an image of a digit which I have cropped, but when tried to write it, it shows the error in the question. Commented Jan 4, 2020 at 15:38
  • 1
    in two line of code and this error I can say only one: digit is empty. I don't knwo if you have something more between these two lines but maybe you assign new empty array to digit before write. Only you know all your code and only you can use print() directly before imwrite to check what you have in digit Commented Jan 4, 2020 at 16:22
  • BTW: if you run cv2.imwrite('img.png', np.array([])) then you get the same error - because array is empty. If you add at least one value to array - ie. np.array([1]) - then it works without error. Commented Jan 4, 2020 at 16:28

1 Answer 1

1

That 'x'or 'y' is out of bound of our 'thresh' array Most of the time it will be in negative value. Solution would be

x=max(0,x)
y=max(0,y)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.