%	LDEP returns the local diagonal extrema pattern of an image.
%   The original code of LBP is used and updated to the LDEP by Shiv Ram Dubey, IIIT Allahabad
%   This code can be used only for the academic and research purposes and can not be used for any commercial purposes.
%   Cite the paper 'Shiv Ram Dubey, Satish Kumar Singh, Rajat Kumar Singh, 
%		"Local Diagonal Extrema Pattern: A New and Efficient Feature Descriptor for CT Image Retrieval," 
%		IEEE Signal Processing Letters, vol. 22, no. 9, pp. 1215-1219, 2015,
%		In case you are using this code.


function h11=LDEP(path_image)
% path_image='patch60.tiff';
img=im2uint8(imread(path_image));
if length(size(img))==3
    img=rgb2gray(img);
end
h11=ldep1(img,1,8,0,'nh');
h11=h11/sum(h11);
end


function result = ldep1(varargin) % image,radius,neighbors,mapping,mode)
error(nargchk(1,5,nargin));

image=varargin{1};
d_image=double(image);

if nargin==1
    spoints=[-1 -1; -1 0; -1 1; 0 -1; -0 1; 1 -1; 1 0; 1 1];
    neighbors=8;
    mapping=0;
    mode='h';
end

if (nargin == 2) && (length(varargin{2}) == 1)
    error('Input arguments');
end

if (nargin > 2) && (length(varargin{2}) == 1)
    radius=varargin{2};
    neighbors=varargin{3};
    
    spoints=zeros(neighbors,2);

    % Angle step.
    a = 2*pi/neighbors;
    
    for i = 1:neighbors
        spoints(i,1) = -radius*sin((i-1)*a);
        spoints(i,2) = radius*cos((i-1)*a);
    end
    
    if(nargin >= 4)
        mapping=varargin{4};
        if(isstruct(mapping) && mapping.samples ~= neighbors)
            error('Incompatible mapping');
        end
    else
        mapping=0;
    end
    
    if(nargin >= 5)
        mode=varargin{5};
    else
        mode='h';
    end
end

if (nargin > 1) && (length(varargin{2}) > 1)
    spoints=varargin{2};
    neighbors=size(spoints,1);
    
    if(nargin >= 3)
        mapping=varargin{3};
        if(isstruct(mapping) && mapping.samples ~= neighbors)
            error('Incompatible mapping');
        end
    else
        mapping=0;
    end
    
    if(nargin >= 4)
        mode=varargin{4};
    else
        mode='h';
    end   
end

% Determine the dimensions of the input image.
[ysize xsize] = size(image);



miny=min(spoints(:,1));
maxy=max(spoints(:,1));
minx=min(spoints(:,2));
maxx=max(spoints(:,2));

% Block size, each LDEP code is computed within a block of size bsizey*bsizex
bsizey=ceil(max(maxy,0))-floor(min(miny,0))+1;
bsizex=ceil(max(maxx,0))-floor(min(minx,0))+1;

% Coordinates of origin (0,0) in the block
origy=1-floor(min(miny,0));
origx=1-floor(min(minx,0));

% Minimum allowed size for the input image depends
% on the radius of the used LDEP operator.
if(xsize < bsizex || ysize < bsizey)
  error('Too small input image. Should be at least (2*radius+1) x (2*radius+1)');
end

% Calculate dx and dy;
dx = xsize - bsizex;
dy = ysize - bsizey;

% Fill the center pixel matrix C.
C = image(origy:origy+dy,origx:origx+dx);
d_C = double(C);


%Compute the neighbors

for i = 1:neighbors
  y = spoints(i,1)+origy;
  x = spoints(i,2)+origx;
  % Calculate floors, ceils and rounds for the x and y.
  fy = floor(y); cy = ceil(y); ry = round(y);
  fx = floor(x); cx = ceil(x); rx = round(x);
  % Check if interpolation is needed.
  if (abs(x - rx) < 1e-6) && (abs(y - ry) < 1e-6)
    % Interpolation is not needed, use original datatypes
    N(:,:,i) = d_image(ry:ry+dy,rx:rx+dx);
  else
    % Interpolation needed, use double type images 
    ty = y - fy;
    tx = x - fx;

    % Calculate the interpolation weights.
    w1 = (1 - tx) * (1 - ty);
    w2 =      tx  * (1 - ty);
    w3 = (1 - tx) *      ty ;
    w4 =      tx  *      ty ;
    % Compute interpolated pixel values
    N(:,:,i) = w1*d_image(fy:fy+dy,fx:fx+dx) + w2*d_image(fy:fy+dy,cx:cx+dx) + ...
        w3*d_image(cy:cy+dy,fx:fx+dx) + w4*d_image(cy:cy+dy,cx:cx+dx);
  end    
end
result11=zeros(dy+1,dx+1);result12=zeros(dy+1,dx+1);
result21=zeros(dy+1,dx+1);result22=zeros(dy+1,dx+1);
result31=zeros(dy+1,dx+1);result32=zeros(dy+1,dx+1);

N1(:,:,1:4)=N(:,:,2:2:8);
[N2,in2]=max(N1,[],3);[N3,in3]=min(N1,[],3);
N2=N2>d_C;N3=N3>d_C;
p1= N2==1 & N3==1;
p2= N2==0 & N3==0;
p3= N2==1 & N3==0;
result11=p1.*in2;result12=p1.*in3;
result21=p2.*in2;result22=p2.*in3;
result31=p3.*in2;result32=p3.*in3;

result11=hist(result11(:),0:4);    result12=hist(result12(:),0:4);
result21=hist(result21(:),0:4);    result22=hist(result22(:),0:4);
result31=hist(result31(:),0:4);    result32=hist(result32(:),0:4);

result11(1)=[];result12(1)=[];result21(1)=[];result22(1)=[];result31(1)=[];result32(1)=[];
result=[result11 result12 result21 result22 result31 result32];


end




