图像检索Matlab程序
读取待检索图片,在图片库里进行检索,可以计算相似度,使用Hu不变矩算法,实现图像检索技术。
以下是完整的MATLAB代码实现:
function ImageRetrievalUsingHuMoments()
% 创建GUI界面
figure('Name', 'Image Retrieval Using Hu Moments', 'NumberTitle', 'off', ...
'Position', [100, 100, 800, 600]);
% 添加控件
uicontrol('Style', 'pushbutton', 'String', 'Load Query Image', ...
'Position', [50, 500, 150, 30], 'Callback', @loadQueryImage);
uicontrol('Style', 'pushbutton', 'String', 'Load Image Database', ...
'Position', [250, 500, 150, 30], 'Callback', @loadImageDatabase);
uicontrol('Style', 'pushbutton', 'String', 'Retrieve Similar Images', ...
'Position', [450, 500, 150, 30], 'Callback', @retrieveImages);
% 轴用于显示图片
axes('Position', [0.1, 0.1, 0.4, 0.7]);
title('Query Image');
axes('Position', [0.5, 0.1, 0.4, 0.7]);
title('Most Similar Image');
% 初始化变量
handles.queryImage = [];
handles.imageDatabase = {};
handles.handles = guidata(gcf);
end
% 加载待检索图片
function loadQueryImage(hObject, eventdata)
[file, path] = uigetfile({'*.jpg;*.png;*.bmp', 'Image Files (*.jpg, *.png, *.bmp)'});
if isequal(file, 0)
return;
end
img = imread(fullfile(path, file));
img = rgb2gray(img); % 转换为灰度图像
axes(findobj('Tag', 'QueryImageAxes'));
imshow(img);
title('Query Image');
handles = guidata(gcf);
handles.queryImage = img;
guidata(gcf, handles);
end
% 加载图片库
function loadImageDatabase(hObject, eventdata)
folder = uigetdir('.', 'Select Image Database Folder');
if isequal(folder, 0)
return;
end
imageFiles = dir(fullfile(folder, '*.jpg')); % 支持更多格式可添加'*.png', '*.bmp'
if isempty(imageFiles)
errordlg('No images found in the selected folder.', 'Error');
return;
end
handles = guidata(gcf);
handles.imageDatabase = {imageFiles.name}';
handles.databasePath = folder;
guidata(gcf, handles);
disp(['Loaded ', num2str(length(handles.imageDatabase)), ' images from the database.']);
end
% 检索相似图片
function retrieveImages(hObject, eventdata)
handles = guidata(gcf);
if isempty(handles.queryImage)
warndlg('Please load a query image first!', 'Warning');
return;
end
if isempty(handles.imageDatabase)
warndlg('Please load an image database first!', 'Warning');
return;
end
% 计算查询图片的Hu不变矩
queryHuMoments = computeHuMoments(handles.queryImage);
% 遍历图片库,计算相似度
minDistance = inf;
mostSimilarImage = [];
for i = 1:length(handles.imageDatabase)
imgFile = fullfile(handles.databasePath, handles.imageDatabase{i});
img = imread(imgFile);
img = rgb2gray(img); % 转换为灰度图像
huMoments = computeHuMoments(img);
% 计算欧几里得距离
distance = norm(queryHuMoments - huMoments);
if distance < minDistance
minDistance = distance;
mostSimilarImage = img;
end
end
% 显示最相似图片
axes(findobj('Tag', 'SimilarImageAxes'));
imshow(mostSimilarImage);
title(['Most Similar Image (Distance: ', num2str(minDistance), ')']);
end
% 计算Hu不变矩
function huMoments = computeHuMoments(img)
% 转换为二值图像
bwImg = imbinarize(img);
% 计算区域属性
stats = regionprops(bwImg, 'Image');
region = stats.Image;
% 计算Hu不变矩
moments = regionprops(region, 'HuMoments');
huMoments = moments.HuMoments; % 返回7个Hu不变矩
huMoments = huMoments(:); % 转换为列向量
end
Load Query Image
按钮选择一张待检索的图片。Load Image Database
按钮选择包含图片的文件夹,程序会自动加载所有支持格式的图片。完成以上步骤后,运行你的MATLAB程序,测试各项功能是否正常工作。希望这个示例能帮助你构建一个完整的基于Hu不变矩的图像检索系统!