plot image cv2 code example
Example 1: show image opencv python
import cv2
img = cv2.imread('/path_to_image/opencv-logo.png')
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()
elif k == ord('s'):
cv2.imwrite('messigray.png',img)
cv2.destroyAllWindows()
Example 2: cv show image python
cv2.imshow('image',img)
cv2.waitKey(0)
Example 3: load img cv2
img = cv2.imread('img.jpg')
Example 4: 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-')
for i in range(1000):
line1.set_ydata(np.cos(2 * np.pi * (x1+i*3.14/2) ) * np.exp(-x1) )
fig.canvas.draw()
img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8,
sep='')
img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
cv2.imshow("plot",img)
ret,frame = cap.read()
cv2.imshow("cam",frame)
k = cv2.waitKey(33) & 0xFF
if k == 27:
break