-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTesting1.m
145 lines (133 loc) · 4.28 KB
/
Testing1.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
%% Name: Rakshit Kothari
% Title: ACV project, Scene Classification
% Method: Scene classification using GMM and fixed SIFT features
% File details: This is the M file for matching SIFT features for each
% category. The output is fed into a neural network, which estimates the scene.
% To re-extract SIFT features,please run the M file named 'Reset_files.m'
%%
clear all
close all
clc
%% Setup for VL feat library
run VLFEATROOT/toolbox/vl_setup
%% Constants
load('Data.mat')
label_names=struct('Names',{'Sky' 'Water' 'Greens' 'Trees' 'Sand/Snow' 'Rocks/Mountains' 'Highway/Roads' 'Bldgs' 'Unclassified'});
% num=input('Please enter the image you want to classify');
h=fspecial('gaussian',5,sqrt(2));
trans_size=[128 128];
radius=8;
grid_k=5;
%% Algorithm
current=cd('Images_Training');
direc=dir('*.jpg');
num=1;
z=1;
while num<=150
count_labels=zeros(1,9);
scene_name=direc(num).name;
scene_name=scene_name(1:(find(scene_name=='_')-1));
img=double(imread(direc(num).name));
cd(current)
[X, Y, ~]=size(img);
img_ycbcr=rgb2ycbcr(img);
img_filtered=imfilter(img,h,'same');
img_ycbcr_trans=rgb2ycbcr(img_filtered);
img_ycbcr_trans=imresize(img_ycbcr_trans,trans_size);
a=img_ycbcr_trans(:,:,2); %Cb frame
trans_img(1,:)=a(:);
a=img_ycbcr_trans(:,:,3); %Cr frame
trans_img(2,:)=a(:);
tic
[label,model,~]=vbgm(trans_img,6);
figure(1)
spread(trans_img,label);
toc
r=reshape(label,trans_size);
figure(2)
imshow(r,[])
means=model.m;
reconstructed=zeros(X,Y);
for i=1:X
for j=1:Y
temp(1,1)=img_ycbcr(i,j,2);
temp(2,1)=img_ycbcr(i,j,3);
temp=repmat(temp,1,length(means));
temp=abs(means-temp);
temp=sum(temp,1);
[~,loc]=min(temp);
reconstructed(i,j)=loc;
clear temp
end
end
figure(3)
subplot(1,2,1)
imshow(reconstructed,[])
subplot(1,2,2)
imshow(uint8(img),[])
%% Extracting SIFT features
[x,y]=form_grid(X,Y,grid_k);
x=x(:)';
y=y(:)';
fc=[x;y;radius*ones(1,length(x));zeros(1,length(x))];
img_sift=single(rgb2gray(uint8(img)));
[f,d]=vl_sift(img_sift,'frames',fc,'orientations');
%% SIFT matching
for i=1:max(label)
mask=reconstructed==i;
if any(mask(:))
[com]=find_com(mask);
com_locs(i,:)=com;
trip=complete_belong(f,mask,0.9,radius);
g=f(:,trip);
sift_vector=d(:,trip);
if ~isempty(g)
masked_img=img_sift.*mask;
% figure(5)
% subplot(1,2,1)
% imshow(uint8(img),[])
% subplot(1,2,2)
% imshow(uint8(masked_img),[])
% h11 = vl_plotframe(g);
% h22 = vl_plotframe(g);
% set(h11,'color','k','linewidth',3);
% set(h22,'color','y','linewidth',2);
% drawnow
count_matches=zeros(1,max(categories));
for j=1:max(categories)
database=cell2mat(categories_sift_features(j,1));
if ~isempty(database)
[matches,scores]=vl_ubcmatch(sift_vector,database,1.2);
if ~isempty(matches)
matches=remove_repeated(matches);
end
count_matches(j)=length(matches)/length(g);
count_matches_check(i,j)={count_matches(j)};
count_scores(i,j)={scores};
end
end
end
end
if ~exist('count_matches','var') || isempty(count_matches)
count_matches=[zeros(1,8) 1];
end
[~,loc]=max(count_matches);
label_region(i)=loc;
count_labels(loc)=count_labels(loc)+1;
end
figure(6)
imshow(uint8(img),[])
for i=1:max(label)
x=com_locs(i,1);
y=com_locs(i,2);
text(x,y,label_names(label_region(i)).Names,'Color',[0 0 0],'EdgeColor',[0 0 0],'BackgroundColor',[1 1 1])
end
testing_ip(:,z)=count_labels';
testing_op(z)=get_category(scene_name);
num=num+1;
z=z+1;
current=cd('Images_Training');
save('Testing.mat','testing_ip','testing_op')
end
cd(current)
save('Testing.mat','testing_ip','testing_op')