📄 cbir_edgedirection.m
字号:
function edgehist = CBIR_edgedirection(rgb)
% EE6850 HW3, Content-Based Image Retrieval
% CBIR_edgedirection() -- edge direction histogram calculation
% input:
% MxNx3 image data, in RGB
% or MxN luminance channel only
% output:
% 1x5 edgehistogram <== global 5 bins (0, 45, 90, 135, no-oreintation)
% NOT the MPEG-7 edge histogram descriptor:
%
% 10-25-2001, xlx@ee.columbia.edu
DEBUG = 0;
if DEBUG
subplot(231);
imshow(rgb); title('original');
end
% check input
if length(size(rgb)) == 2
% take as luminance directly
lum = rgb;
elseif size(rgb,3) == 3
% convert to luminance
lum = rgb2ycbcr(rgb);
lum = lum(:,:,1);
end
imgsize = size(lum);
% get rid of irrelevant boundaries
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);
if DEBUG
subplot(232);
imshow(lum); title('central portion lum.');
end
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; % for numeric stability
g_y = filter2(sobel_y, lum, 'same');
tg = g_y./g_x;
edgemap = edge(lum, 'sobel');
% threshhold for edge direction quantization:
% upperbound; lowerbound (the last one is for absolute value)
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));
if DEBUG
subplot(2,3,i+2);
imshow(directmap & edgemap);
title([num2str(edgedir(i)) '^o edge #: ' num2str(edgehist(i))]);
end
end
edgehist = edgehist/prod(imgsize);
%sum(edgehist)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -