├── README.md
├── cvPLOT_sin.gif
├── cvPLot.py
├── opencv-multiplot.py
├── opencv-plot.py
├── opencv_multiplot.gif
└── opencv_plot.gif
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | # opencv-plot
5 | Plotting real time data in opencv using python
6 |
7 | This repo can be used to plot single as well as multiple values in OpenCV python.
8 | ## opencv-plot.py
9 | Plot single integer values in realtime using this file.
10 |
11 | ### Usage:
12 | ### Create a plotter class object
13 | ```
14 | p = Plotter(400, 200) #(plot_width, plot_height)
15 | ```
16 |
17 | ### Create dummy values using for loop
18 | ```
19 | for v in range(500):
20 | # call 'plot' method for realtime plot
21 | p.plot(v/5)
22 | ```
23 | `
24 |
25 | ## opencv-multiplot.py (Maximum: 9 values at a time)
26 | Plot multiple values in reallime using this file.
27 |
28 | ### Usage:
29 | ### Create a plotter class object
30 | ```
31 | p = Plotter(400, 200, 3) #(plot_width, plot_height)
32 | ```
33 |
34 | ### Create dummy values using for loop
35 | ```
36 | for v in range(500):
37 | # call 'plot' method for realtime plot
38 | v1 = random.randint(0,200) - 100
39 | v2 = int(math.sin(v*3.14/180)*100)
40 | v3 = int(math.cos(v*3.14/180)*100)
41 | p.multiplot([v1, v2, v3]) #List of all values to be plotted
42 | ```
43 | `
44 |
45 |
46 |
47 | ## cvPLOT.py (use different label for each plot)
48 | Plot multiple values in reallime in multiwindows with a simple
49 |
50 | ### Usage:
51 | ### Create a plotter class object
52 | ```
53 | p = Plotter(400, 200, 100) #(plot_width, plot_height, sample_count)
54 | ```
55 |
56 | ### Create dummy values using for loop
57 | ```
58 | p = Plotter(400, 200,sample_buffer=200)
59 |
60 | for v in range(1,3000):
61 |
62 | p.plot(int(math.sin(v*3.14/180)*100),label='sin') ## Add label 'sin' to show in a seperate window
63 |
64 | p.plot(int(math.cos(v*3.14/180)*50),label='cos')
65 | ```
66 | `
67 |
68 |
--------------------------------------------------------------------------------
/cvPLOT_sin.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2vin/opencv-plot/6d5247e31ffd778e1f049019440d943e20d8c039/cvPLOT_sin.gif
--------------------------------------------------------------------------------
/cvPLot.py:
--------------------------------------------------------------------------------
1 | import cv2
2 | import numpy as np
3 | import math
4 | import time
5 | from random import random
6 |
7 | # Plot values in opencv program
8 | class Plotter:
9 | def __init__(self, plot_width, plot_height,sample_buffer=None):
10 | self.width = plot_width
11 | self.height = plot_height
12 | self.color = (255, 0 ,0)
13 | self.plot_canvas = np.ones((self.height, self.width, 3))*255
14 | self.ltime = 0
15 | self.plots = {}
16 | self.plot_t_last = {}
17 | self.margin_l = 10
18 | self.margin_r = 10
19 | self.margin_u = 10
20 | self.margin_d = 50
21 | self.sample_buffer = self.width if sample_buffer is None else sample_buffer
22 |
23 |
24 | # Update new values in plot
25 | def plot(self, val, label = "plot"):
26 | if not label in self.plots:
27 | self.plots[label] = []
28 | self.plot_t_last[label] = 0
29 |
30 | self.plots[label].append(int(val))
31 | while len(self.plots[label]) > self.sample_buffer:
32 | self.plots[label].pop(0)
33 | self.show_plot(label)
34 | # Show plot using opencv imshow
35 | def show_plot(self, label):
36 |
37 | self.plot_canvas = np.zeros((self.height, self.width, 3))*255
38 | cv2.line(self.plot_canvas, (self.margin_l, int((self.height-self.margin_d-self.margin_u)/2)+self.margin_u ), (self.width-self.margin_r, int((self.height-self.margin_d-self.margin_u)/2)+self.margin_u), (0,0,255), 1)
39 |
40 | # Scaling the graph in y within buffer
41 | scale_h_max = max(self.plots[label])
42 | scale_h_min = min(self.plots[label])
43 | scale_h_min = -scale_h_min if scale_h_min<0 else scale_h_min
44 | scale_h = scale_h_max if scale_h_max > scale_h_min else scale_h_min
45 | scale_h = ((self.height-self.margin_d-self.margin_u)/2)/scale_h if not scale_h == 0 else 0
46 |
47 |
48 | for j,i in enumerate(np.linspace(0,self.sample_buffer-2,self.width-self.margin_l-self.margin_r)):
49 | i = int(i)
50 | cv2.line(self.plot_canvas, (j+self.margin_l, int((self.height-self.margin_d-self.margin_u)/2 +self.margin_u- self.plots[label][i]*scale_h)), (j+self.margin_l, int((self.height-self.margin_d-self.margin_u)/2 +self.margin_u- self.plots[label][i+1]*scale_h)), self.color, 1)
51 |
52 |
53 | cv2.rectangle(self.plot_canvas, (self.margin_l,self.margin_u), (self.width-self.margin_r,self.height-self.margin_d), (255,255,255), 1)
54 | cv2.putText(self.plot_canvas,f" {label} : {self.plots[label][-1]} , dt : {int((time.time() - self.plot_t_last[label])*1000)}ms",(int(0),self.height-20),cv2.FONT_HERSHEY_SIMPLEX,0.6,(0,255,255),2)
55 | cv2.circle(self.plot_canvas, (self.width-self.margin_r, int(self.margin_u + (self.height-self.margin_d-self.margin_u)/2 - self.plots[label][-1]*scale_h)), 2, (0,200,200), -1)
56 |
57 | self.plot_t_last[label] = time.time()
58 | cv2.imshow(label, self.plot_canvas)
59 | cv2.waitKey(1)
60 |
61 |
62 | def single_sample():
63 | # Create dummy values using for loop
64 | p = Plotter(400, 200,sample_buffer=200)
65 |
66 | for v in range(1,3000):
67 |
68 | p.plot(int(math.sin(v*3.14/180)*100),label='sin')
69 |
70 | p.plot(int(math.cos(v*3.14/180)*50),label='cos')
71 |
72 |
73 |
74 | if '__main__' == __name__:
75 | single_sample()
--------------------------------------------------------------------------------
/opencv-multiplot.py:
--------------------------------------------------------------------------------
1 | ################################################ ##
2 | ### Real time data plotter in opencv (python) ###
3 | ## Plot multiple data for debugging and analysis ##
4 | ## Contributors - Vinay @ www.connect.vin ##
5 | ## For more details, check www.github.com/2vin ##
6 | ###################################################
7 |
8 | import cv2
9 | import numpy as np
10 | import random
11 | import math #for sine and cosine functions
12 |
13 | # Plot values in opencv program
14 | class Plotter:
15 | def __init__(self, plot_width, plot_height, num_plot_values):
16 | self.width = plot_width
17 | self.height = plot_height
18 | self.color_list = [(255, 0 ,0), (0, 250 ,0),(0, 0 ,250),
19 | (0, 255 ,250),(250, 0 ,250),(250, 250 ,0),
20 | (200, 100 ,200),(100, 200 ,200),(200, 200 ,100)]
21 | self.color = []
22 | self.val = []
23 | self.plot = np.ones((self.height, self.width, 3))*255
24 |
25 | for i in range(num_plot_values):
26 | self.color.append(self.color_list[i])
27 |
28 | # Update new values in plot
29 | def multiplot(self, val, label = "plot"):
30 | self.val.append(val)
31 | while len(self.val) > self.width:
32 | self.val.pop(0)
33 |
34 | self.show_plot(label)
35 |
36 | # Show plot using opencv imshow
37 | def show_plot(self, label):
38 | self.plot = np.ones((self.height, self.width, 3))*255
39 | cv2.line(self.plot, (0, int(self.height/2) ), (self.width, int(self.height/2)), (0,255,0), 1)
40 | for i in range(len(self.val)-1):
41 | for j in range(len(self.val[0])):
42 | cv2.line(self.plot, (i, int(self.height/2) - self.val[i][j]), (i+1, int(self.height/2) - self.val[i+1][j]), self.color[j], 1)
43 |
44 | cv2.imshow(label, self.plot)
45 | cv2.waitKey(10)
46 |
47 |
48 | ## Test on sample data
49 |
50 | # Create a plotter class object
51 | p = Plotter(400, 200, 3)
52 |
53 | # Create dummy values using for loop
54 | for v in range(500):
55 | # call 'plot' method for realtime plot
56 | v1 = random.randint(0,200) - 100
57 | v2 = int(math.sin(v*3.14/180)*100)
58 | v3 = int(math.cos(v*3.14/180)*100)
59 | p.multiplot([v1, v2, v3])
60 |
--------------------------------------------------------------------------------
/opencv-plot.py:
--------------------------------------------------------------------------------
1 | ##################################################
2 | ### Real time data plotter in opencv (python) ###
3 | ## Plot integer data for debugging and analysis ##
4 | ## Contributors - Vinay @ www.connect.vin ##
5 | ## For more details, check www.github.com/2vin ##
6 | ##################################################
7 |
8 | import cv2
9 | import numpy as np
10 |
11 | # Plot values in opencv program
12 | class Plotter:
13 | def __init__(self, plot_width, plot_height):
14 | self.width = plot_width
15 | self.height = plot_height
16 | self.color = (255, 0 ,0)
17 | self.val = []
18 | self.plot_canvas = np.ones((self.height, self.width, 3))*255
19 |
20 | # Update new values in plot
21 | def plot(self, val, label = "plot"):
22 | self.val.append(int(val))
23 | while len(self.val) > self.width:
24 | self.val.pop(0)
25 |
26 | self.show_plot(label)
27 |
28 | # Show plot using opencv imshow
29 | def show_plot(self, label):
30 | self.plot_canvas = np.ones((self.height, self.width, 3))*255
31 | cv2.line(self.plot_canvas, (0, int(self.height/2) ), (self.width, int(self.height/2)), (0,255,0), 1)
32 | for i in range(len(self.val)-1):
33 | cv2.line(self.plot_canvas, (i, int(self.height/2) - self.val[i]), (i+1, int(self.height/2) - self.val[i+1]), self.color, 1)
34 |
35 | cv2.imshow(label, self.plot_canvas)
36 | cv2.waitKey(10)
37 |
38 |
39 | ## Test on sample data
40 |
41 | # Create a plotter class object
42 | p = Plotter(400, 200)
43 |
44 | # Create dummy values using for loop
45 | for v in range(500):
46 | # call 'plot' method for realtime plot
47 | p.plot(v/5)
48 |
--------------------------------------------------------------------------------
/opencv_multiplot.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2vin/opencv-plot/6d5247e31ffd778e1f049019440d943e20d8c039/opencv_multiplot.gif
--------------------------------------------------------------------------------
/opencv_plot.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2vin/opencv-plot/6d5247e31ffd778e1f049019440d943e20d8c039/opencv_plot.gif
--------------------------------------------------------------------------------