Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Here is what I have now.

The imported libraries:

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
import cv2
import time

For instance(this part of code is from internet example), I draw a plot with a "text" on it.

fig = plt.figure(figsize=(6,6.5))
canvas = FigureCanvas(fig)
ax = fig.gca()
ax.text(0.0,0.0,"Test", fontsize=45)
ax.axis('off')

Then, I save it to, well, "1.png".

plt.savefig("1.png")

Finally, I create a video writer and write the png into the video.

size = (600,650)
fps = 2
fourcc = cv2.VideoWriter_fourcc('I', '4', '2', '0')
video = cv2.VideoWriter("1.avi", fourcc, fps, size)
image = cv2.imread('1.png')
video.write(image)
video.release()

Here is the question: I need to save the picture and then read it as the numpy array that opencv can read, is there any way that I can directly transform the fig object to the image object(numpy array)?

Thank you guys very much!

question from:https://stackoverflow.com/questions/66050702/how-to-generate-a-video-with-python-matplotlib-and-opencv

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
192 views
Welcome To Ask or Share your Answers For Others

1 Answer

Let's analyze step-by-step:

  1. size = (600,650)
    

Does the size of the image is 600, 650?

If I were you, I would initialize as

image = cv2.imread('1.png')
(height, width) = image.shape[:2]
size = (width, height)

or if you want the output as (600, 650), then resize the image:

image = cv2.imread('1.png')
image = cv2.resize(image, (600, 650))
  1. fps = 2
    

Why? you set frame-per-second to 2, why not 10?, 15? source

  1. fourcc = cv2.VideoWriter_fourcc('I', '4', '2', '0')
    

Why I420? You want to create .avi file, then set to MJPG source

If I were you, I would initialize as:

fourcc = cv2.VideoWriter_fourcc(*'MJPG')
  1. video = cv2.VideoWriter("1.avi", fourcc, fps, size)
    

What type of images are you working with? color, gray?. You need to initialize as a parameter in the VideoWriter

For color image:

video = cv2.VideoWriter("1.avi", fourcc, fps, size, isColor=True)

For gray images:

video = cv2.VideoWriter("1.avi", fourcc, fps, size, isColor=False)

An example code:


import cv2

img = cv2.imread("1.png")
(h, w) = img.shape[:2]
size = (w, h)
fps = 20
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
video = cv2.VideoWriter("1.avi", fourcc, fps, size, isColor=True)
video.write(img)
video.release()

Also know that cv2.imread reads the image in BGR fashion. If you want your images as RGB you need to convert it like:

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...