The function is badly written. There are unused variables lying around, and the 2 for loops could be removed in favor of the usual Matlab tricks for computational efficiency.
The results are not that visually satisfying either - the color temperature is definitely off on some of the images I tried this function on.
Thanks this was useful. This script is slow only because of the first double for-loop doing a dynamic memory allocation each time the condition is met. MATLAB is terrible with dynamic memory allocations. Simply add:
Ciny = zeros(1,x*y);
right before the outer for loop (for i=1:x), and then add:
Ciny(cnt:end) = ;
right at the end of the for loop (right above cnt=cnt-1; and then the sort) to get rid of the unused memory. This speeds up the code tremendously.