function [out bin bin2] = generate_skinmap(filename)
if nargin > 1 | nargin < 1
error('usage: generate_skinmap(filename)');
end;
img_orig = imread(filename);
height = size(img_orig,1);
width = size(img_orig,2);
out = img_orig;
bin = zeros(height,width);
img = grayworld(img_orig);
img_ycbcr = rgb2ycbcr(img);
Cb = img_ycbcr(:,:,2);
Cr = img_ycbcr(:,:,3);
[r,c,v] = find(Cb>=79 & Cb<=127 & Cr>=145 & Cr<=180);
%[r,c,v] = find(Cb<77 & Cb>127 & Cr<133 & Cr>173);
numind = size(r,1);
for i=1:numind
out(r(i),c(i),:) = [0 0 255];
bin(r(i),c(i)) = 1;
end
SE = strel('square',4);
bin1 =imdilate(bin,SE);
bin2 = medfilt2(bin1);
% LEN = 21;
% THETA = 11;
% PSF = fspecial('motion', LEN, THETA);
% noise_var=0.00001;
% estimated_nsr = noise_var / var(bin(:));
% bin3 = deconvwnr(bin2, PSF, estimated_nsr);
bin3=bin2;
bin3 =bwareaopen(bin2,600);
imwrite(bin3,'A-template.jpg');
% bin4=edge(bin3,'canny');
% SE = strel('square',3);
% bin5 = imdilate(bin4,SE);
% ------------ croping ---
[y,x,z]=size(bin3);
myI=double(bin3);
tic
Blue_y=zeros(y,1);
for i=1:y
for j=1:x
if(myI(i,j,1)==1)
Blue_y(i,1)= Blue_y(i,1)+1;
end
end
end
[temp MaxY]=max(Blue_y);
PY1=MaxY;
while ((Blue_y(PY1,1)>=5)&&(PY1>1))
PY1=PY1-1;
end
PY2=MaxY;
while ((Blue_y(PY2,1)>=5)&&(PY2<y))
PY2=PY2+1;
end
IY=bin(PY1:PY2,:,:);
Blue_x=zeros(1,x);
for j=1:x
for i=PY1:PY2
if(myI(i,j,1)==1)
Blue_x(1,j)= Blue_x(1,j)+1;
end
end
end
PX1=1;
while ((Blue_x(1,PX1)<3)&&(PX1<x))
PX1=PX1+1;
end
PX2=x;
while ((Blue_x(1,PX2)<3)&&(PX2>PX1))
PX2=PX2-1;
end
PY1=PY1-2;
PX1=PX1-2;
PX2=PX2+3;
PY2=PY2+10;
dw=img(PY1:PY2-8,PX1:PX2,:);
toc
figure('name','Daerah Calon Plat'),subplot(1,2,1),imshow(IY),title('Daerah Calon Plat');
figure('name','Croping Plat'),%subplot(1,2,2),
imshow(dw),title('Hasil Croping Plat dan Simpan hasilnya')
imwrite(dw,'dw.jpg');
figure; imshow(img_orig); title ('gambar asal')
%figure; imshow(img_ycbcr);
figure; imshow(out); title ('proses ycbcr')
figure; imshow(bin); title ('morphologi dilation')
figure; imshow(bin2);title ('median filter')
figure; imshow(bin3);title ('bw area open')
% figure; imshow(bin4); title ('canny')
% figure; imshow(bin5);
% % % Load image
% % load wbarb;
% % % Define wavelet of your choice
% wavelet = 'haar';
% % % Define wavelet decomposition level
% level = 2;
% % % Compute multilevel 2D wavelet decomposition
% [C S] = wavedec2(bin4,level,wavelet);
% % % Define colormap and set rescale value
% map = [r/255 c/255 v/255]
% colormap(map); rv = length(map);
% % % Plot wavelet decomposition using square mode
% plotwavelet2(C,S,level,wavelet,rv,'square');
% title(['Decomposition at level ',num2str(level)]);
% figure; imshow(bin4);
end