adding the GLCM features (divide the image into 16-by-16 blocks, and for each block get the energy and contrast features)

1 view (last 30 days)
dear brothers and sisters
i am new student in matlab i have a task that dataset (contains 2 categories, each has 50 image),
i -calculate the same features as above for each image
2- divide the data into 25 images train and 25 images test for each category
3- calculate the % correct in retrieving the nearest image for each query
4- adding the GLCM features (divide the image into 16-by-16 blocks, and for each block get the energy and contrast features)
5- Also, try to use the PCA
----------------------------------------------
i finsh points (1,2,3)
how can i applay GLCM and PCA
ANY ONE HELP
i will uploud my code which solve points (1,2,3) which we make it in two file
first file ---------------->
%This code reads all the COREL images and extracts feature vector for each image
%The features used are:
%(a)
clear;
clearvars;
%First we load the total image that contains images from all categories
%Then we create the color map from this image
for jndex=1:2 %over the 2 categories
if(jndex==1)
wwww = 'NPatsmall';
elseif(jndex==2)
wwww = 'TPatsmall';
end
www= wwww ;
K=40; %the DCT block width
KK=16; %the histohram length of R,G,B
KKK = 256; %this is the size of the Global color MAP
%here we load an image which is a combination of many colors
ZZZ=imread('TOTAL.jpg'); %this is the combined image
[IIND MAP] = rgb2ind(ZZZ,256); %this is the global color table MAP
ind=0;
path=strcat('Patches\', www ,'\');
outputfile2=strcat(www);
outputfileName2=strcat(path,outputfile2);
outputfileName3=strcat(outputfile2);
for iiii=1:50 %loop over the 100 images in each category
iii = (jndex-1)*50 + iiii -1;
fileName1=strcat(num2str(iii,'%2d'),'.tif');
outputfile1=strcat(num2str(iii,'%2d'),'.feats');
fileName1=strcat(path,fileName1);
outputfileName=strcat(path,outputfile1);
%reading the image:
X= imread(fileName1);
Xg=rgb2gray(X);
%This is the DCT2 for the GRAY and we take the upper left DCT2 coefficients
%with a square of size K-by-K
Xg=im2double(Xg);
Jg = dct2(Xg);
jg=im2col(Jg,[K K],'distinct'); %if K=12 this gives a vector length 144
bg = jg(:,1);
%For K=40, this will give 1600 features
%Here we get each color channel alone
y1=X(:,:,1); %Red
y2=X(:,:,2); %Green
y3=X(:,:,3); %Blue
Y1=im2double(y1);
Y2=im2double(y2);
Y3=im2double(y3);
%Now for the global color histograms for each separate color channel
h1 = imhist(Y1,KK); %red histogram
h2 = imhist(Y2,KK); %green histogram
h3 = imhist(Y3,KK); %blue histogram
%each histo has only KK bins (chosen to be 16 here)
h=[h1; h2; h3]; %here we concatenate the 3 histos from the 3 colors
%this makes 3*16 = 48 features in this case
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Now we have the color histogram for the combined colors using the color
%MAP. This color map was obtained from a collection of one image from each
%category. The MAP has
Xind = rgb2ind(X,MAP); %this transforms the image to an indexed image using the total map
UU = imhist(Xind,MAP); %this is the KKK vector with the combined colors histogram
%here KKK=256 giving 256 features
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
D= [bg; h; UU]; %the concatenated super vector
%So, total number of features is 144+48+256 = 448 features.
L = length(D);
ind=ind+1;
Data(ind,1:L)=D(1:L);
end
save(outputfileName3,'Data','-ASCII'); %this saves one feature file for each category
%in the same folder that this file is working from.
end
seconf file -------------------->
%in this code we do locally adative random projections followed by a
%bagging trees selection and retrieval
clear;
clearvars;
doPCA=1; %this is to do PCA on the RAW features to produce reduced dimension features
NC=21; %# of PCA coeffs.
Normalize=0; %normalize each feature before the PCA: here we only divide over the max(abs)
ZNormalize=0; %if 1 then Z-normalize each feature after PCA
UseBias= 0; %to add a bias term after the PCA
HII = 50; %the maximum number of images to recall
%The COREL data with some not very good features (indexed color histogram, DCT,
%color histograms)
load NPatsmall
load TPatsmall
sss=size(NPatsmall); %they all have same size
s11 = sss(1); %this is the number of examples in this category (100,true50)
s22 = sss(2); %this is the number of features extracted from the original images(3)
AAAA = [NPatsmall; TPatsmall]; %This array contains the whole data. Size= (1000,true100) by #features
mam=max(abs(AAAA)); %the absolute max for each feature
%this is to make feature ranges close to each others
s2=s22;
if(Normalize==1)
for i=1:100
for j=1:s2
AA(i,j) = AAAA(i,j)/mam(j);
end
end
else
AA = AAAA;
end
%Now we divide the data into training and testing (50,true 25)% each
cn1=0;
cn2=0;
for i=1:2:100
j=i+1;
cn1=cn1+1;
Train(cn1,1:s2)=AA(i,1:s2);
Test(cn1,1:s2)=AA(j,1:s2);
end
TrainLong = Train;
%%%%%%%%%%%%%%%%%% PCA part (principal component analysis to reduce
%%%%%%%%%%%%%%%%%% number of features)
if(doPCA==1)
[coeffs, scores, latents] = pca(Train);
num_pca=NC; %This is optional but usually up to the number of images
evectors = coeffs(:, 1:num_pca);
Train2 = evectors' * Train';
Test2 = evectors' * Test';
Train = Train2';
Test = Test2';
newSize = size(Train);
s2 = newSize(2);
s1 = newSize(1);
end
newSizeI = size(Train);
s2 = newSizeI(2);
s1 = newSizeI(1);
XX=[Train; Test];
if(ZNormalize==1)
[XXX]=normalizemean(XX);
else
XXX = XX;
end
if(UseBias==1)
XXX(1:2*s1, s2) = 0.5; %bias term
end
TrainN=XXX(1:s1,1:s2);
TestN=XXX((s1+1):(2*s1), 1:s2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Now all features are normalized to zero
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% mean and unity variance
%------------------------------------------------------
ssz=size(Train);
s1=ssz(1);
N = s1; %the number of training example
TrainD=Train; %to initialize, then TrainD will dynamically change
%Then we do the filtering process based on the RELIEF algorithm
%here we look at the training data and we look the whole binary code as
%a feature vector over the whole training data
%%%********************************************
%SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%% The Evaluation part %%%%%%%%%%%%%%%%%%%
%this is for the raw data recognition rate
ccntt=0;
for i=1:50
aAtom=Test(i,:);
aclass1=floor((i-1)/25)+1;
aDDD=dist(aAtom,Train'); %the Euclidean distance calculation
[ddd iii]=sort(aDDD); %this is to find the minimum distance
aclass2= floor((iii(1)-1)/25)+1;
if(aclass1==aclass2)
ccntt=ccntt+1;
end
end
disp('recog rate for test data using the features')
100*ccntt/50
%%%Now we see the category of the top HII using the original data
xCcntt(1:HII)=0;
for i=1:50 %over all the test data
Atom=Test(i,:);
class1=floor((i-1)/25)+1; %the class of the test pattern
DDD=dist(Atom,Train');
[ddd iii]=sort(DDD);
%iii(1:HII) = Xranking(i,1:HII);
for hii=1:HII
xClass2(hii) = floor((iii(hii)-1)/25)+1;
if(class1==xClass2(hii)) xCcntt(hii)=xCcntt(hii)+1;
xCCC(i,hii)=1;
else
xCCC(i,hii)=0;
end
end
end
100*xCcntt/50;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j=1:50
xcorr=0;
for i=1:50
xcorr = xcorr + sum(xCCC(i,1:j));
end
xprec(j)=xcorr/(5*j);
xrecal(j)=xcorr/(5*25);
end
plot(xrecal,xprec);

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!