I want to calculate LTrp for 0,45,90,13​5,180,225,​270,315 degrees. in the below code 0,90,180,270 calculated.

1 visualización (últimos 30 días)
function [hist_LTrP_u2,hist_LTrP_u2_convex_up,hist_LTrP_u2_convex_down] = LTrP_func(image,window_size,thres)
[row,col]=size(image);
evm = evm_full(image,window_size);
evm = evm(2:end-2,2:end-2);
temp = conv2(image,[1 -1]);
diff_x_0 = temp(:,2:end)>0;
temp = -1*conv2(image,[-1 1]);
diff_x_180 = temp(:,1:end-1)>0;
temp = -1*conv2(image,[-1 ;1]);
diff_y_270 = temp(2:end,:)>0;
temp = -1*conv2(image,[1 ;-1]);
diff_y_90 = temp(1:end-1,:)>0;
pat_x_0_y_90 = diff_x_0 + 2*diff_y_90;
pat_x_0_y_270 = diff_x_0 + 2*diff_y_270;
pat_x_180_y_90 = diff_x_180 + 2*diff_y_90;
pat_x_180_y_270 = diff_x_180 + 2*diff_y_270;
%% Calculate LTrP Pattern for 0 degree and 90 degree direction (derivative along Postive x axis and Positive y axis)
% Append all 8-neighborhood to a single matrix
pat_i_y = pat_x_0_y_90;
neigh_mtx=zeros(row-3,col-3,8);
neigh_mtx(:,:,1)=pat_i_y(2:row-2,2:col-2);
neigh_mtx(:,:,2)=pat_i_y(2:row-2,3:col-1);
neigh_mtx(:,:,3)=pat_i_y(2:row-2,4:col);
neigh_mtx(:,:,4)=pat_i_y(3:row-1,4:col);
neigh_mtx(:,:,5)=pat_i_y(4:row,4:col);
neigh_mtx(:,:,6)=pat_i_y(4:row,3:col-1);
neigh_mtx(:,:,7)=pat_i_y(4:row,2:col-2);
neigh_mtx(:,:,8)=pat_i_y(3:row-1,2:col-2);
mapping=getmapping(8,'u2');
neigh_mtx_temp = neigh_mtx == 1;
LTrP_code_i_y=zeros(row-3,col-3);
for neighbor=1:8
LTrP_code_i_y=LTrP_code_i_y+2^(neighbor-1)*neigh_mtx_temp(:,:,neighbor);
end
LTrP_u2=mapping.table(LTrP_code_i_y+1);
hist_LTrP_u2_0_90_1=hist(LTrP_u2(:),0:58);
hist_LTrP_u2_0_90_1_convex_down=hist(LTrP_u2(evm>=thres),0:58);
hist_LTrP_u2_0_90_1_convex_up=hist(LTrP_u2(evm<thres),0:58);
neigh_mtx_temp = neigh_mtx == 2;
LTrP_code_i_y=zeros(row-3,col-3);
for neighbor=1:8
LTrP_code_i_y=LTrP_code_i_y+2^(neighbor-1)*neigh_mtx_temp(:,:,neighbor);
end
LTrP_u2=mapping.table(LTrP_code_i_y+1);
hist_LTrP_u2_0_90_2=hist(LTrP_u2(:),0:58);
hist_LTrP_u2_0_90_2_convex_down=hist(LTrP_u2(evm>=thres),0:58);
hist_LTrP_u2_0_90_2_convex_up=hist(LTrP_u2(evm<thres),0:58);
neigh_mtx_temp = neigh_mtx == 3;
LTrP_code_i_y=zeros(row-3,col-3);
for neighbor=1:8
LTrP_code_i_y=LTrP_code_i_y+2^(neighbor-1)*neigh_mtx_temp(:,:,neighbor);
end
LTrP_u2=mapping.table(LTrP_code_i_y+1);
hist_LTrP_u2_0_90_3=hist(LTrP_u2(:),0:58);
hist_LTrP_u2_0_90_3_convex_down=hist(LTrP_u2(evm>=thres),0:58);
hist_LTrP_u2_0_90_3_convex_up=hist(LTrP_u2(evm<thres),0:58);
%% Calculate LTrP Pattern for 0 degree and 270 degree direction (derivative along Postive x axis and Negative y axis)
% Append all 8-neighborhood to a single matrix
pat_i_y = pat_x_0_y_270;
neigh_mtx=zeros(row-3,col-3,8);
neigh_mtx(:,:,1)=pat_i_y(2:row-2,2:col-2);
neigh_mtx(:,:,2)=pat_i_y(2:row-2,3:col-1);
neigh_mtx(:,:,3)=pat_i_y(2:row-2,4:col);
neigh_mtx(:,:,4)=pat_i_y(3:row-1,4:col);
neigh_mtx(:,:,5)=pat_i_y(4:row,4:col);
neigh_mtx(:,:,6)=pat_i_y(4:row,3:col-1);
neigh_mtx(:,:,7)=pat_i_y(4:row,2:col-2);
neigh_mtx(:,:,8)=pat_i_y(3:row-1,2:col-2);
mapping=getmapping(8,'u2');
neigh_mtx_temp = neigh_mtx == 1;
LTrP_code_i_y=zeros(row-3,col-3);
for neighbor=1:8
LTrP_code_i_y=LTrP_code_i_y+2^(neighbor-1)*neigh_mtx_temp(:,:,neighbor);
end
LTrP_u2=mapping.table(LTrP_code_i_y+1);
hist_LTrP_u2_0_270_1=hist(LTrP_u2(:),0:58);
hist_LTrP_u2_0_270_1_convex_down=hist(LTrP_u2(evm>=thres),0:58);
hist_LTrP_u2_0_270_1_convex_up=hist(LTrP_u2(evm<thres),0:58);
neigh_mtx_temp = neigh_mtx == 2;
LTrP_code_i_y=zeros(row-3,col-3);
for neighbor=1:8
LTrP_code_i_y=LTrP_code_i_y+2^(neighbor-1)*neigh_mtx_temp(:,:,neighbor);
end
LTrP_u2=mapping.table(LTrP_code_i_y+1);
hist_LTrP_u2_0_270_2=hist(LTrP_u2(:),0:58);
hist_LTrP_u2_0_270_2_convex_down=hist(LTrP_u2(evm>=thres),0:58);
hist_LTrP_u2_0_270_2_convex_up=hist(LTrP_u2(evm<thres),0:58);
neigh_mtx_temp = neigh_mtx == 3;
LTrP_code_i_y=zeros(row-3,col-3);
for neighbor=1:8
LTrP_code_i_y=LTrP_code_i_y+2^(neighbor-1)*neigh_mtx_temp(:,:,neighbor);
end
LTrP_u2=mapping.table(LTrP_code_i_y+1);
hist_LTrP_u2_0_270_3=hist(LTrP_u2(:),0:58);
hist_LTrP_u2_0_270_3_convex_down=hist(LTrP_u2(evm>=thres),0:58);
hist_LTrP_u2_0_270_3_convex_up=hist(LTrP_u2(evm<thres),0:58);
%% Calculate LTrP Pattern for 180 degree and 90 degree direction (derivative along Negative x axis and Positive y axis)
% Append all 8-neighborhood to a single matrix
pat_i_y = pat_x_180_y_90;
neigh_mtx=zeros(row-3,col-3,8);
neigh_mtx(:,:,1)=pat_i_y(2:row-2,2:col-2);
neigh_mtx(:,:,2)=pat_i_y(2:row-2,3:col-1);
neigh_mtx(:,:,3)=pat_i_y(2:row-2,4:col);
neigh_mtx(:,:,4)=pat_i_y(3:row-1,4:col);
neigh_mtx(:,:,5)=pat_i_y(4:row,4:col);
neigh_mtx(:,:,6)=pat_i_y(4:row,3:col-1);
neigh_mtx(:,:,7)=pat_i_y(4:row,2:col-2);
neigh_mtx(:,:,8)=pat_i_y(3:row-1,2:col-2);
mapping=getmapping(8,'u2');
neigh_mtx_temp = neigh_mtx == 1;
LTrP_code_i_y=zeros(row-3,col-3);
for neighbor=1:8
LTrP_code_i_y=LTrP_code_i_y+2^(neighbor-1)*neigh_mtx_temp(:,:,neighbor);
end
LTrP_u2=mapping.table(LTrP_code_i_y+1);
hist_LTrP_u2_180_90_1=hist(LTrP_u2(:),0:58);
hist_LTrP_u2_180_90_1_convex_down=hist(LTrP_u2(evm>=thres),0:58);
hist_LTrP_u2_180_90_1_convex_up=hist(LTrP_u2(evm<thres),0:58);
neigh_mtx_temp = neigh_mtx == 2;
LTrP_code_i_y=zeros(row-3,col-3);
for neighbor=1:8
LTrP_code_i_y=LTrP_code_i_y+2^(neighbor-1)*neigh_mtx_temp(:,:,neighbor);
end
LTrP_u2=mapping.table(LTrP_code_i_y+1);
hist_LTrP_u2_180_90_2=hist(LTrP_u2(:),0:58);
hist_LTrP_u2_180_90_2_convex_down=hist(LTrP_u2(evm>=thres),0:58);
hist_LTrP_u2_180_90_2_convex_up=hist(LTrP_u2(evm<thres),0:58);
neigh_mtx_temp = neigh_mtx == 3;
LTrP_code_i_y=zeros(row-3,col-3);
for neighbor=1:8
LTrP_code_i_y=LTrP_code_i_y+2^(neighbor-1)*neigh_mtx_temp(:,:,neighbor);
end
LTrP_u2=mapping.table(LTrP_code_i_y+1);
hist_LTrP_u2_180_90_3=hist(LTrP_u2(:),0:58);
hist_LTrP_u2_180_90_3_convex_down=hist(LTrP_u2(evm>=thres),0:58);
hist_LTrP_u2_180_90_3_convex_up=hist(LTrP_u2(evm<thres),0:58);
%% Calculate LTrP Pattern for 180 degree and 270 degree direction (derivative along Negative x axis and Negative y axis)
% Append all 8-neighborhood to a single matrix
pat_i_y = pat_x_180_y_270;
neigh_mtx=zeros(row-3,col-3,8);
neigh_mtx(:,:,1)=pat_i_y(2:row-2,2:col-2);
neigh_mtx(:,:,2)=pat_i_y(2:row-2,3:col-1);
neigh_mtx(:,:,3)=pat_i_y(2:row-2,4:col);
neigh_mtx(:,:,4)=pat_i_y(3:row-1,4:col);
neigh_mtx(:,:,5)=pat_i_y(4:row,4:col);
neigh_mtx(:,:,6)=pat_i_y(4:row,3:col-1);
neigh_mtx(:,:,7)=pat_i_y(4:row,2:col-2);
neigh_mtx(:,:,8)=pat_i_y(3:row-1,2:col-2);
mapping=getmapping(8,'u2');
neigh_mtx_temp = neigh_mtx == 1;
LTrP_code_i_y=zeros(row-3,col-3);
for neighbor=1:8
LTrP_code_i_y=LTrP_code_i_y+2^(neighbor-1)*neigh_mtx_temp(:,:,neighbor);
end
LTrP_u2=mapping.table(LTrP_code_i_y+1);
hist_LTrP_u2_180_270_1=hist(LTrP_u2(:),0:58);
hist_LTrP_u2_180_270_1_convex_down=hist(LTrP_u2(evm>=thres),0:58);
hist_LTrP_u2_180_270_1_convex_up=hist(LTrP_u2(evm<thres),0:58);
neigh_mtx_temp = neigh_mtx == 2;
LTrP_code_i_y=zeros(row-3,col-3);
for neighbor=1:8
LTrP_code_i_y=LTrP_code_i_y+2^(neighbor-1)*neigh_mtx_temp(:,:,neighbor);
end
LTrP_u2=mapping.table(LTrP_code_i_y+1);
hist_LTrP_u2_180_270_2=hist(LTrP_u2(:),0:58);
hist_LTrP_u2_180_270_2_convex_down=hist(LTrP_u2(evm>=thres),0:58);
hist_LTrP_u2_180_270_2_convex_up=hist(LTrP_u2(evm<thres),0:58);
neigh_mtx_temp = neigh_mtx == 3;
LTrP_code_i_y=zeros(row-3,col-3);
for neighbor=1:8
LTrP_code_i_y=LTrP_code_i_y+2^(neighbor-1)*neigh_mtx_temp(:,:,neighbor);
end
LTrP_u2=mapping.table(LTrP_code_i_y+1);
hist_LTrP_u2_180_270_3=hist(LTrP_u2(:),0:58);
hist_LTrP_u2_180_270_3_convex_down=hist(LTrP_u2(evm>=thres),0:58);
hist_LTrP_u2_180_270_3_convex_up=hist(LTrP_u2(evm<thres),0:58);
hist_LTrP_u2 = [hist_LTrP_u2_0_90_1 hist_LTrP_u2_0_90_2 hist_LTrP_u2_0_90_3 hist_LTrP_u2_180_270_1 hist_LTrP_u2_180_270_2 hist_LTrP_u2_180_270_3 hist_LTrP_u2_180_90_1 hist_LTrP_u2_180_90_2 hist_LTrP_u2_180_90_3 hist_LTrP_u2_0_270_1 hist_LTrP_u2_0_270_2 hist_LTrP_u2_0_270_3];
hist_LTrP_u2_convex_down = [hist_LTrP_u2_0_90_1_convex_down hist_LTrP_u2_0_90_2_convex_down hist_LTrP_u2_0_90_3_convex_down hist_LTrP_u2_180_270_1_convex_down hist_LTrP_u2_180_270_2_convex_down hist_LTrP_u2_180_270_3_convex_down hist_LTrP_u2_180_90_1_convex_down hist_LTrP_u2_180_90_2_convex_down hist_LTrP_u2_180_90_3_convex_down hist_LTrP_u2_0_270_1_convex_down hist_LTrP_u2_0_270_2_convex_down hist_LTrP_u2_0_270_3_convex_down];
hist_LTrP_u2_convex_up = [hist_LTrP_u2_0_90_1_convex_up hist_LTrP_u2_0_90_2_convex_up hist_LTrP_u2_0_90_3_convex_up hist_LTrP_u2_180_270_1_convex_up hist_LTrP_u2_180_270_2_convex_up hist_LTrP_u2_180_270_3_convex_up hist_LTrP_u2_180_90_1_convex_up hist_LTrP_u2_180_90_2_convex_up hist_LTrP_u2_180_90_3_convex_up hist_LTrP_u2_0_270_1_convex_up hist_LTrP_u2_0_270_2_convex_up hist_LTrP_u2_0_270_3_convex_up];
end
function Normalized_EVM = evm_full(I,x)
% x should be odd
factor = [0 0 6 0 50 0 196 0 540 0 1210 0 2366 0 4200 0 6936 0 10830 0 16170 0 23276 0 32500];
a = 1:x;
k_xcentroid = repmat(a,[x,1]);
k_ycentroid = k_xcentroid';
U = ones(x,x);
k_cov = k_xcentroid .* k_ycentroid;
k_xvar = k_xcentroid.^2;
k_yvar = k_ycentroid.^2;
conv2_UI = conv2(I,U,'same');
X_centroid = conv2(I,k_xcentroid,'same')./conv2_UI;
Y_centroid = conv2(I,k_ycentroid,'same')./conv2_UI;
X_centroid(~isfinite(X_centroid))=0;
Y_centroid(~isfinite(Y_centroid))=0;
X_var = conv2(I,k_xvar,'same')-(X_centroid).^2 .* conv2_UI;
Y_var = conv2(I,k_yvar,'same')-(Y_centroid).^2 .* conv2_UI;
Cov_xy = conv2(I,k_cov,'same')- X_centroid .* Y_centroid .* conv2_UI;
cum_var = X_var+Y_var;
bw_sqrt = sqrt(cum_var.^2-4*(X_var.*Y_var-Cov_xy.^2));
EVM = (cum_var+bw_sqrt)/2;
Normalized_EVM = EVM./(I*factor(x));
%GETMAPPING returns a structure containing a mapping table for LBP codes.
% MAPPING = GETMAPPING(SAMPLES,MAPPINGTYPE) returns a
% structure containing a mapping table for
% LBP codes in a neighbourhood of SAMPLES sampling
% points. Possible values for MAPPINGTYPE are
% 'u2' for uniform LBP
% 'ri' for rotation-invariant LBP
% 'riu2' for uniform rotation-invariant LBP.
%
% Example:
% I=imread('rice.tif');
% MAPPING=getmapping(16,'riu2');
% LBPHIST=lbp(I,2,16,MAPPING,'hist');
% Now LBPHIST contains a rotation-invariant uniform LBP
% histogram in a (16,2) neighbourhood.
%
function mapping = getmapping(samples,mappingtype)
% Version 0.2
% Authors: Marko Heikkil?, Timo Ahonen and Xiaopeng Hong
% Changelog
% 0.1.1 Changed output to be a structure
% Fixed a bug causing out of memory errors when generating rotation
% invariant mappings with high number of sampling points.
% Lauge Sorensen is acknowledged for spotting this problem.
% Modified by Xiaopeng HONG and Guoying ZHAO
% Changelog
% 0.2
% Solved the compatible issue for the bitshift function in Matlab
% 2012 & higher
matlab_ver = ver('MATLAB');
matlab_ver = str2double(matlab_ver.Version);
if matlab_ver < 8
mapping = getmapping_ver7(samples,mappingtype);
else
mapping = getmapping_ver8(samples,mappingtype);
end
end
function mapping = getmapping_ver7(samples,mappingtype)
disp('For Matlab version 7.x and lower');
table = 0:2^samples-1;
newMax = 0; %number of patterns in the resulting LBP code
index = 0;
if strcmp(mappingtype,'u2') %Uniform 2
newMax = samples*(samples-1) + 3;
for i = 0:2^samples-1
j = bitset(bitshift(i,1,samples),1,bitget(i,samples)); %rotate left
numt = sum(bitget(bitxor(i,j),1:samples)); %number of 1->0 and
%0->1 transitions
%in binary string
%x is equal to the
%number of 1-bits in
%XOR(x,Rotate left(x))
if numt <= 2
table(i+1) = index;
index = index + 1;
else
table(i+1) = newMax - 1;
end
end
end
if strcmp(mappingtype,'ri') %Rotation invariant
tmpMap = zeros(2^samples,1) - 1;
for i = 0:2^samples-1
rm = i;
r = i;
for j = 1:samples-1
r = bitset(bitshift(r,1,samples),1,bitget(r,samples)); %rotate
%left
if r < rm
rm = r;
end
end
if tmpMap(rm+1) < 0
tmpMap(rm+1) = newMax;
newMax = newMax + 1;
end
table(i+1) = tmpMap(rm+1);
end
end
if strcmp(mappingtype,'riu2') %Uniform & Rotation invariant
newMax = samples + 2;
for i = 0:2^samples - 1
j = bitset(bitshift(i,1,samples),1,bitget(i,samples)); %rotate left
numt = sum(bitget(bitxor(i,j),1:samples));
if numt <= 2
table(i+1) = sum(bitget(i,1:samples));
else
table(i+1) = samples+1;
end
end
end
mapping.table=table;
mapping.samples=samples;
mapping.num=newMax;
end
function mapping = getmapping_ver8(samples,mappingtype)
%disp('For Matlab version 8.0 and higher');
table = 0:2^samples-1;
newMax = 0; %number of patterns in the resulting LBP code
index = 0;
if strcmp(mappingtype,'u2') %Uniform 2
newMax = samples*(samples-1) + 3;
for i = 0:2^samples-1
i_bin = dec2bin(i,samples);
j_bin = circshift(i_bin',-1)'; %circularly rotate left
numt = sum(i_bin~=j_bin); %number of 1->0 and
%0->1 transitions
%in binary string
%x is equal to the
%number of 1-bits in
%XOR(x,Rotate left(x))
if numt <= 2
table(i+1) = index;
index = index + 1;
else
table(i+1) = newMax - 1;
end
end
end
if strcmp(mappingtype,'ri') %Rotation invariant
tmpMap = zeros(2^samples,1) - 1;
for i = 0:2^samples-1
rm = i;
r_bin = dec2bin(i,samples);
for j = 1:samples-1
r = bin2dec(circshift(r_bin',-1*j)'); %rotate left
if r < rm
rm = r;
end
end
if tmpMap(rm+1) < 0
tmpMap(rm+1) = newMax;
newMax = newMax + 1;
end
table(i+1) = tmpMap(rm+1);
end
end
if strcmp(mappingtype,'riu2') %Uniform & Rotation invariant
newMax = samples + 2;
for i = 0:2^samples - 1
i_bin = dec2bin(i,samples);
j_bin = circshift(i_bin',-1)';
numt = sum(i_bin~=j_bin);
if numt <= 2
table(i+1) = sum(bitget(i,1:samples));
else
table(i+1) = samples+1;
end
end
end
mapping.table=table;
mapping.samples=samples;
mapping.num=newMax;
end
[SL: formatted the code as code not text]

Respuestas (1)

ArunaKumari Palugu
ArunaKumari Palugu el 18 de Jul. de 2020
pat_i_y = pat_x_0_y_90; neigh_mtx=zeros(row-3,col-3,8); neigh_mtx(:,:,1)=pat_i_y(2:row-2,2:col-2);

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by