基于Seam+Carving和显著性分析的图像缩放方法MATLAB仿真
Posted fpga&matlab
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于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仿真的主要内容,如果未能解决你的问题,请参考以下文章
Coursera 算法二 week2 Seam Carving
hdoj 5092 Seam Carving 树塔DP变形 + 路径输出 简单题
基于显著性目标检测的非特定类别图像分割实现以及部署过程(附源码+数据集)
R语言glm拟合logistic回归模型实战:基于glm构建逻辑回归模型及模型系数统计显著性分析每个预测因子对响应变量的贡献