前言
其实数字图像处理并不是我的研究方向,仅仅是因为在开题中简单提到了基于图像的设备安全监控,就这样被导师安排去学习图像处理的相关知识。
Orz……好吧,假期回家还不到两周就这样匆匆忙忙的提前赶回了学校,和实验室三维重建方向的的筒子们一起去上美国罗文大学唐瑛(Ying (Gina) Tang)教授开设的数字图像处理入门课程。这周从周一到周三一共上了三节大课,算是简单了解了图像处理的相关内容吧,课程结束后估计也就是浅尝辄止。
在唐教授的安排下阅读了Oliva, Torrallaba, 和Schyns所发表的论文《Hybrid images》,今天主要写写如何用matlab将论文所提到的图像混合进行代码的实现(估计这是图像处理小白级的内容了吧,大神请无视哈~)
实现的整体思想比较简单,就是对两幅图像分别进行高通和低通滤波,从而将两幅图像融合到一起,同时通过修改截止频率还可以改善融合效果,这样就可以实现在近处看是其中一副图像的效果,而在远处看又是另一幅图像的效果,如下所示:
- 待混合图像1
- 待混合图像2
- 混合完成图像
matlab实现hybrid images
hybrid_images函数的编写
对两幅图像分别进行高通和低通滤波,其中涉及到傅里叶变换和反变换,图像混合函数所在文件hybrid_images.m
如下:
function re=hybrid_images(ImageFile1,ImageFile2,radius_input)
I1=ImageFile1;
I2=ImageFile2;
ImgFileOut='C:\Users\dell\Desktop\hybird_images/out.jpg';
radius = radius_input;
I1_ = fftshift(fft2(double(I1))); %进行傅里叶变换,并将零点移动到中心
I2_ = fftshift(fft2(double(I2)));
[m, n, z]=size(I1);
h = fspecial('gaussian', [m,n], radius);
h = h./max(max(h));
for colorI = 1:3
J_(:,:,colorI) = I1_(:,:,colorI).*(1-h) + I2_(:,:,colorI).*h;
end
J = uint8(real(ifft2(ifftshift(J_)))); %反变换
imwrite(J,ImgFileOut); %图片保存到本地
re=J;
end
GUI设计
GUI关键代码
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im1;
[filename,pathname]=uigetfile({'*.*';'*.bmp';'*.jpg';'*.tif';'*.jpg'},'选择图像');
if isequal(filename,0)||isequal(pathname,0)
errordlg('您还没有选取图片!!','温馨提示');%如果没有输入,则创建错误对话框
return;
else
image=[pathname,filename];%合成路径+文件名
im1=imread(image);%读取图像
set(handles.axes1,'HandleVisibility','ON');%打开坐标,方便操作
axes(handles.axes1);%%使用图像,操作在坐标1
imshow(im1);%在坐标axes1显示原图像
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im2;
[filename,pathname]=uigetfile({'*.*';'*.bmp';'*.jpg';'*.tif';'*.jpg'},'选择图像');
if isequal(filename,0)||isequal(pathname,0)
errordlg('您还没有选取图片!!','温馨提示');%如果没有输入,则创建错误对话框
return;
else
image=[pathname,filename];%合成路径+文件名
im2=imread(image);%读取图像
set(handles.axes2,'HandleVisibility','ON');%打开坐标,方便操作
axes(handles.axes2);%%使用图像,操作在坐标1
imshow(im2);%在坐标axes1显示原图像
end
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
slide_value=get(hObject,'Value');%获取滑块当前值
global im1;
global im2;
radius=slide_value;
set(handles.edit1,'string',num2str(slide_value));
x=hybrid_images(im1,im2,radius);
set(handles.axes5,'HandleVisibility','ON');%打开坐标,方便操作
axes(handles.axes5)
imshow(x);
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
运行程序
paper链接
http://cvcl.mit.edu/hybrid/OlivaTorralb_Hybrid_Siggraph06.pdf