IMAGE PROCESSING

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Run Length Encoding

Consider a matrix A with 15 elements,
A= [10 10 9 9 9 9 4 0 0 0 0 0 10 10 10]
In the given example,
10 has occurred 2 times, 9 has occurred 4 times, 4 has occurred once, 0 has occurred 5 times and 10 has occurred 3 times.
After Run length encoding, we obtain the matrix without any repetition in the adjacent elements, [10     9     4     0    10].  And the occurrences of each element [2     4     1     5     3]
Thus the matrix is reduced to 10 elements from 15 elements.
Let’s see how to code this reduction method.
Consider the above matrix A,
1.     Find the difference between adjacent elements. Use the function ‘diff(A)’ to find the difference.
[0    -1     0     0     0    -5    -4     0     0     0     0    10     0     0]
2.     Convert it to logical format.  The elements without repetition are denoted with one and the repeated elements with zero.
[0         0     0     0     1     1     0     0     0     0     1     0     0     1]
3.     Find the position of the elements that has the value one in the above step.
[2     6     7    12    15].
4.     Find the unique element values using the positions obtained from the above step. In the matrix A, the element at the position 2 is 10, the element at the position 6 is 9, the element at the position 7 is 4, the element at the position 12 is 0 and the element at the position 15 is 10.
[10     9     4     0    10]
5.     The first element in the matrix is 10, it has occurred 2 times. We obtained the occurrence of the first element alone from the matrix in the step 3. For the remaining elements, find the difference of the matrix in the step 3.
i.e. diff([2     6     7    12    15]); The result after concatenating the first element of the matrix obtained in step 3 with difference for the matrix in the step 3 is [2     4     1     5     3]
6.     Thus in the step 4 we obtain the elements without repetition,
[10     9     4     0    10] and the occurrences in step 5, [2     4     1     5     3].

MATLAB CODE:
A=[5 2 2 2 3 3 3 3 4 4 1 1 1 1 1 1 1 6 6 4 4]
F=[logical(diff(A)) 1];
In=find(F~=0);

Ele=A(In);

C=[In(1) diff(In)];

Result=zeros([numel(Ele) 2]);
Result(:,1)=Ele;
Result(:,2)=C;
display(Result);

SAMPLE OUTPUT:
Result =

5     1
2     3
3     4
4     2
1     7
6     2
4     2
EXPLANATION:
5  has occurred 1 times, 2 has occurred 3 times, 3 has occurred 4 times, 4 has occurred 2 times, 1 has occurred 7 times, 6 has occurred 2 times and 4 has occurred 2 times.
Like "IMAGE PROCESSING" page

ccie security bootcamp said...

Interesting post. Thank you

asmita haveliya said...

hey its a nice code...can u tell me what are the applications of the Run Length Encoding....

can this coding be applied to images as well...

Steve said...

nice blog checkout my latest blog at

http://www.togetherfornature.blogspot.com