scaling = 5;
nbins = 11;
shiftX = 100;
directory_name = 'C:\kerning\JNN';
files = dir(directory_name);
fileIndex = find(~[files.isdir]);
for i = 1:length(fileIndex)
fileName{i} = files(fileIndex(i)).name;
end
cd(directory_name);
fileID{1} = fopen('C:\kerning\0_shift.txt','w');
fileID{2} = fopen('C:\kerning\100_shift.txt','w');
fileID{3} = fopen('C:\kerning\200_shift.txt','w');
fileID{4} = fopen('C:\kerning\300_shift.txt','w');
fileID{5} = fopen('C:\kerning\400_shift.txt','w');
fileID{6} = fopen('C:\kerning\500_shift.txt','w');
fileID{7} = fopen('C:\kerning\600_shift.txt','w');
fileID{8} = fopen('C:\kerning\700_shift.txt','w');
fileID{9} = fopen('C:\kerning\800_shift.txt','w');
fileID{10} = fopen('C:\kerning\900_shift.txt','w');
fileID{11} = fopen('C:\kerning\1000_shift.txt','w');
for ind = 1:length(fileIndex)
y = imread(fileName{ind});
% y = imread('li.png');
[L W] = size(y);
m = ones(L,W);
xx = xor(y,m);
y = bwconvhull(xx,'union');
% imshow(y);
% pause;
divisor = fix(shiftX/scaling);
nW = fix(W/divisor);
if(nW > nbins)
nW = nbins;
end
starting = ones(nW,L);
ending = ones(nW,L);
start_h = zeros(1,11);
end_h = L*ones(1,11);
for j = 1:nW
for i = 0:2
starting(j,:) = starting(j,:) & y(:,end - (j-1)*divisor - i)';
ending(j,:) = ending(j,:) & y(:,i+1 + (j-1)*divisor)';
end
end
for j = 1:nW
start_h(j) = find(starting(j,:) == 1, 1, 'last' );
if(isempty(start_h(j)))
start_h(j) = -1;
else
start_h(j) = L - start_h(j);
end
end_h(j) = find(ending(j,:) == 1, 1 );
if(isempty(end_h(j)))
end_h(j) = -1;
else
end_h(j) = L - end_h(j);
end
end
%
diff_start = start_h(1:end-1) - start_h(2:end);
diff_end= end_h(1:end-1) - end_h(2:end);
st = find(diff_start < 0,1);
if(~isempty(st))
start_h(st+1:nW) = start_h(st);
end
en = find(diff_end > 0,1);
if(~isempty(en))
end_h(en+1:nW) = end_h(en);
end
name = fileName{ind}(1:end-4);
for j = 1:11
R{ind}{1} = name;
R{ind}{2}{j} = [fix(scaling*start_h(j)) ; fix(scaling*end_h(j))];
end
for j = 1:11
fprintf(fileID{j},'%s\t',name);
fprintf(fileID{j},'%d\t%d',fix(scaling*start_h(j)),fix(scaling*end_h(j)));
fprintf(fileID{j},'\n');
end
% imshow(y);
% hold on;
% plot(W - (0:nW-1)*divisor,L - start_h(1:nW),'rd');
% plot((0:nW-1)*divisor,L - end_h(1:nW),'gd');
%
% hold off;
%
ind
end
for j = 1:11
fclose(fileID{j});
end