Can any one explain the following code which implements CBIR..??
1 view (last 30 days)
Show older comments
function CBIR_Query(q_imgid, dist_id, lamda)
imgdir = 'D:\ENTRY LEVEL\M.Tech Seminar & Project\Project\DB\1000 images\';
fetdir = 'D:\ENTRY LEVEL\M.Tech Seminar & Project\Project\DB\1000 images\';
imgno = 1:1000;
imgsuffix = '.jpg';
q_imgid=input('Enter the Query Image (1~1000):');
dist_id = [3 3];
lamda = 0.7;
t1=tic;
q_feature = load([fetdir,num2str(q_imgid),'.mat']);
q_feature = q_feature.feature;
dist = zeros(2, length(imgno));
for i=imgno
load([fetdir,num2str(i),'.mat']);
if dist_id(1) == 1
dist(1,i) = dist(1,i) +
CBIR_L2dist(q_feature.colorhist, feature.colorhist);
elseif dist_id(1) == 2
dist(1,i) = dist(1,i) +
CBIR_cosinedist(q_feature.colorhist,feature.colorhist);
elseif dist_id(1) == 3
dist(1,i) = dist(1,i) +
CBIR_histintersection(q_feature.colorhist,feature.colorhist);
end
if dist_id(2) == 1
dist(2,i) = dist(2,i) +
CBIR_L2dist(q_feature.edgedirection,feature.edgedirection);
elseif dist_id(2) == 2
dist(2,i) = dist(2,i) +
CBIR_cosinedist(q_feature.edgedirection, feature.edgedirection);
elseif dist_id(2) == 3
dist(2,i) = dist(2,i) +
CBIR_histintersection(q_feature.edgedirection,feature.edgedirection);
end
end
w = [lamda 1-lamda];
c_dist = w*dist;
[score(1,:), score(2,:)] = sort(c_dist);
N = 20; C = 4; R = 5; ct=0;
figure
for i = 1 : N
ct=ct+1;
img = imread([imgdir,num2str(score(2,i)),imgsuffix]);
subplot(C, R, i);
imshow(img);
end
precision=ct/20;
precision
recall=ct/100;
recall
fprintf('Time calculated for Color and Shape Feature only \n');
toc(t1)
function dist = CBIR_histintersection(x1, x2)
dist = sum(min([x1(:)'; x2(:)']));
denom = min([sum(x1(:)),sum(x2(:))])+1e-8;
dist = dist/denom;
dist = 1-dist;
function CBIR_featurecalc()
imgdir = 'D:\ENTRY LEVEL\M.Tech Seminar & Project\Project\DB\1000 images\';
fetdir = 'D:\ENTRY LEVEL\M.Tech Seminar & Project\Project\DB\1000 images\';
imgno = 1:1000;
imgsuffix = '.jpg';
for i=imgno
imgrgb=imread([imgdir,num2str(i),imgsuffix]);
feature.colorhist = CBIR_colorhist(imgrgb);
feature.edgehist = CBIR_edgehist(imgrgb);
feature.edgedirection = CBIR_edgedirection(imgrgb);
save([fetdir,num2str(i),'.mat'], 'feature');
end
function edgehist = CBIR_edgehist(rgb)
imgsize = size(lum);
i0=round(0.05*imgsize(1)); i1=round(0.95*imgsize(1));
j0=round(0.05*imgsize(2)); j1=round(0.95*imgsize(2));
lum = lum(i0:i1, j0:j1);
imgsize = size(lum);
edgemask(:,:,1) = [1 -1; 1 -1];
edgemask(:,:,4) = [1 1; -1 -1];
edgemask(:,:,2) = [sqrt(2) 0; 0 -sqrt(2)];
edgemask(:,:,3) = [0 sqrt(2); -sqrt(2) 0];
edgemask(:,:,5) = [2 -2; -2 2];
TH = 100*ones(1,5);
edgedir = [{'0^o'} {'45^o'} {'-45^o'} {'90^o'} {'iso'}];
for i = 1:5
gradient = filter2(edgemask(:,:,i), lum, 'same');
edgehist(i) = sum(abs(gradient(:))>TH(i));
end
edgehist = edgehist/prod(imgsize);
function edgehist = CBIR_edgedirection(rgb)
imgsize = size(lum);
i0=round(0.05*imgsize(1)); i1=round(0.95*imgsize(1));
j0=round(0.05*imgsize(2)); j1=round(0.95*imgsize(2));
lum = lum(i0:i1, j0:j1);
imgsize = size(lum);
sobel_y = [1 0 -1; 2 0 -2; 1 0 -1];
sobel_x = [1 2 1; 0 0 0; -1 -2 -1];
g_x = filter2(sobel_x, lum, 'same');
g_x = g_x + (g_x==0)*1e-6;
g_y = filter2(sobel_y, lum, 'same');
tg = g_y./g_x;
edgemap = edge(lum, 'sobel');
TH = [ tan(pi/8), tan(pi*3/8), tan(-pi/8), Inf; ... tan(-pi/8), tan(pi/8), tan(-pi*3/8), tan(pi*3/8)];
edgedir = [0 45 -45 90];
for i = 1:4
if i~=4
directmap = (tg<TH(1,i) & tg>=TH(2,i));
else
directmap = ((abs(tg)<TH(1,i)) & (abs(tg)>=TH(2,i)));
end
edgehist(i) = sum(sum(directmap & edgemap));
end
edgehist = edgehist/prod(imgsize);
function colorhist = CBIR_colorhist(rgb)
H_BITS = 4;
S_BITS = 2;
V_BITS = 2;
hsv = uint8(255*rgb2hsv(rgb));
imgsize = size(hsv);
i0=round(0.05*imgsize(1)); i1=round(0.95*imgsize(1));
j0=round(0.05*imgsize(2)); j1=round(0.95*imgsize(2));
hsv = hsv(i0:i1, j0:j1, :);
for i = 1 : 2^H_BITS
for j = 1 : 2^S_BITS
for k = 1 : 2^V_BITS
colorhist(i,j,k) = sum(sum( ...
bitshift(hsv(:,:,1),-(8-H_BITS))==i-1 &...
bitshift(hsv(:,:,2),-(8-S_BITS))==j-1 &...
bitshift(hsv(:,:,3),-(8-V_BITS))==k-1 ));
end
end
end
colorhist = reshape(colorhist, 1, 2^(H_BITS+S_BITS+V_BITS));
colorhist = colorhist/sum(colorhist);
0 Comments
Answers (0)
See Also
Categories
Find more on Ceramics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!