Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

convert matplotlib figure to cv2 image

import matplotlib
matplotlib.use('TkAgg')

import numpy as np
import cv2
import matplotlib.pyplot as plt

fig = plt.figure()
cap = cv2.VideoCapture(0)


x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)

y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)


line1, = plt.plot(x1, y1, 'ko-')        # so that we can update data later

for i in range(1000):
    # update data
    line1.set_ydata(np.cos(2 * np.pi * (x1+i*3.14/2) ) * np.exp(-x1) )

    # redraw the canvas
    fig.canvas.draw()

    # convert canvas to image
    img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8,
            sep='')
    img  = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))

    # img is rgb, convert to opencv's default bgr
    img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)


    # display image with opencv or any operation you like
    cv2.imshow("plot",img)

    # display camera feed
    ret,frame = cap.read()
    cv2.imshow("cam",frame)

    k = cv2.waitKey(33) & 0xFF
    if k == 27:
        break
Comment

PREVIOUS NEXT
Code Example
Python :: menubar pyqt 
Python :: pandas convert column to datetime 
Python :: python default dic 
Python :: merge dataframe pandas 
Python :: python except print error type 
Python :: pandas df to mongodb 
Python :: dataframe time index convert tz naive to tz aware 
Python :: split a string with 2 char each in python 
Python :: how to convert timestamp to date in python 
Python :: or in django query 
Python :: dataclass default list 
Python :: python remove all elemnts in list containing string 
Python :: create requirement .txt 
Python :: python file directory 
Python :: finding factorial of a number in python 
Python :: python bit shift by 3 
Python :: dt.weekday_name 
Python :: groupby and sort python 
Python :: python sort array of dictionary by value 
Python :: access first element of dictionary python 
Python :: for loop with enumerate python 
Python :: train test split sklearn 
Python :: how to run terminal commands in python 
Python :: python count variable and put the count in a column of data frame 
Python :: shape pandas 
Python :: convert np shape (a,) to (a,1) 
Python :: group by in ruby mongoid 
Python :: python turtle triangle 
Python :: check if element in list python 
Python :: sqlalchemy filter between dates 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =