Categories
code graphics opencv vision

Run length encoding in OpenCV [w/code]

Code snippet for RLE calculation on OpenCV Mats

RLE exampleSharing a simple code snippet for run-length encoding with OpenCV…

The code is quite self explanatory, and adapted from the excellent Rosetta Code project.

The console output for the image at the top is:

0: 235 0,
1: 235 0,
2: 235 0,
3: 235 0,
4: 235 0,
5: 235 0,
6: 235 0,
7: 235 0,
8: 235 0,
9: 235 0,
10: 235 0,
11: 235 0,
12: 235 0,
13: 235 0,
14: 235 0,
15: 235 0,
16: 235 0,
17: 235 0,
18: 235 0,
19: 235 0,
20: 235 0,
21: 235 0,
22: 235 0,
23: 235 0,
24: 137 0, 8 255, 90 0,
25: 137 0, 19 255, 79 0,
26: 136 0, 26 255, 73 0,
27: 136 0, 27 255, 72 0,
28: 133 0, 31 255, 71 0,
29: 132 0, 17 255, 7 0, 8 255, 71 0,
30: 132 0, 16 255, 9 0, 8 255, 70 0,
31: 124 0, 2 255, 4 0, 15 255, 9 0, 2 255, 3 0, 6 255, 3 0, 5 255, 6 0, 10 255, 46 0,
32: 124 0, 2 255, 4 0, 14 255, 10 0, 2 255, 4 0, 6 255, 2 0, 5 255, 4 0, 13 255, 45 0,
33: 123 0, 3 255, 3 0, 14 255, 11 0, 2 255, 5 0, 5 255, 2 0, 5 255, 3 0, 16 255, 43 0,
34: 122 0, 5 255, 2 0, 13 255, 11 0, 3 255, 7 0, 4 255, 2 0, 4 255, 3 0, 17 255, 42 0,
35: 122 0, 19 255, 11 0, 5 255, 6 0, 4 255, 1 0, 5 255, 3 0, 17 255, 42 0,
36: 122 0, 20 255, 10 0, 5 255, 7 0, 7 255, 5 0, 18 255, 41 0,
37: 122 0, 20 255, 9 0, 6 255, 7 0, 7 255, 5 0, 18 255, 41 0,
38: 123 0, 19 255, 6 0, 9 255, 10 0, 4 255, 5 0, 19 255, 40 0,
39: 123 0, 13 255, 2 0, 4 255, 5 0, 10 255, 19 0, 19 255, 40 0,
40: 123 0, 13 255, 2 0, 3 255, 5 0, 12 255, 18 0, 19 255, 40 0,
41: 123 0, 13 255, 2 0, 3 255, 3 0, 15 255, 17 0, 19 255, 40 0,
42: 123 0, 11 255, 3 0, 4 255, 2 0, 16 255, 17 0, 20 255, 39 0,
43: 122 0, 12 255, 3 0, 3 255, 3 0, 17 255, 16 0, 20 255, 39 0,
44: 120 0, 14 255, 4 0, 2 255, 2 0, 18 255, 17 0, 12 255, 3 0, 4 255, 39 0,
45: 120 0, 13 255, 5 0, 2 255, 2 0, 8 255, 2 0, 8 255, 8 0, 4 255, 6 0, 10 255, 4 0, 5 255, 38 0,
46: 119 0, 12 255, 9 0, 9 255, 2 0, 4 255, 2 0, 3 255, 8 0, 4 255, 6 0, 10 255, 4 0, 5 255, 38 0,
47: 109 0, 1 255, 9 0, 12 255, 8 0, 10 255, 2 0, 4 255, 13 0, 3 255, 7 0, 8 255, 6 0, 5 255, 38 0,
48: 105 0, 9 255, 1 0, 16 255, 8 0, 10 255, 2 0, 5 255, 12 0, 3 255, 19 0, 6 255, 39 0,
49: 102 0, 29 255, 8 0, 10 255, 2 0, 5 255, 12 0, 3 255, 19 0, 5 255, 40 0,
50: 97 0, 2 255, 1 0, 31 255, 8 0, 10 255, 2 0, 5 255, 10 0, 5 255, 18 0, 6 255, 40 0,
51: 96 0, 37 255, 5 0, 9 255, 5 0, 4 255, 11 0, 4 255, 18 0, 6 255, 40 0,
52: 94 0, 53 255, 5 0, 4 255, 33 0, 5 255, 41 0,
53: 93 0, 13 255, 3 0, 3 255, 2 0, 34 255, 4 0, 5 255, 32 0, 4 255, 42 0,
54: 92 0, 11 255, 7 0, 2 255, 1 0, 15 255, 5 0, 18 255, 2 0, 4 255, 31 0, 5 255, 42 0,
55: 90 0, 12 255, 11 0, 15 255, 5 0, 24 255, 30 0, 5 255, 43 0,
56: 89 0, 9 255, 1 0, 1 255, 12 0, 16 255, 10 0, 23 255, 26 0, 5 255, 43 0,
57: 86 0, 12 255, 14 0, 16 255, 10 0, 28 255, 20 0, 6 255, 43 0,
58: 86 0, 10 255, 14 0, 18 255, 10 0, 10 255, 4 0, 21 255, 11 0, 8 255, 43 0,
59: 86 0, 9 255, 15 0, 18 255, 10 0, 9 255, 6 0, 24 255, 6 0, 7 255, 45 0,
60: 86 0, 9 255, 14 0, 19 255, 10 0, 8 255, 8 0, 4 255, 1 0, 30 255, 46 0,
61: 86 0, 9 255, 12 0, 15 255, 1 0, 5 255, 10 0, 10 255, 6 0, 4 255, 5 0, 26 255, 46 0,
62: 86 0, 9 255, 11 0, 15 255, 18 0, 8 255, 7 0, 4 255, 5 0, 23 255, 49 0,
63: 86 0, 9 255, 11 0, 14 255, 19 0, 8 255, 7 0, 3 255, 6 0, 4 255, 1 0, 5 255, 4 0, 7 255, 51 0,
64: 87 0, 8 255, 10 0, 15 255, 19 0, 10 255, 3 0, 5 255, 4 0, 5 255, 3 0, 3 255, 63 0,
65: 88 0, 9 255, 5 0, 16 255, 22 0, 10 255, 2 0, 5 255, 3 0, 6 255, 3 0, 3 255, 63 0,
66: 88 0, 10 255, 1 0, 18 255, 23 0, 10 255, 2 0, 4 255, 4 0, 6 255, 3 0, 3 255, 63 0,
67: 91 0, 25 255, 24 0, 26 255, 2 0, 4 255, 63 0,
68: 92 0, 22 255, 27 0, 24 255, 70 0,
69: 94 0, 20 255, 28 0, 22 255, 71 0,
70: 99 0, 8 255, 3 0, 4 255, 28 0, 21 255, 72 0,
71: 143 0, 19 255, 73 0,
72: 144 0, 18 255, 73 0,
73: 145 0, 15 255, 75 0,
74: 148 0, 11 255, 76 0,
75: 154 0, 4 255, 77 0,
76: 235 0,
77: 235 0,
78: 235 0,
79: 235 0,
80: 235 0,
81: 235 0,
82: 235 0,
83: 235 0,
84: 235 0,
85: 235 0,
86: 235 0,
87: 235 0,
88: 235 0,
89: 235 0,
90: 235 0,
91: 235 0,
92: 235 0,
93: 235 0,
94: 235 0,
95: 235 0,
96: 235 0,
97: 235 0,
98: 235 0,
99: 235 0,
100: 235 0,
101: 235 0,
102: 235 0,
103: 235 0,
104: 235 0,
105: 235 0,
106: 235 0,
107: 235 0,
108: 235 0,
109: 235 0,
110: 235 0,
111: 235 0,
112: 235 0,
113: 235 0,
114: 235 0,
115: 235 0,
116: 235 0,
117: 235 0,
118: 235 0,
119: 235 0,

Leave a Reply

Your email address will not be published. Required fields are marked *