| Matlab: |
function Y=mcut (I,level );
% Median Cut renk kuantalayici
% I, RGB renkli bir goruntu
% level, goruntuyu ifade etmek istediginiz renk sayisi
% Y, kuantalanmis uint8 goruntu
%
% Yazan : Seyhan AGAOGLU
% seyhan_eng@yahoo.com
% SAKARYA UNIVERISTESI
I = double (I );
R = I (:,:, 1);
G = I (:,:, 2);
B = I (:,:, 3);
m = size (I, 1);
n = size (I, 2);
num = 1;
for i= 1:m
for j= 1:n
DR (num )=R (i,j );
DG (num )=G (i,j );
DB (num )=B (i,j );
num = num + 1;
end
end
for i= 1:m*n
indice (i )=i;
end
data = [DR; DG; DB; indice ];
divider = size (indice, 2);
step = log2 (level );
for i= 1:step
[data,divider,color ]=stepmcut (data,divider );
end
Data = sortrows (data ',4);
data = Data';
x= 1;
y= 1;
for i= 1:size (indice, 2)
f = data (5,i );
Y (x,y, 1) = color (f, 1);
Y (x,y, 2) = color (f, 2);
Y (x,y, 3) = color (f, 3);
y = y + 1;
if y == n+ 1
x = x + 1;
y = 1;
end
end
% end of function
|
| Matlab: |
function [sdata,sdivider,color ]=stepmcut (Data,divider );
% Median Cut renk kuantalayici
%
% Yazan : Seyhan AGAOGLU
% seyhan_eng@yahoo.com
% SAKARYA UNIVERISTESI
data (1: 4,: )=Data (1: 4,: );
sub_number = size (divider, 2);
a = 1;
start = 1;
old_start = 1;
to_end = divider (1);
old_end = divider (1);
new_data = data;
for indice= 1:sub_number
s_count = 0;
for i=start:to_end
Temp (:,s_count+ 1) = data (:,i );
s_count = s_count + 1;
end
maxR = max (Temp (1,: ));
minR = min (Temp (1,: ));
maxG = max (Temp (2,: ));
minG = min (Temp (2,: ));
maxB = max (Temp (3,: ));
minB = min (Temp (3,: ));
l (1) = maxR - minR;
l (2) = maxG - minG;
l (3) = maxB - minB;
f = find (l (: )==max (l ));
S = sortrows (Temp ',f);
S = S';
lenght = size (S, 2);
l1 = round (lenght/ 2);
l2 = lenght - l1;
new_divider (a )=l1;
if l2~= 0
new_divider (a+ 1)=l2;
a = a + 2;
else
a = a + 1;
end
i_count = 0;
for i=start:to_end
new_data (:,i ) = S (:,i_count+ 1);
i_count = i_count + 1;
end
if indice~=sub_number
start = old_start + s_count;
old_start = start;
to_end = old_end + divider (indice+ 1);
old_end = to_end;
end
Temp = [0; 0; 0; 0];
end
sdivider = new_divider;
sdata = new_data;
new_data = double (new_data );
old_j = 0;
sum = double ([0; 0; 0; 0]);
for i= 1:size (sdivider, 2)
c_count = sdivider (i );
for j= 1:c_count
sum = sum + new_data (:,j+old_j );
sdata (5,j+old_j ) = i;
end
color (1: 3,i ) = sum (1: 3,: )/c_count;
old_j = old_j + c_count;
sum = double ([0; 0; 0; 0]);
end
color = uint8 (round (color ))';
% end of function |
|