
408 A Computational Introduction to Digital Image Processing, Second Edition
Implementing Run Length Coding
We can experiment with run length encoding by writing a simple function to implement
it. We shall assume our data is binary, and in one single column. The first step is to find the
places at which the data changes values, starting with the value 1, indicating that the first
value is indeed different from the previous (non-existent!) value. We also need to append a
1 at the end, so that we can find the length of the final run. This can be done very easily
by comparing the data with a shifted version of itself:
MATLAB/Octave
>> changes = [data ~= circshift(data,1);1]; ...