%kadd.m
clc
clear all
% img = imread('testimgdebugleft_small1.png');
im(:,:,1) = importdata('img1.txt');
im(:,:,2) = importdata('img2.txt');
im(:,:,3) = importdata('img0.txt');
im = im(1:12,1:12,:);
[m n d] = size(im);
cl_Q = 10;
n_bufs = 61;
m_nPixels = m*n;
m_height = m;
m_width = n;
k_ind = zeros(m,n);
exp2 = zeros(12,1);
exp2(0+1) = 1;
for i=1:32
exp2(i+1) = exp2(i+1-1)*2;
end
lg2Q = 0;
while(exp2(lg2Q+1) < cl_Q)
lg2Q = lg2Q + 1;
end
exp2
cl_Q = exp2(lg2Q+1)
H = zeros(cl_Q,256);
Cnt = zeros(cl_Q,1);
Thr = zeros(cl_Q,1);
for l = 0:lg2Q-1
ch = mod(l+1,3);
if (ch == 0)
ch = 3;
end
imch = im(:,:,ch);
H = zeros(cl_Q,256);
Cnt = zeros(cl_Q,1);
Thr = zeros(cl_Q,1);
lB = exp2(l+1);
for i = 1:m
for j = 1:n
q = k_ind(i,j);
H(q+1, imch(i,j)+1) = H(q+1, imch(i,j)+1) + 1;
Cnt(q+1) = Cnt(q+1) + 1;
end
end
for ll = 0:lB-1
Cnt(ll+1) = floor(Cnt(ll+1) / 2);
sumv = 0;
md = 0;
while(Cnt(ll+1) > sumv)
sumv = sumv + H(ll+1, md + 1);
md = md + 1;
end
if(md > 0)
md = md - 1;
end
Thr(ll+1) = md;
Cnt(ll+1) = floor(H(ll+1, md + 1) / 2);
end
for i = 1:m
for j = 1:n
q = k_ind(i,j);
if(imch(i,j) == Thr(q + 1))
if(Cnt(q+1) == 0 )
k_ind(i,j) = k_ind(i,j) + lB;
else
Cnt(q+1) = Cnt(q+1) - 1;
end
elseif(imch(i,j) > Thr(q+1))
k_ind(i,j) = k_ind(i,j) + lB;
end
end
end
end
% kind_vs = importdata('kind1.txt');
% diff = kind_vs - k_ind;
% diff1 = sum(diff(:))