function [parent]=imdirectlab(varargin)
%ask for image, output BIL [RGB, Decorrelated RGB, LAB, decorrelated
%LAB] output: [bil], inputs: ()
titlestr = sprintf('Choose RGB Image to Load');
file=uigetfile({'*.jpg;*.bmp;*.png'},titlestr);
img=imread(file);
% img=[img(:,50:300,:) img(:,950:1270,:)];
dimg=decorrstretch(img);
cform=makecform('srgb2lab','AdaptedWhitePoint',whitepoint('D50'));
cform2=makecform('lab2srgb');
lab=applycform(img,cform);
dlab=decorrstretch(lab);


Thre=0.1;

sobel_filt=edge(imadjust(rgb2gray(img),stretchlim(rgb2gray(img)),[]),'sobel','nothinning');

figure
imshow(sobel_filt);
title('Sobel Filter');

figure
subplot(4,1,1);
imshow(img); 
title('Original RGB');

subplot(4,1,2); 
imshow(dimg); 
title('Decorrelated RGB');

dlab2=dlab;

subplot(4,1,3); 
rlab=lab;
rlab(:,:,1)=ones(size(lab,1),size(lab,2)).*180;
imshow(applycform(rlab,cform2));
title('Recombinant a*b*');
axis off

subplot(4,1,4); 
rdlab=dlab;
rdlab(:,:,1)=ones(size(lab,1),size(lab,2)).*180;
imshow(applycform(rdlab,cform2));
title('Recombinant a*b* (Decorr. RGB)');
axis off

figure
subplot(1,3,1);
junk=lab;
imshow(lab(:,:,1)) 
title('L* Component');
axis off

subplot(1,3,2); 
junk(:,:,1)=ones(size(lab,1),size(lab,2)).*180;
junk(:,:,3)=ones(size(lab,1),size(lab,2)).*130;
imshow(applycform(junk,cform2)); 
title('a* Component');
axis off

subplot(1,3,3); 
junk=lab;
junk(:,:,1)=ones(size(lab,1),size(lab,2)).*180;
junk(:,:,2)=ones(size(lab,1),size(lab,2)).*125;
imshow(applycform(junk,cform2)); 
title('b* Component');
axis off

parent.data.image=img;
parent.data.dimage=dimg;
parent.data.lab=lab;
parent.data.dlab=dlab;

img=double(img);
dimg=double(dimg);
lab=double(lab);
dlab=double(dlab);

parent.score=reshape(lab(:,:,2:3),[],2);
parent.sobel=sobel_filt;
parent.findk=[];
parent.map=[];

nrows=size(img,1);
ncols=size(img,2);
img=reshape(img,nrows*ncols,[]);
dimg=reshape(dimg,nrows*ncols,[]);
lab=reshape(lab,nrows*ncols,[]);
dlab=reshape(dlab,nrows*ncols,[]);
dlab2=decorrstretch(reshape(lab(:,2:3),[],1,2));
dlab3=decorrstretch(reshape(lab,[],1,3));

knockout0=sobel_filt(1:50:end);
knockout1=sobel_filt(1:200:end);
knockout2=sobel_filt(1:200:end);
knockout3=sobel_filt(1:200:end);

x0=lab(1:50:end,2);
y0=lab(1:50:end,3);
c0=zeros(size(y0,1),1);
x0(knockout0==1)=NaN;
y0(knockout0==1)=NaN;

x1=dlab(1:200:end,2);
y1=dlab(1:200:end,3);
c1=zeros(size(y1,1),1);
x1(knockout1==1)=NaN;
y1(knockout1==1)=NaN;

x2=dlab2(1:200:end,1);
y2=dlab2(1:200:end,2);
c2=ones(size(y2,1),1);
x2(knockout2==1)=NaN;
y2(knockout2==1)=NaN;

x3=dlab3(1:200:end,2);
y3=dlab3(1:200:end,3);
c3=2.*ones(size(y3,1),1);
x3(knockout3==1)=NaN;
y3(knockout3==1)=NaN;

figure
scatter(x0,y0,10,'filled');
hold on
scatter(x1,y1,5,'filled');
scatter(x2,y2,4,'filled');
scatter(x3,y3,3,'filled');
xlabel('a*')
ylabel('b*')
legend('Original','RGB Dec.','ab Dec.','Lab Dec.');
title('a*b* Plot: Decorrelation')
hold off
colormap(hot)

figure;
scatter(x0,y0,10,img(1:50:end,:)./255,'filled');
xlabel('a*')
ylabel('b*')
title('a*b* Plot: RGB Coded')
hold on
scatter(127.5,127.5,5,[0 0 0],'filled');
hold off

figure;
ang=parent.score;
ang=ang./255;
ang=ang-0.5;
ang1=ang(:,1);
ang2=ang(:,2);
dist=(ang1.^2+ang2.^2).^0.5;
ang1(dist<Thre)=[];
ang2(dist<Thre)=[];

plotgram(atan2d(ang2,ang1),160);
xlim([-180 180]);
xlabel('Hue Angle /degrees')
ylabel('Bin Population')
box off
end