4

I have read every SO thread I could find on this topic, and have read the documentation rather extensively, and even copied and pasted code from these questions/tutorials. But I am still unable to load a jpg, annotate it, and save the figure with matplotlib. I really could use some advice on this topic.

Here is an example of one of my many attempts:

import cv2
import matplotlib.pyplot as plt

image = cv2.imread(...filepath-to-img...)
fig, ax = plt.subplots()
ax.imshow(image)
ax.add_patch(plt.Rectangle(...params...)
plt.savefig(...filepath...)

The image loads correctly, and when I work interactively and run the plt.subplots( and ax.imshow(image) commands, I see a plot with the image pop up, and I get a note saying that it is an AxesImage object.

But when I got to save, it says 'Figure size 432x288 with 0 Axes,' and the resulting image saved to disk is blank.

I've also tried the following for saving to no avail.

my_fig = plt.cgf()
my_fig.savefig(...filepath...)

Basically, it seems that creating a figure and axes, and calling ax.imshow() is not adding the image to my axes, nor are the ax.add_patch() calls doing anything to the axes.

I've also tried it without creating separate axes, as with:

plt.figure()
plt.imshow(image)
my_axes = plt.gca()
my_axes.add_patch(plt.Rectangle(...params...)
plt.savefig(...filepath...)

Again, the resulting figure is blank and has 0 axes.

I know I'm probably missing an obvious step, but I can't figure out what it is, and even copying and pasting code has been no help.

Edit: Adding complete code in response to comment

import cv2
from matplotlib import pyplot as plt

img = './1.png' # 364x364

image = cv2.imread(img)
fig, ax = plt.subplots()
ax.imshow(image)

color = (1, 0, 0, 1)
ax.add_patch(plt.Rectangle((139, 25), 85, 336,
    color = color,
    fill = False,
    linewidth = 2))
plt.savefig('./annotated.png')
7
  • Unless there is an error produced by loading the image or displaying it, the code itself looks fine and the problem is presumably rather related on how you run this code, of which we have no information as it stands. Commented Sep 28, 2018 at 20:28
  • Fair. Will copy full code block. Commented Sep 28, 2018 at 21:12
  • Also, yes, the picture does load properly. I can display it with cv2.imshow(), and as I mentioned in the post, when the ax.imshow(image) line is run, the picture does appear as output. Commented Sep 28, 2018 at 21:33
  • Sorry, maybe that wasn't clear. What I meant by "how you run the code" was that you could tell where you type in that code, what you do to execute it, and which programs and libraries (including their respective versions) are in use. Commented Sep 28, 2018 at 21:36
  • Ahh. I see. Python 3.6.5, OpenCV 3.4.1, matplotlib 2.2.2. I generally work from Atom/Hydrogen with iPython/Jupyter and run code interactively. I get the same behavior when I run line-by-line, or run the entire script at once. The same also happens when I run from within Spyder. Commented Sep 28, 2018 at 21:39

1 Answer 1

5

I also faced the same problem and here is what worked for me.

from matplotlib.patches import Rectangle
fig,ax = plt.subplots(figsize=(15,12))
ax.imshow(frames[0])
x,y,w,h = bboxes[0]
ax.add_patch(Rectangle((x,y),w,h, linewidth=3, edgecolor='r', facecolor='none'))
plt.axis('off')
plt.savefig("out.png",bbox_inches='tight',pad_inches=0)
plt.show()

I was also getting a blank image on disk when plt.show() was written before plt.savefig()

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.