基于Seam+Carving和显著性分析的图像缩放方法MATLAB仿真
本課題的主要工作是使用seam+carving算法對圖像進行非等比例縮放以及無縫拼接,關于seam+caring算法的理論,這里不再重復,主要見如下的參考文獻(已經提供)。
下面介紹本系統的主要操作方法以及對應的函數說明:
打開圖片:
選擇圖片:
?
得到如下的仿真結果:
下面開始非等比例縮放:
從上面的圖中,你可以看到人物的大小基本不變,然偶人物上方的藍天的背景基本被縮小了。這個就是非等比例縮放的效果.
?
無縫拼接,利用seam-curing算法對圖片進行裁剪。
部分代碼如下:
func_addpath();
[FileName,PathName] = uigetfile('*.*','選擇圖像');
FullPathName ? ? ? ?= [PathName,'\',FileName];
Image_RGB ? ? ? ? ? = imread(FullPathName);
%保存圖片的相關參數信息到句柄函數handles中
handles.Image_RGB = ?double(Image_RGB)/255;
handles.Idata ? ? = (handles.Image_RGB);
handles.dispX ? ? = ?handles.Idata;
handles.rows ? ? ?= size(handles.Idata,1);
handles.cols ? ? ?= size(handles.Idata,2);
handles.dim ? ? ? = size(handles.Idata,3);
handles.Engry ? ? = func_gradient(handles.Idata);
handles.dispE ? ? = handles.Engry;
handles.dispS ? ? = zeros(handles.rows, handles.cols);
figure(1);
imshow(Image_RGB);
set(handles.edit1,'String',num2str(handles.cols));
set(handles.edit2,'String',num2str(handles.rows));
axes(handles.axes3);
imshow(Image_RGB);
guidata(hObject,handles);
%垂直Seam縮小
function RemVerSeam_Callback(hObject, eventdata, handles)
% hObject ? ?handle to RemVerSeam (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
func_addpath();
if(handles.rows>1&&handles.cols>1)
? ??
? ? %根據梯度信息計算Seam
? ? handles.seam ? ? ? = func_find_seam(handles.Engry);
? ? handles.SeamVector = func_all_seam(handles.seam);
? ? %刪除seam
? ? handles.Idata ? ? ?= func_ReSeam(handles.Idata,handles.SeamVector);
? ? handles.Engry ? ? ?= func_ReSeam(handles.Engry,handles.SeamVector);
? ? handles.seam ? ? ? = func_ReSeam(handles.seam,handles.SeamVector);
? ??
? ? %更新操作后的圖像大小
? ? handles.rows ? ? ?= size(handles.Idata,1);
? ? handles.cols ? ? ?= size(handles.Idata,2);
? ? handles.dim ? ? ? = size(handles.Idata,3);
? ??
? ? set(handles.edit1,'String',num2str(handles.cols));
? ? set(handles.edit2,'String',num2str(handles.rows));
? ??
? ? handles.dispX ? ? = func_seam_view(handles.Idata , handles.SeamVector);
? ? handles.dispE ? ? = func_seam_view(handles.Engry , handles.SeamVector);
? ? handles.dispS ? ? = func_seam_view(handles.seam ?, handles.SeamVector);
? ? figure(1);
? ? imshow(handles.dispX);
? ??
? ? guidata(hObject,handles);
end
%水平Seam縮小
function RemHorizSeam_Callback(hObject, eventdata, handles)
% hObject ? ?handle to RemHorizSeam (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
func_addpath();
if (handles.rows>1&&handles.cols>1)
? ? handles.Idata ? ? ?= permute(handles.Idata,[2,1,3]);%旋轉
? ? handles.Engry ? ? ?= handles.Engry.';
? ? handles.seam ? ? ? = func_find_seam(handles.Engry);
? ? handles.SeamVector = func_all_seam(handles.seam);
? ? handles.Idata ? ? ?= func_ReSeam(handles.Idata , handles.SeamVector);
? ? handles.Engry ? ? ?= func_ReSeam(handles.Engry , handles.SeamVector);
? ? handles.seam ? ? ? = func_ReSeam(handles.seam ?, handles.SeamVector);
? ??
? ? handles.Idata ? ? ?= permute(handles.Idata,[2,1,3]);
? ? handles.Engry ? ? ?= handles.Engry.';
? ? handles.seam ? ? ? = handles.seam.';
? ??
? ? handles.dispX ? ? ?= permute(func_seam_view(permute(handles.Idata,[2,1,3]),handles.SeamVector),[2,1,3]);
? ? handles.dispE ? ? ?= func_seam_view(handles.Engry.',handles.SeamVector).';
? ? handles.dispS ? ? ?= func_seam_view(handles.seam.',handles.SeamVector).';
? ? %更新操作后的圖像大小
? ? handles.rows ? ? ?= size(handles.Idata,1);
? ? handles.cols ? ? ?= size(handles.Idata,2);
? ? handles.dim ? ? ? = size(handles.Idata,3);
? ??
? ? set(handles.edit1,'String',num2str(handles.cols));
? ? set(handles.edit2,'String',num2str(handles.rows));
? ??
? ? figure(1);
? ? imshow(handles.dispX);
? ? guidata(hObject,handles);
end
%自動縮放
function Resize_Callback(hObject, eventdata, handles)
% hObject ? ?handle to Resize (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
func_addpath();
%獲得需要變換的圖像的像素大小值
%獲得需要變換的圖像的像素大小值
newcols = str2double(get(handles.edit1,'String'));
newrows = str2double(get(handles.edit2,'String'));
Rcols ? = handles.cols-newcols;
Rrows ? = handles.rows-newrows;
?
if Rcols>0
? ? clear M
? ? M ? ? ? ? ? ? ? ? = func_removal(handles.Idata,Rcols);
? ? handles.Idata ? ? = func_ReSeam(handles.Idata,M);
? ? handles.Engry ? ? = func_ReSeam(handles.Engry,M);
? ? handles.seam ? ? ?= func_find_seam(handles.Engry);
? ??
? ? %更新操作后的圖像大小
? ? handles.rows ? ? ?= size(handles.Idata,1);
? ? handles.cols ? ? ?= size(handles.Idata,2);
? ? handles.dim ? ? ? = size(handles.Idata,3); ? ?
?
elseif Rcols<0
? ? clear M
? ? M ? ? ? ? ? ? ? ? = func_removal(handles.Idata,abs(Rcols));
? ? handles.Idata ? ? = func_new_Seam(handles.Idata,M);
? ? handles.Engry ? ? = func_new_Seam(handles.Engry,M);
? ? handles.seam ? ? ?= func_find_seam(handles.Engry);
? ? %更新操作后的圖像大小
? ? handles.rows ? ? ?= size(handles.Idata,1);
? ? handles.cols ? ? ?= size(handles.Idata,2);
? ? handles.dim ? ? ? = size(handles.Idata,3); ?
end
if Rrows>0
? ? clear M
? ? Y ? ? ? ? ? ? ? ? = permute(handles.Idata,[2,1,3]);
? ? M ? ? ? ? ? ? ? ? = func_removal(Y,Rrows);
? ? handles.Idata ? ? = permute(func_ReSeam(Y,M),[2,1,3]);
? ? handles.Engry ? ? = func_ReSeam(handles.Engry.',M).';
? ? handles.seam ? ? ?= func_find_seam(handles.Engry);
? ? %更新操作后的圖像大小
? ? handles.rows ? ? ?= size(handles.Idata,1);
? ? handles.cols ? ? ?= size(handles.Idata,2);
? ? handles.dim ? ? ? = size(handles.Idata,3); ?
elseif Rrows<0
? ? clear M
? ? Y ? ? ? ? ? ? ? ? = permute(handles.Idata,[2,1,3]);
? ? M ? ? ? ? ? ? ? ? = func_removal(Y,abs(Rrows));
? ? handles.Idata ? ? = permute(func_new_Seam(Y,M),[2,1,3]);
? ? handles.Engry ? ? = func_new_Seam(handles.Engry.',M).';
? ? handles.seam ? ? ?= func_find_seam(handles.Engry);
? ? %更新操作后的圖像大小
? ? handles.rows ? ? ?= size(handles.Idata,1);
? ? handles.cols ? ? ?= size(handles.Idata,2);
? ? handles.dim ? ? ? = size(handles.Idata,3); ?
end
handles.dispX ? ? = handles.Idata;
handles.dispE ? ? = handles.Engry;
handles.dispS ? ? = handles.seam;
%更新操作后的圖像大小
handles.rows ? ? ?= size(handles.Idata,1);
handles.cols ? ? ?= size(handles.Idata,2);
handles.dim ? ? ? = size(handles.Idata,3); ?
figure(1);
imshow(handles.dispX);
set(handles.edit1,'String',num2str(handles.cols));
set(handles.edit2,'String',num2str(handles.rows));
guidata(hObject,handles);
%系統復位,圖像還原
function Reset_Callback(hObject, eventdata, handles)
func_addpath();
handles.Idata = handles.Image_RGB;
handles.dispX =handles.Idata;
handles.rows ?= size(handles.Idata,1);
handles.cols ?= size(handles.Idata,2);
handles.dim ? = size(handles.Idata,3);?
handles.Engry = func_gradient(handles.Idata);
handles.dispE = handles.Engry;
handles.dispS = zeros(handles.rows,handles.cols);
figure(1);
imshow(handles.dispX);
set(handles.edit1,'String',num2str(handles.cols));
set(handles.edit2,'String',num2str(handles.rows));
guidata(hObject,handles);
% --------------------------------------------------------------------
function Open_Callback(hObject, eventdata, handles)
% hObject ? ?handle to OpenImg (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function File_Callback(hObject, eventdata, handles)
% hObject ? ?handle to File (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
function edit1_Callback(hObject, eventdata, handles)
% hObject ? ?handle to edit1 (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,'String') returns contents of edit1 as text
% ? ? ? ?str2double(get(hObject,'String')) returns contents of edit1 as a double
%?
% NewVal = get(hObject,'String');
% handles.edit1 = NewVal;
% guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject ? ?handle to edit1 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% ? ? ? See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
? ? set(hObject,'BackgroundColor','white');
end
function edit2_Callback(hObject, eventdata, handles)
% hObject ? ?handle to edit2 (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,'String') returns contents of edit2 as text
% ? ? ? ?str2double(get(hObject,'String')) returns contents of edit2 as a double
%?
% NewVal = str2double(get(hObject,'String'));
% handles.edit2 = NewVal;
% guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject ? ?handle to edit2 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% ? ? ? See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
? ? set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject ? ?handle to pushbutton8 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
func_addpath();
clc;
clear all;
close all;
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject ? ?handle to pushbutton9 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
func_addpath();
a = str2double(get(handles.edit5,'String'));
b = str2double(get(handles.edit6,'String'));
handles.Idata = handles.Image_RGB;
handles.dispX = handles.Idata;
handles.rows ?= size(handles.Idata,1);
handles.cols ?= size(handles.Idata,2);
handles.dim ? = size(handles.Idata,3);?
Images1 ? ? ? = handles.Idata(1:a,:,:);
Images2 ? ? ? = handles.Idata(a+1:handles.rows,:,:);
Rcols ? = 0;
Rrows ? = round(b/2);
?
if Rrows>0
? ? clear M
? ? Y ? ? ? ? ? ? ? ? = permute(Images1,[2,1,3]);
? ? M ? ? ? ? ? ? ? ? = func_removal(Y,Rrows);
? ? Images1 ? ? ? ? ? = permute(func_ReSeam(Y,M),[2,1,3]);
? ? handles.Engry ? ? = func_ReSeam(handles.Engry.',M).';
? ? handles.seam ? ? ?= func_find_seam(handles.Engry);
? ? %更新操作后的圖像大小
? ? handles.rows ? ? ?= size(Images1,1);
? ? handles.cols ? ? ?= size(Images1,2);
? ? handles.dim ? ? ? = size(Images1,3); ?
elseif Rrows<0
? ? clear M
? ? Y ? ? ? ? ? ? ? ? = permute(Images1,[2,1,3]);
? ? M ? ? ? ? ? ? ? ? = func_removal(Y,abs(Rrows));
? ? Images1 ? ? ? ? ? = permute(func_new_Seam(Y,M),[2,1,3]);
? ? handles.Engry ? ? = func_new_Seam(handles.Engry.',M).';
? ? handles.seam ? ? ?= func_find_seam(handles.Engry);
? ? %更新操作后的圖像大小
? ? handles.rows ? ? ?= size(Images1,1);
? ? handles.cols ? ? ?= size(Images1,2);
? ? handles.dim ? ? ? = size(Images1,3); ?
end
if Rrows>0
? ? clear M
? ? Y ? ? ? ? ? ? ? ? = permute(Images2,[2,1,3]);
? ? M ? ? ? ? ? ? ? ? = func_removal(Y,Rrows);
? ? Images2 ? ? ? ? ? = permute(func_ReSeam(Y,M),[2,1,3]);
? ? handles.Engry ? ? = func_ReSeam(handles.Engry.',M).';
? ? handles.seam ? ? ?= func_find_seam(handles.Engry);
? ? %更新操作后的圖像大小
? ? handles.rows ? ? ?= size(Images2,1);
? ? handles.cols ? ? ?= size(Images2,2);
? ? handles.dim ? ? ? = size(Images2,3); ?
elseif Rrows<0
? ? clear M
? ? Y ? ? ? ? ? ? ? ? = permute(Images2,[2,1,3]);
? ? M ? ? ? ? ? ? ? ? = func_removal(Y,abs(Rrows));
? ? Images2 ? ? ? ? ? = permute(func_new_Seam(Y,M),[2,1,3]);
? ? handles.Engry ? ? = func_new_Seam(handles.Engry.',M).';
? ? handles.seam ? ? ?= func_find_seam(handles.Engry);
? ? %更新操作后的圖像大小
? ? handles.rows ? ? ?= size(Images2,1);
? ? handles.cols ? ? ?= size(Images2,2);
? ? handles.dim ? ? ? = size(Images2,3); ?
end
axes(handles.axes4);
finals(:,:,1) = [Images1(:,:,1);Images2(:,:,1)];
finals(:,:,2) = [Images1(:,:,2);Images2(:,:,2)];
finals(:,:,3) = [Images1(:,:,3);Images2(:,:,3)];
imshow(finals);
guidata(hObject,handles);
% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
% hObject ? ?handle to pushbutton10 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
func_addpath();
a = str2double(get(handles.edit3,'String'));
b = str2double(get(handles.edit4,'String'));
handles.Idata = handles.Image_RGB;
handles.dispX =handles.Idata;
handles.rows ?= size(handles.Idata,1);
handles.cols ?= size(handles.Idata,2);
handles.dim ? = size(handles.Idata,3);?
Images1 ? ? ? = handles.Idata(:,1:a,:);
Images2 ? ? ? = handles.Idata(:,a+1:handles.cols,:);
Rcols ? = round(b/2);
Rrows ? = 0;
?
if Rcols>0
? ? clear M
? ? M ? ? ? ? ? ? ? ? = func_removal(Images1,Rcols);
? ? Images1 ? ? ? ? ? = func_ReSeam(Images1,M);
? ? handles.Engry ? ? = func_ReSeam(handles.Engry,M);
? ? handles.seam ? ? ?= func_find_seam(handles.Engry);
? ??
? ? %更新操作后的圖像大小
? ? handles.rows ? ? ?= size(Images1,1);
? ? handles.cols ? ? ?= size(Images1,2);
? ? handles.dim ? ? ? = size(Images1,3); ? ?
?
elseif Rcols<0
? ? clear M
? ? M ? ? ? ? ? ? ? ? = func_removal(Images1,abs(Rcols));
? ? Images1 ? ? ? ? ? = func_new_Seam(Images1,M);
? ? handles.Engry ? ? = func_new_Seam(handles.Engry,M);
? ? handles.seam ? ? ?= func_find_seam(handles.Engry);
? ? %更新操作后的圖像大小
? ? handles.rows ? ? ?= size(Images1,1);
? ? handles.cols ? ? ?= size(Images1,2);
? ? handles.dim ? ? ? = size(Images1,3); ?
end
if Rcols>0
? ? clear M
? ? M ? ? ? ? ? ? ? ? = func_removal(Images2,Rcols);
? ? Images2 ? ? ? ? ? = func_ReSeam(Images2,M);
? ? handles.Engry ? ? = func_ReSeam(handles.Engry,M);
? ? handles.seam ? ? ?= func_find_seam(handles.Engry);
? ??
? ? %更新操作后的圖像大小
? ? handles.rows ? ? ?= size(Images2,1);
? ? handles.cols ? ? ?= size(Images2,2);
? ? handles.dim ? ? ? = size(Images2,3); ? ?
?
elseif Rcols<0
? ? clear M
? ? M ? ? ? ? ? ? ? ? = func_removal(Images2,abs(Rcols));
? ? Images2 ? ? ? ? ? = func_new_Seam(Images2,M);
? ? handles.Engry ? ? = func_new_Seam(handles.Engry,M);
? ? handles.seam ? ? ?= func_find_seam(handles.Engry);
? ? %更新操作后的圖像大小
? ? handles.rows ? ? ?= size(Images2,1);
? ? handles.cols ? ? ?= size(Images2,2);
? ? handles.dim ? ? ? = size(Images2,3); ?
end
?
A23-04
總結
以上是生活随笔為你收集整理的基于Seam+Carving和显著性分析的图像缩放方法MATLAB仿真的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于FPGA的OLED屏幕开发
- 下一篇: 10.Verilog状态机使用方法