該項(xiàng)目為數(shù)字圖像處理課程的期末大項(xiàng)目,主要內(nèi)容是用matlab軟件實(shí)現(xiàn)一些美圖秀秀相關(guān)功能,比如對(duì)圖像進(jìn)行水平垂直翻轉(zhuǎn),旋轉(zhuǎn),裁剪,摳圖等基礎(chǔ)功能;對(duì)圖像添加浮雕,藝術(shù)噪聲,灰度膠片,動(dòng)感模糊,素描,油畫(huà),羽化等濾鏡的功能;以及祛痘祛痣,白牙,大眼,磨皮,美白,增強(qiáng)等美化功能。在實(shí)現(xiàn)以上功能的基礎(chǔ)上,還使用了GUI圖像用戶(hù)界面,增強(qiáng)了用戶(hù)交互式體驗(yàn)感。
GUI界面的一些使用方法可看之前我發(fā)的兩篇博客:
Matlab GUI界面使用方法(一):打開(kāi)GUI&GUI常用控件(最基礎(chǔ))
Matlab GUI界面使用方法(二):GUI控件功能實(shí)現(xiàn)
這是該簡(jiǎn)易版美圖秀秀的GUI界面:
以下是一些功能的原理步驟以及代碼和部分注釋?zhuān)?/p>
1.導(dǎo)入圖像和從攝像頭獲取圖像
原理步驟:
從攝像頭獲?。和ㄟ^(guò)imaqhwinfo函數(shù)獲取攝像頭信息,再在videoinput函數(shù)中創(chuàng)建一個(gè)視頻對(duì)象,用preview函數(shù)創(chuàng)建預(yù)覽窗口,即可完成使用matlab調(diào)用攝像頭獲取圖像了。
從文件夾中選擇圖片:用uigetfile函數(shù)打開(kāi)文件對(duì)話(huà)框,選擇指定格式的圖片,這時(shí)會(huì)返回圖像的名稱(chēng)和路徑信息,如果選擇成功就將編輯文本的string設(shè)置為文件的名稱(chēng)和路徑,并將名稱(chēng)和路徑存儲(chǔ)在handles中,最后在確認(rèn)按鈕中從handles獲取到名稱(chēng)路徑信息,并用imshow函數(shù)讀取即可。
從攝像頭獲?。?% --- Executes on button press in pushbuttoncam.
function pushbuttoncam_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttoncam (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
vid=videoinput('winvideo',1);%創(chuàng)建視頻輸入對(duì)象
h=preview(vid);%顯示視頻輸入對(duì)象
while ishandle(h)
img=getsnapshot(vid);
imshow(img);
end從文件夾選?。?% 主界面選擇圖片按鈕
% --- 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)
[filename, filepath]=uigetfile({'*.png;*.jpg'},'選擇圖片文件');
% 打開(kāi)文件對(duì)話(huà)框,返回相應(yīng)格式文件的名稱(chēng)和路徑
if isequal(filename,0)||isequal(filepath,0)
% 若未選擇到圖片
errouglg('沒(méi)有選中文件,請(qǐng)重新選擇');
% 彈窗提示
return;
else
str=[filepath filename];
set(handles.edit1,'string',str)
% 若選擇成功,就將edit1的string文本設(shè)置為選擇文件的路徑和名稱(chēng)
end
% 復(fù)制文本路徑
road=get(handles.edit1,'string');
% 獲取文本路徑
handles.road=road;
guidata(hObject,handles);
% 存儲(chǔ)路徑
% 主界面圖像確認(rèn)按鈕
% --- Executes on button press in pushbuttonsure.
function pushbuttonsure_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonsure (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes2);%清除第二個(gè)坐標(biāo)軸上的圖像
cla reset;
road=handles.road;%獲取存儲(chǔ)的路徑
axes(handles.axes1);%在第一個(gè)坐標(biāo)軸上顯示原圖像
imshow(road);
2.圖像旋轉(zhuǎn)
原理步驟:
用戶(hù)點(diǎn)擊圖像旋轉(zhuǎn)按鈕就可使圖像逆時(shí)針旋轉(zhuǎn)90度,再點(diǎn)擊一次就在已旋轉(zhuǎn)的基礎(chǔ)上再旋轉(zhuǎn)90度。實(shí)現(xiàn)原理是先用getframe函數(shù)獲取當(dāng)前坐標(biāo)軸的圖像,方便進(jìn)行多次旋轉(zhuǎn)操作,然后將圖像轉(zhuǎn)化為double型調(diào)用matlab自帶的imrotate函數(shù)進(jìn)行圖像旋轉(zhuǎn),并顯示即可。而旋轉(zhuǎn)圖像本質(zhì)上就是將圖像構(gòu)成的矩陣乘一個(gè)旋轉(zhuǎn)矩陣從而得到一個(gè)旋轉(zhuǎn)后的新矩陣。
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-487917.html
功能鍵-圖像旋轉(zhuǎn)
% --- Executes on button press in pushbuttontrun.
function pushbuttontrun_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttontrun (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
img=getframe;%獲取當(dāng)前坐標(biāo)軸的圖像
axes(handles.axes2);%第二個(gè)坐標(biāo)軸
imshow(img.cdata);%顯示圖像
img=img.cdata;%原圖像
imgdou=im2double(img);%將原圖像轉(zhuǎn)化為double類(lèi)型
img2=imrotate(imgdou,90);%用imrotate函數(shù)將圖像選擇90度
axes(handles.axes2);%第二個(gè)坐標(biāo)軸
imshow(img2);%顯示旋轉(zhuǎn)后的圖像
3.圖像水平豎直旋轉(zhuǎn)
原理步驟:
點(diǎn)擊水平或豎直翻轉(zhuǎn)按鈕可使圖片左右或上下翻轉(zhuǎn),實(shí)現(xiàn)原理是利用fliplr函數(shù)會(huì)將矩陣圍繞垂直軸左右方向翻轉(zhuǎn)各其列,flipud函數(shù)圍繞矩陣水平軸上下方向翻轉(zhuǎn)各行,fliper函數(shù)和flipud函數(shù)處理多維數(shù)組時(shí)均只處理前兩個(gè)維度構(gòu)成的平面,所以可以對(duì)圖像每個(gè)通道都進(jìn)行一次翻轉(zhuǎn)。
?
功能鍵-水平翻轉(zhuǎn)
% --- Executes on button press in pushbuttonfanzhuan.
function pushbuttonfanzhuan_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonfanzhuan (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
img=getframe;%獲取當(dāng)前坐標(biāo)軸的圖像
axes(handles.axes2);%第二個(gè)坐標(biāo)軸
imshow(img.cdata);%顯示圖像
img=img.cdata;%原圖像
for k=1:3
img2(:,:,k)=fliplr(img(:,:,k));%將每一個(gè)通道都進(jìn)行翻轉(zhuǎn)
end
axes(handles.axes2);
imshow(img2);%顯示水平翻轉(zhuǎn)后的圖像
功能鍵-豎直翻轉(zhuǎn)
% --- Executes on button press in pushbuttonshufanhzuan.
function pushbuttonshufanhzuan_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonshufanhzuan (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA).
img=getframe;%獲取當(dāng)前坐標(biāo)軸的圖像
axes(handles.axes2);%第二個(gè)坐標(biāo)軸
imshow(img.cdata);%顯示圖像
img=img.cdata;%原圖像
for k=1:3
img2(:,:,k)=flipud(img(:,:,k));%將每一個(gè)通道都進(jìn)行翻轉(zhuǎn)
end
axes(handles.axes2);
4.裁剪
原理步驟:
用戶(hù)根據(jù)交互式裁剪區(qū)域選擇裁剪圖像,右鍵crop image就可在新窗口顯示裁剪的圖像?;驹硎牵涸谶x取裁剪區(qū)域的時(shí)候,使用的是自帶函數(shù)imcrop函數(shù),它可以返回用戶(hù)繪制的選擇區(qū)域的大小以及各個(gè)點(diǎn)的像素值J和矩形的位置向量rect[xmin ymin width height],其中(xmin,ymin)表示矩形區(qū)域左上角坐標(biāo),width,height表示矩形區(qū)域的寬和高,這里要實(shí)現(xiàn)圖片裁剪功能,只需返回J并顯示即可。但此函數(shù)的rect在實(shí)現(xiàn)后面一些功能的時(shí)候有用到。
?
?
% 功能鍵-圖像裁剪
% --- Executes on button press in pushbuttoncut.
function pushbuttoncut_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttoncut (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
road=handles.road;%讀取原圖像
img=imread(road);
[J,rect]=imcrop(img);%打開(kāi)裁剪工具,rect為繪制的矩形四像素位置向量
axes(handles.axes2);%顯示選擇的裁剪區(qū)域
imshow(J);
5.添加文字
原理步驟:
用戶(hù)在添加文字后面的輸入框中輸入需要添加的文字,并點(diǎn)擊添加文字按鈕,再在圖像中選取添加文字的位置,crop image后即可實(shí)現(xiàn)添加文字效果。實(shí)現(xiàn)原理是我們可以通過(guò)text函數(shù)像圖片添加文字,需要的參數(shù)有顯示文字的位置坐標(biāo)以及顯示的文字,顯示的文字可以通過(guò)用戶(hù)在edittext輸入框中輸入,然后get輸入的文字即可,但是由于用戶(hù)想要添加文字的位置不唯一,所以不能我們指定位置,需要用戶(hù)指定位置,這時(shí),上個(gè)功能鍵裁剪imcrop函數(shù)返回的rect就有用了,用戶(hù)可以自主選擇需要添加文字的地方,imcrop函數(shù)會(huì)返回rect,利用rect返回的前兩個(gè)參數(shù)作為text函數(shù)顯示文字位置的參數(shù),這樣就可以實(shí)現(xiàn)該功能了。
?
?
功能鍵-添加文字
% --- Executes on button press in pushbuttontext.
function pushbuttontext_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttontext (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
img=getframe;%獲取當(dāng)前坐標(biāo)軸的圖像
img=img.cdata;%原圖像
axes(handles.axes2);%先將原圖像顯示在第二個(gè)坐標(biāo)軸上
imshow(img);
str=get(handles.edit2,'string');%先獲取輸入的文字
[J,rect]=imcrop(img);%選取文字框
posx=round(rect(1));%返回位置
posy=round(rect(2));
text(posx,posy,str);%添加文本
6.增強(qiáng)
圖像增強(qiáng)就是圖像的亮度和飽和度對(duì)比要增強(qiáng),hsv顏色空間中顏色參數(shù)H代表色調(diào),用角度度量,不同角度代表不同顏色;S代表飽和度,表示顏色接近光譜色的程度,值越大,飽和度越高,顏色就越深艷;V代表明度,就是顏色的明暗程度。因此就可以將原圖像的rgb顏色空間轉(zhuǎn)化為hsv顏色空間,通過(guò)調(diào)整圖像的飽和度通道和明度通道就可實(shí)現(xiàn)圖像增強(qiáng)效果。
?
功能鍵-增強(qiáng)
% --- Executes on button press in pushbuttonmeibai.
function pushbuttonmeibai_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonmeibai (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
road=handles.road;%讀取原圖像
RGB=imread(road);
HSV=rgb2hsv(RGB);%將原圖像rgb轉(zhuǎn)化成hsv通道
H=HSV(:,:,1);
S=HSV(:,:,2);
V=HSV(:,:,3);
V=imadjust(V);%對(duì)明度通道進(jìn)行增強(qiáng)
S=imadjust(S);%對(duì)飽和度通道進(jìn)行增強(qiáng)
HSV(:,:,1)=H;
HSV(:,:,2)=S;
HSV(:,:,3)=V;
img2=hsv2rgb(HSV);%再將hsv轉(zhuǎn)回rgb
axes(handles.axes2);%顯示圖像
imshow(img2);
7.磨皮
原理步驟:
磨皮就是為了去除皮膚上的瑕疵,如不均勻的色差等,通過(guò)磨皮可以使皮膚看上去光滑,平整。所以可以用濾波器對(duì)圖像進(jìn)行處理,由于高斯濾波沒(méi)有考慮到圖像邊緣,所以會(huì)將圖像中的邊緣也一并模糊,效果不太好,所以可以采用雙邊濾波,它能夠同時(shí)達(dá)到保邊和去噪的效果。雙邊濾波的原理和高斯濾波相似,它將圖像中每個(gè)像素的灰度值替換為相鄰像素灰度值的加權(quán)平均值,權(quán)重取決于灰度差異和像素點(diǎn)之間的歐式距離。根據(jù)這個(gè)原理,就可在matlab中用雙邊濾波器處理圖像了,即先構(gòu)造高斯濾波器,再遍歷需卷積的區(qū)域,得到灰度差值矩陣,并用高斯函數(shù)處理為權(quán)重矩陣,差值越大,權(quán)重越小,接著用權(quán)重矩陣與高斯濾波器相乘,就可得到雙邊濾波器,并將權(quán)值和化為一,最后用雙邊濾波器進(jìn)行卷積求和即可。為了實(shí)現(xiàn)該功能,還需在功能鍵中用雙邊濾波器依次處理三個(gè)色彩通道,至此,磨皮效果就能實(shí)現(xiàn)了。
?
?
功能鍵-磨皮
% --- Executes on button press in pushbuttonmopi.
function pushbuttonmopi_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonmopi (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
road=handles.road;%讀取原圖像
img0=imread(road);
tempsize = 5;%高斯濾波器大小
sigma1 = 5;%高斯濾波器標(biāo)準(zhǔn)差
sigma2 = 0.08;%灰度敏感性
img = double(padarray(img0,[tempsize,tempsize],0))/255;%模板補(bǔ)零,避免出現(xiàn)黑邊
imgr = img(:,:,1);%提取出三個(gè)通道
imgg = img(:,:,2);
imgb = img(:,:,3);
[m,n] = size(imgr);%獲取大小
img(:,:,1) = B_filter(imgr,tempsize,sigma1,sigma2);%濾波器依次處理三個(gè)通道
img(:,:,2) = B_filter(imgg,tempsize,sigma1,sigma2);
img(:,:,3) = B_filter(imgb,tempsize,sigma1,sigma2);
g=img(tempsize+1:m-tempsize,tempsize+1:n-tempsize,:);%只顯示彩色區(qū)域,去除黑邊
axes(handles.axes2);%顯示效果圖
imshow(g);
雙邊濾波器函數(shù)
function out = B_filter(Img,tempsize,sigma0,sigma1)
%模板定義
gauss = fspecial('gauss',2*tempsize+1,sigma0);%構(gòu)造高斯濾波器
[m,n] = size(Img);%獲取圖像大小
for i = 1+ tempsize : m - tempsize%遍歷需要卷積區(qū)域
for j = 1+ tempsize : n - tempsize
%提取處理區(qū)域得到灰度差值矩陣
temp = abs(Img(i - tempsize:i + tempsize,j - tempsize:j + tempsize) - Img(i,j));%調(diào)整因子
temp = exp(-temp.^2/(2*sigma1^2));%像素值權(quán)重
%得到雙邊濾波器并將權(quán)值和化為一
filter = gauss.*temp;
filter = filter/sum(filter(:));
%用雙邊濾波器進(jìn)行卷積運(yùn)算
Img(i,j) = sum(sum((Img(i - tempsize:i + tempsize,j - tempsize:j + tempsize).*filter)));
end
end
out = Img;
end
8.美白
原理步驟:
將彩色RGB圖像轉(zhuǎn)化為L(zhǎng)*a*b*顏色空間,其中L表示圖像亮度層,a表示紅綠層,b表示黃藍(lán)層,為了實(shí)現(xiàn)美白效果,只需對(duì)亮度層進(jìn)行操作,由于亮度層的亮度值范圍是0-100,先將亮度值歸一化再用imadjust函數(shù)增強(qiáng)圖像亮度值,乘100之后返回RGB顏色空間,只影響了像素的強(qiáng)度,會(huì)保留圖像原始顏色,從而達(dá)到美白的效果。
?
功能鍵-美白
% --- Executes on button press in pushbuttonzengqiang.
function pushbuttonzengqiang_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonzengqiang (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
road=handles.road;%讀取原圖像
img=imread(road);
img2=rgb2lab(img);%將RGB轉(zhuǎn)換為L(zhǎng)*a*b* L表示亮暗,A表示紅綠,B表示黃藍(lán)
max_lum=100;%亮度值的范圍是0-100
L=img2(:,:,1)/max_lum;%將亮度值歸一化
img2(:,:,1)=imadjust(L)*max_lum;%增強(qiáng)亮度通道的值
img3=lab2rgb(img2);%將圖像轉(zhuǎn)回RGB顏色空間
axes(handles.axes2);%顯示效果圖像
imshow(img3);
9.祛痘祛痣
原理步驟:
用戶(hù)自主選擇需要祛痘祛痣的地方,然后右鍵crate mask就可顯示處理后的效果,實(shí)現(xiàn)過(guò)程是通過(guò)roipoly函數(shù)選取痘痘范圍作為蒙版,然后將蒙版和原圖像的三個(gè)通道進(jìn)行相乘,再將這三個(gè)通道連接,得到選取區(qū)域的圖像。遍歷某一個(gè)通道,找到第一個(gè)像素不為0的點(diǎn),獲取該像素值作為采樣得到的像素值,在該像素值±5個(gè)像素值區(qū)間內(nèi)獲取隨機(jī)數(shù)產(chǎn)生填充圖像并分別對(duì)三個(gè)通道進(jìn)行處理,再將三個(gè)通道連接起來(lái)得到處理后的選取區(qū)域圖像,最后只需將兩部分圖像想加即可。
?
?
功能鍵-祛痘祛痣
% --- Executes on button press in pushbuttonqudou.
function pushbuttonqudou_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonqudou (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
img=getframe;%獲取當(dāng)前坐標(biāo)軸的圖像
img=img.cdata;%原圖像
img2=qudou(img);%調(diào)用祛痘函數(shù)
axes(handles.axes2);%顯示效果圖像
imshow(img2);
祛痘祛痣函數(shù):
function [ f ] = qudou(img)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
img2= img;
[ M,N,~ ] = size(img);%得到原圖像的大小
%進(jìn)行交互選擇處理區(qū)域
mask = roipoly( img2 );%roipoly函數(shù)選取指定多邊形
x1 = immultiply( mask,img2( :,:,1 ) );%將選取的范圍和原圖通道相乘
x2 = immultiply( mask,img2( :,:,2 ) );
x3 = immultiply( mask,img2( :,:,3 ) );
x = cat( 3,x1,x2,x3 );%將三個(gè)通道連接在一起,得到選取的部分圖像
f1 = zeros( M,N );%f1,f2,f3存儲(chǔ)三個(gè)通道的運(yùn)算結(jié)果
f2 = zeros( M,N );
f3 = zeros( M,N );
%找到第一個(gè)像素值不為0的點(diǎn),得到該點(diǎn)像素值,作為采樣后填充的像素
for i = 1:M
for j = 1:N
if( x1( i,j ) ~= 0 )
r = x( i,j,: );%得到采樣的像素值
end
end
end
%隨機(jī)產(chǎn)生填充圖像
y = zeros(3,3,3);
y( :,:,1 ) = randi([r(1)-5,r(1)+5],[3,3]);%randi函數(shù)-隨機(jī)整數(shù)--在采樣到的±5的區(qū)間中找隨機(jī)數(shù)
y( :,:,2 ) = randi([r(2)-5,r(2)+5],[3,3]);%并返回3*3大小的矩陣
y( :,:,3 ) = randi([r(3)-5,r(3)+5],[3 3]);
%類(lèi)型轉(zhuǎn)換
y = double(y);
%對(duì)于三個(gè)通道分別進(jìn)行處理,用采樣得到的像素點(diǎn)取代原來(lái)的像素點(diǎn)
for i = 2:3:M-1
for j = 2:3:N-1
f1( i-1:i+1,j-1:j+1 ) = mask( i-1:i+1,j-1:j+1 ).* y( :,:,1 );
f2( i-1:i+1,j-1:j+1 ) = mask( i-1:i+1,j-1:j+1 ).* y( :,:,2 );
f3( i-1:i+1,j-1:j+1 ) = mask( i-1:i+1,j-1:j+1 ).* y( :,:,3 );
end
end
f = cat( 3,f1,f2,f3 );%將三個(gè)通道連接在一起,得到選取區(qū)域處理后的圖像
f = uint8( f );%類(lèi)型轉(zhuǎn)換
%得到處理后圖像
a = img2 - x;%原圖中除了選取區(qū)域的圖像
f = f + a;%疊加
end
10.摳圖
原理步驟:
用戶(hù)根據(jù)需要點(diǎn)擊一系列點(diǎn),這些點(diǎn)連接起來(lái)形成的圖像就是需要摳出來(lái)的圖。實(shí)現(xiàn)過(guò)程是先逐個(gè)接受用戶(hù)點(diǎn)擊圖像中某個(gè)點(diǎn)的位置,用line函數(shù)在圖像中顯示連接起來(lái)的線,再生成一個(gè)蒙版,也就是和原圖像同樣大小的全零矩陣,再寫(xiě)一個(gè)具有連線功能的函數(shù),該函數(shù)通過(guò)取整確定線段覆蓋的像素位置并將其賦值為1,用這個(gè)函數(shù)將這些點(diǎn)逐個(gè)連接起來(lái),并讓收尾相連,此時(shí)該矩陣就一條封閉的曲線了,接著填充這個(gè)閉合的曲線,蒙版就變成了只有選取的區(qū)域是白色,其余是黑色,最后將該蒙版與原圖像相乘,即可得到選取的圖像區(qū)域,也就實(shí)現(xiàn)了摳圖的功能。
受祛痘功能的啟發(fā),該算法可以使用matlab自帶函數(shù)roipoly函數(shù)進(jìn)行處理,還會(huì)簡(jiǎn)單方便很多,用roipoly函數(shù)可直接返回蒙版,只需將每個(gè)通道和原圖像相乘再連接起來(lái)就可實(shí)現(xiàn)摳圖功能了。
?
功能鍵-摳圖
% --- Executes on button press in pushbuttonkoutu.
function pushbuttonkoutu_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonkoutu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
road=handles.road;%讀取原圖像
img=imread(road);
im=double(img);
[out,mask,p]=manseg(im);%調(diào)用摳圖函數(shù)
out=uint8(out);%輸出結(jié)果
axes(handles.axes2);%顯示效果圖
imshow(out);
摳圖函數(shù):
function [ out,mask,p ] = manseg( im )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
% 準(zhǔn)備工作
[M,N,D]=size(im);
k=0;
p=[];
% 手動(dòng)選點(diǎn)
hold on
while 1
[x,y,flag]=ginput(1);%返回點(diǎn)擊時(shí)的坐標(biāo)
if flag==1
k=k+1;%計(jì)算共點(diǎn)擊了幾次
plot(x,y,'b.','MarkerSize',20)%繪制選中的這些點(diǎn)
p(k,1:2)=round([y,x]);%四舍五入去整--取反是因?yàn)閳D像像素坐標(biāo)索引與xy坐標(biāo)系相反
if k>1
line([p(k-1,2),p(k,2)],[p(k-1,1),p(k,1)],'LineWidth',2) %將點(diǎn)和上一個(gè)點(diǎn)用2榜直線連起來(lái)
end
else
line([p(1,2),p(k,2)],[p(1,1),p(k,1)],'LineWidth',2) %將點(diǎn)和上一個(gè)點(diǎn)用2榜直線連起來(lái)
break
end
end
hold off
% 生成蒙板
mask=zeros(M,N);%生成一個(gè)和原圖像同樣大小的蒙版
for i=1:k%遍歷每個(gè)選取的點(diǎn)
if i<k
mask=pixelcontect(mask,p(i,:),p(i+1,:));%依次連接所有點(diǎn)
else
mask=pixelcontect(mask,p(i,:),p(1,:));%末尾與起點(diǎn)相連
end
end
mask=imfill(mask,'hole');
if D>1
mask=cat(3,mask,mask,mask);
end
% 提取目標(biāo)
out=mask.*im;% mask類(lèi)型是double,所以輸入的im也改成double,否則報(bào)錯(cuò)類(lèi)型不匹配。
end
具有連線功能的函數(shù):
% 其中 p0,p1為兩個(gè)點(diǎn)的坐標(biāo),a為蒙板
function a=pixelcontect(a,p0,p1)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
a(p0(1),p0(2))=1;%輸入點(diǎn)像素值設(shè)為0
a(p1(1),p1(2))=1;%輸入點(diǎn)像素值設(shè)為0
dis=p1-p0;%dy,dx
gap=((-1).^double(dis<0));%取正
absdis=abs(dis);%取正
more=max(absdis);%變化大的
less=min(absdis);%變化小的
if absdis(1)>=absdis(2)%如果y方向變化比x方向變化大
dir1=[gap(1),0];%向y方向走
dir2=[0,gap(2)];
else
dir2=[gap(1),0];%向x方向走
dir1=[0,gap(2)];
end
lmp=less/more;%斜率
i=0;j=0;
while i<more%將線段覆蓋的點(diǎn)的像素值設(shè)為1
p0=p0+dir1;
a(p0(1),p0(2))=1;
i=i+1;
if i<more
p1=p1-dir1;
a(p1(1),p1(2))=1;
i=i+1;
end
if j/i<lmp%若比斜率小
if j<less
p0=p0+dir2;
a(p0(1),p0(2))=1;
j=j+1;
end
if j<less
p1=p1-dir2;
a(p1(1),p1(2))=1;
j=j+1;
end
end
end
11.白牙
原理步驟:
用戶(hù)通過(guò)選取牙齒部分右鍵create mask就可在效果圖中看到牙齒的效果了,實(shí)現(xiàn)原理受摳圖功能的影響,既然白牙功能的目的是只將牙齒部分美白,其他部分不變,而摳圖可以選取感興趣的區(qū)域圖像f,并只顯示該區(qū)域,其余部分像素值均為0,那么就可以用原圖像減去該區(qū)域得到img2,即除了該區(qū)域的圖像。隨后遍歷f當(dāng)像素值不為0時(shí),即只作用于選取的區(qū)域,用imadd函數(shù)增加其亮度,最后將處理后的f和img2相加,就可以得到只增亮牙齒的圖像了,從而實(shí)現(xiàn)了該功能。
?
?
?
功能鍵-白牙
% --- Executes on button press in pushbuttonwhite.
function pushbuttonwhite_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonwhite (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
road=handles.road;%讀取圖像
img=imread(road);
mask = roipoly(img);%蒙版選擇
[ M,N,~ ] = size(mask);%獲取蒙版大小
x1 = immultiply( mask,img( :,:,1 ) );%將選取的范圍和原圖通道疊加
x2 = immultiply( mask,img( :,:,2 ) );
x3 = immultiply( mask,img( :,:,3 ) );
f = cat( 3,x1,x2,x3 );%將三個(gè)通道連接在一起,得到選取的圖像
img2=img-f;%原圖減去選取的區(qū)域
for i=1:M%遍歷選取的圖像,若像素值不為0,就增亮
for j=1:N
if(f(i,j)~=0)
f(i,j,:)=imadd(f(i,j,:),30);
end
end
end
f=imadd(f,img2);%將選取區(qū)域增亮后和除該區(qū)域的圖像相加
axes(handles.axes2);%顯示效果圖
imshow(f);
12.大眼
原理步驟:
大眼功能就是為了放大眼部,讓眼睛看起來(lái)比之前更大,為了增加交互式體驗(yàn)感,也同時(shí)受到截圖功能中imcrop函數(shù)的影響,所以用戶(hù)可以通過(guò)選擇眼部區(qū)域,特別是眼球區(qū)域,右鍵crop image進(jìn)行放大眼睛。而基本實(shí)現(xiàn)過(guò)程是在用戶(hù)進(jìn)行選區(qū)的時(shí)候會(huì)返回rect即選區(qū)的左上角坐標(biāo)和選取長(zhǎng)寬,通過(guò)這四個(gè)參數(shù),我們就可以得到該選區(qū)的中點(diǎn)位置,將原圖像和選取中點(diǎn)位置以及大眼半徑參數(shù)傳入大眼函數(shù),大眼函數(shù)就可以得到需要放大區(qū)域的上下左右坐標(biāo),遍歷這個(gè)區(qū)域,如果該點(diǎn)像素到中點(diǎn)的距離小于傳入的半徑,那么就等用最近鄰插值算法比例放大,經(jīng)過(guò)這一系列操作就可實(shí)現(xiàn)手動(dòng)眼部放大了。
?
?
功能鍵-大眼
% --- Executes on button press in pushbuttonbigeyes.
function pushbuttonbigeyes_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonbigeyes (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
img=getframe;%獲取當(dāng)前坐標(biāo)軸的圖像
img=img.cdata;
[J,rect]=imcrop(img);%選取框
posx=round(rect(3)/2+rect(2));%選取框中點(diǎn)
posy=round(rect(1)+rect(4)/2);
img2=bigger(img,posx,posy,25);%調(diào)用大眼函數(shù)
axes(handles.axes2);%顯示放大眼睛后的圖像
imshow(img2);
大眼函數(shù):
function [ J ] = bigger(I,pointx,pointy,r)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
%I為原圖像,pointx和pointy為放大中心點(diǎn)坐標(biāo),r為放大半徑
im=I;
left=round(pointy-r);%分別得到放大區(qū)域的上下左右坐標(biāo)
right=round(pointy+r);
top=round(pointx-r);
bottom=round(pointx+r);
space = r * r;%放大區(qū)域面積
strength=25; %放大強(qiáng)度
fr=im(:,:,1);%原圖像為彩色圖像,要分成RGB三個(gè)分量進(jìn)行處理
fg=im(:,:,2);
fb=im(:,:,3);
im2fr=fr;
im2fg=fg;
im2fb=fb;
%插值算法
for x=top:bottom%遍歷需要放大的區(qū)域
offsetx=x-pointx;%當(dāng)前點(diǎn)距中點(diǎn)的在x上的距離
for y=left:right
offsety=y-pointy;
xy=offsetx*offsetx+offsety*offsety;%當(dāng)前點(diǎn)距中點(diǎn)的距離
if xy<=space%若當(dāng)前點(diǎn)在需放大的圓形區(qū)域內(nèi)
%等比例放大
scale=1-xy/space;
scale=1-strength/100*scale;
%posy和posx為放大后坐標(biāo)值
%采用最近鄰插值算法
posy=round(offsety*scale+pointy);
posx=round(offsetx*scale+pointx);
im2fr(x,y)=fr(posx,posy);
im2fg(x,y)=fg(posx,posy);
im2fb(x,y)=fb(posx,posy);
end
end
end
J=cat(3,im2fr,im2fg,im2fb);%將RGB三個(gè)分量整合,得到彩色圖像
end
13.特征點(diǎn)檢測(cè)
原理步驟:
特征點(diǎn)檢測(cè)主要檢測(cè)的是人臉及眼睛,鼻子,嘴巴,這里使用的是matlab自帶的分類(lèi)器進(jìn)行直接檢測(cè),不需要額外的訓(xùn)練。使用時(shí)輸入?yún)?shù)有原圖像,分類(lèi)模型,存儲(chǔ)對(duì)象等,輸出參數(shù)包括檢測(cè)結(jié)果和一個(gè)輸出矩陣。
?
?
?
?
功能鍵-特征點(diǎn)檢測(cè)
% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu3 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu3
num=get(handles.popupmenu3,'value');%獲取下拉列表選擇的序列
switch num
case 1
case 2
%定位人臉
road=handles.road;%讀取原圖像
I=imread(road);
FDetect = vision.CascadeObjectDetector; %創(chuàng)建人臉檢測(cè)器對(duì)象
face_dtect = step(FDetect,I); %檢測(cè)人臉
axes(handles.axes2); %顯示圖像
imshow(I);
hold on
for i = 1:size(face_dtect,1) %遍歷檢測(cè)到的人臉個(gè)數(shù),畫(huà)矩形框
rectangle('Position',face_dtect(i,:),'LineWidth',5,'LineStyle','-','EdgeColor','m');
end
case 3
%定位眼睛
road=handles.road;%讀取原圖像
input_image=imread(road);
detector=vision.CascadeObjectDetector('LeftEyeCART');%創(chuàng)建檢測(cè)眼部檢測(cè)器對(duì)象
eyes=step(detector,input_image);%檢測(cè)眼睛
axes(handles.axes2);%顯示圖像
imshow(input_image);
size_eyes = size(eyes);%眼睛的狂
hold on;
for i =1:size_eyes(1); %遍歷檢測(cè)到的眼睛個(gè)數(shù),畫(huà)矩形框
rectangle('Position',eyes(i,:),'LineWidth',5,'LineStyle','-','EdgeColor','m');
end
case 4
road=handles.road;%讀取原圖像
I=imread(road);
mouth_detect = vision.CascadeObjectDetector('Mouth','MergeThreshold',16); %創(chuàng)建檢測(cè)嘴巴檢測(cè)器對(duì)象
face_dtect=step(mouth_detect,I); %檢測(cè)嘴巴
axes(handles.axes2);%顯示圖像
imshow(I);
hold on
for i = 1:size(face_dtect,1) %遍歷檢測(cè)到的嘴巴個(gè)數(shù),畫(huà)矩形框
rectangle('Position',face_dtect(i,:),'LineWidth',4,'LineStyle','-','EdgeColor','r');
end
case 5
road=handles.road;%讀取原圖像
I=imread(road);
NoseDetect = vision.CascadeObjectDetector('Nose','MergeThreshold',16); %創(chuàng)建檢測(cè)鼻子檢測(cè)器對(duì)象
face_dtect=step(NoseDetect,I); %檢測(cè)鼻子
axes(handles.axes2);%顯示圖像
imshow(I);
hold on
for i = 1:size(face_dtect,1) %遍歷檢測(cè)到的鼻子個(gè)數(shù),畫(huà)矩形框
rectangle('Position',face_dtect(i,:),'LineWidth',4,'LineStyle','-','EdgeColor','m');
end
end
14.貼紙
原理步驟:
這兒的貼紙功能沒(méi)有采用人臉特征點(diǎn)定位,大家可以自行結(jié)合13和14功能點(diǎn)進(jìn)行特征點(diǎn)定位貼圖。
在圖像上添加貼紙的原理就是將兩張圖片疊加起來(lái),但是很明顯原圖像和貼紙大小不同,而原圖像大小也不確定。所以我們就可以通過(guò)先構(gòu)造一個(gè)和原圖像同樣大小的全零矩陣,再將貼紙放進(jìn)這個(gè)矩陣作為和原圖像同樣大小的新圖像與原圖像疊加,就實(shí)現(xiàn)了貼紙的功能。
?
?
功能鍵-添加貼紙
% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2
tiezhi=get(handles.popupmenu2,'value');%獲取下拉列表選擇的序列
road=handles.road;%讀取原圖像
im1=imread(road);
switch tiezhi%添加不同貼紙
case 1
case 2
im2 = imread('fagu1.png');%小圖
case 3
im2 = imread('srat.png');%小圖
case 4
im2 = imread('moon.png');%小圖
end
% 顯示大尺寸的彩色風(fēng)景圖和小尺寸彩色照片的原始圖
axes(handles.axes2);
imshow(im1);
imshow(im2);
% 小尺寸彩色照片在風(fēng)景圖當(dāng)中的居中處理
[m1,n1,l1] = size(im1);
[m2,n2,l2] = size(im2);
t = zeros(m1,n1,l1);
t = uint8(t);
t((m1/2-m2/2+1):(m1/2+m2/2),(n1/2-n2/2+1):(n1/2+n2/2),:) = im2 ;%做居中處理
C = imadd(0.5*t,im1);%乘以0.5對(duì)中間小照片做透明處理
axes(handles.axes2);%顯示原圖像
imshow(C);
15.油畫(huà)
原理步驟:
油畫(huà)就是用快干性的植物油調(diào)和出的顏料畫(huà)出來(lái)的畫(huà)。而油畫(huà)濾鏡就是將圖片處理成油畫(huà)出來(lái)的效果,算法思想是用當(dāng)前點(diǎn)四周一定范圍內(nèi)的任意一點(diǎn)的顏色來(lái)代替該點(diǎn)的顏色,所以就需要確定四周范圍的大小并用rand函數(shù)隨機(jī)找到某一點(diǎn)?;瑒?dòng)條可以控制隨機(jī)像素點(diǎn)的范圍,值越大,油畫(huà)效果就越模糊,反之,就越清晰。
?
濾鏡-油畫(huà)
% --- Executes on button press in pushbuttonyouhua.
function pushbuttonyouhua_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonyouhua (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
road=handles.road;%讀取原圖像
img=imread(road);
[height,width,k]=size(img);%獲取原圖像大小
N=10;
out=zeros(height,width,3);
for i=1:height%遍歷每個(gè)像素點(diǎn)
for j=1:width
temp=uint8(rand()*(N^2-1));%生成隨機(jī)數(shù)
m=temp/N;%取商
n=mod(temp,N);%取余
h=mod(double(i-1)+double(m),double(height));%找到隨機(jī)對(duì)應(yīng)的原圖像像素
w=mod(double(j-1)+double(n),double(width));
if w==0;
w=width;%若找到了上方和左方像素點(diǎn)就用下方和右方的代替
end
if h==0
h=height;
end
out(i,j,:)=img(h,w,:);%將找到的原圖像像素點(diǎn)的值賦給輸出圖像
end
end
axes(handles.axes2);%顯示效果圖
imshow(out/255)
滑動(dòng)條-油畫(huà)
% --- Executes on slider movement.
function slideryouhua_Callback(hObject, eventdata, handles)
% hObject handle to slideryouhua (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
N=get(hObject,'value');
road=handles.road;%讀取原圖像
img=imread(road);
[height,width,k]=size(img);%獲取原圖像大小
% N=10;
out=zeros(height,width,3);
for i=1:height%遍歷每個(gè)像素點(diǎn)
for j=1:width
temp=uint8(rand()*(N^2-1));%生成隨機(jī)數(shù)
m=temp/N;%取商
n=mod(temp,N);%取余
h=mod(double(i-1)+double(m),double(height));%找到隨機(jī)對(duì)應(yīng)的原圖像像素
w=mod(double(j-1)+double(n),double(width));
if w==0;
w=width;%若找到了上方和左方像素點(diǎn)就用下方和右方的代替
end
if h==0
h=height;
end
out(i,j,:)=img(h,w,:);%將找到的原圖像像素點(diǎn)的值賦給輸出圖像
end
end
axes(handles.axes2);%顯示效果圖
imshow(out/255)
16.藝術(shù)噪聲
原理步驟
藝術(shù)噪聲是一種濾鏡,可以增加圖片的質(zhì)感。實(shí)現(xiàn)原理是用imnoise函數(shù)給圖片添加高斯噪聲,因?yàn)楦咚乖肼曉趫D像上出現(xiàn)的位置是一定的,但是幅值是隨機(jī)的,用高斯噪聲處理圖像就可以實(shí)現(xiàn)有噪點(diǎn)的濾鏡效果。
?
濾鏡-藝術(shù)噪聲
% --- Executes on button press in pushbuttonvoice.
function pushbuttonvoice_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonvoice (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
road=handles.road;%讀取原圖像
img=imread(road);
img2=imnoise(img,'gauss',0.03);%添加高斯噪聲
axes(handles.axes2);%顯示添加了高斯噪聲后的圖像
imshow(img2);
17.灰度膠片
原理步驟
灰度膠片濾鏡就是將彩色圖像變?yōu)榛叶葓D像,實(shí)現(xiàn)原理是先將彩色圖像的三個(gè)通道轉(zhuǎn)成double型,否則uint8在大于255時(shí)會(huì)溢出,然后取三個(gè)通道灰度的平均值作為新圖像img2的灰度值,這種方法就是彩色圖像灰度化的平均值法,最后將原圖像RGB三個(gè)通道的值都取成Img2,也就是將三通道RGB值相同的灰度圖作為結(jié)果圖顯示。
?
濾鏡-灰度膠片
% --- Executes on button press in pushbuttongray.
function pushbuttongray_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttongray (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
road=handles.road;%讀取原圖像
img=imread(road);
imR=im2double(img(:,:,1));%將每個(gè)通道轉(zhuǎn)成double型,否則uint8>255會(huì)溢出
imG=im2double(img(:,:,2));
imB=im2double(img(:,:,3));
imRGB=round((imR+imG+imB)/3*255);%去三個(gè)通道的灰度均值--平均值法
img(:,:,1)=imRGB;%灰度圖的三通道RGB值相同
img(:,:,2)=imRGB;
img(:,:,3)=imRGB;
axes(handles.axes2);%顯示處理后的灰度圖
imshow(img);
18.動(dòng)感模糊
原理步驟:
動(dòng)感模糊濾鏡就是對(duì)圖像進(jìn)行運(yùn)動(dòng)模糊處理,該功能的運(yùn)動(dòng)模糊就是模擬了由于成像過(guò)程中相機(jī)運(yùn)動(dòng)或場(chǎng)景變化時(shí)造成不同空間位置信息的混疊。實(shí)現(xiàn)過(guò)程是先用fspecial函數(shù)創(chuàng)建運(yùn)動(dòng)模糊濾波器,也就是濾波算子,用motion類(lèi)型的濾波器更加接近相機(jī)的線性運(yùn)動(dòng),接著設(shè)定運(yùn)動(dòng)位移和運(yùn)動(dòng)角度參數(shù),之后在imfilter函數(shù)中該算子會(huì)對(duì)圖像進(jìn)行卷積運(yùn)算,最后就可得到運(yùn)動(dòng)模糊的效果圖了。
?
濾鏡-動(dòng)感模糊
% --- Executes on button press in pushbuttonmohu.
function pushbuttonmohu_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonmohu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
road=handles.road;%讀取原圖像
img=imread(road);
PSF=fspecial ('motion',25,11);%創(chuàng)建運(yùn)動(dòng)模糊濾波器
Blurred=imfilter (img,PSF, 'conv' , 'circular') ;%進(jìn)行濾波
axes(handles.axes2);%顯示結(jié)果圖
imshow(Blurred);
19.素描
原理步驟:
素描濾鏡就是將圖像的效果處理成通過(guò)黑白素描畫(huà)畫(huà)出來(lái)的效果,在ps中將彩色圖片變成素描效果需要先將圖像去色,復(fù)制該圖層后反色,接著向反色圖像添加高斯模糊,最后疊加圖像選擇顏色減淡效果。根據(jù)這個(gè)過(guò)程,也可在matlab實(shí)現(xiàn)彩色圖像素描化,圖像去色就是去某一個(gè)通道的灰度值進(jìn)行處理;而圖像反色就是遍歷每個(gè)像素點(diǎn)然后將該點(diǎn)像素值設(shè)為255-原像素值;添加高斯模糊就是同上一個(gè)功能先用fspecial函數(shù)構(gòu)造一個(gè)濾波器,再用imfilter函數(shù)進(jìn)行高斯模糊處理;最后顏色減淡就是根據(jù)C =MIN( A +(A×B)/(255-B),255)進(jìn)行處理,PS官方對(duì)顏色減淡的解釋是通過(guò)混合色及基色的各通道顏色值進(jìn)行對(duì)比,減少二者的對(duì)比度使基色的變量來(lái)反映混合色。至此,就可用matlab實(shí)現(xiàn)彩色圖像素描化的效果了。
該功能還可以通過(guò)拖動(dòng)滑動(dòng)條改變素描的效果,原理同上,滑動(dòng)條的參數(shù)代表構(gòu)造高斯濾波器時(shí)的標(biāo)準(zhǔn)差,值越大,越模糊,模糊程度越高,得到的素描結(jié)果就越清晰,框架紋理顏色就越深。
?
?
濾鏡-素描
% --- Executes on button press in pushbuttonsumiao.
function pushbuttonsumiao_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonsumiao (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
road=handles.road;%讀取圖像
I=imread(road);
[height,width,k]=size(I); %獲取原圖像大小
N=zeros(height,width); %取反之后值
rc = I(:,:,1);%三通道
gc = I(:,:,2);
bc = I(:,:,3);
channel = gc;%選擇一個(gè)通道進(jìn)行處理
out=zeros(height,width);
for i=1:height %遍歷每一個(gè)像素,將像素值取反
for j=1:width
N(i,j)=uint8(255-channel(i,j));
end
end
%高斯模糊
gausize = 9; %濾波器大小,越大越模糊
gausigma = 10; %越大越模糊
GH = fspecial('gaussian', gausize, gausigma);%構(gòu)造高斯模糊濾波器
G = imfilter(N, GH);%對(duì)圖像進(jìn)行濾波處理
for i=1:height
for j=1:width
b=double(G(i,j)); %高斯模糊后的圖像
a=double(channel(i,j)); %原圖某一個(gè)通道
temp=a+a*b/(255-b); %疊加,顏色減淡
out(i,j)=uint8(min(temp,255));
end
end
axes(handles.axes2);%顯示圖像
imshow(out/255);
滑動(dòng)條-素描
% --- Executes on slider movement.
function slidersumiao_Callback(hObject, eventdata, handles)
% hObject handle to slidersumiao (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
gausigma=get(hObject,'value');%獲取活動(dòng)條的數(shù)值
road=handles.road;
I=imread(road);
[height,width,k]=size(I); %獲取原圖像大小
N=zeros(height,width); %取反之后值
rc = I(:,:,1);%三通道
gc = I(:,:,2);
bc = I(:,:,3);
%選擇一個(gè)通道進(jìn)行處理
channel = gc;
out=zeros(height,width);
%顏色取反
for i=1:height
for j=1:width
N(i,j)=uint8(255-channel(i,j)); %double
end
end
%高斯模糊
gausize = 9; %濾波器大小,越大越模糊
% gausigma = 10; %越大越模糊
GH = fspecial('gaussian', gausize, gausigma);%構(gòu)造高斯模糊濾波器
G = imfilter(N, GH);%對(duì)圖像進(jìn)行濾波處理
for i=1:height
for j=1:width
b=double(G(i,j)); %高斯模糊后的圖像
a=double(channel(i,j)); %原圖某一個(gè)通道
temp=a+a*b/(255-b); %疊加,顏色減淡
out(i,j)=uint8(min(temp,255));
end
end
axes(handles.axes2);%顯示圖像
imshow(out/255);
20.浮雕
原理步驟:
浮雕濾鏡就是通過(guò)勾勒?qǐng)D像的輪廓和降低周?chē)伾祦?lái)生成凹陷或凸起的浮雕效果。在這里可通過(guò)相鄰元素相減的方法得到輪廓與邊緣的差,從而獲得凹凸的立體感。而浮雕算法為Y(i,j)=X(i-1,j-1)-X(i+1,j+1)+128,+128是為了不讓效果圖太黑,采用的算子是[1 0 0,0 0 0,0 0 -1],也就是對(duì)圖像中的每個(gè)像素進(jìn)行卷積處理從而實(shí)現(xiàn)浮雕效果。
?
?
濾鏡-浮雕
% --- Executes on button press in pushbuttonfudiao.
function pushbuttonfudiao_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonfudiao (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
road=handles.road;%讀取原圖像
img=imread(road);
[height,width,k]=size(img);%獲取原圖像大小
img2=zeros(height,width,3);
imggray=rgb2gray(img);%將原圖像灰度化
for i=2:height-1%遍歷每個(gè)像素點(diǎn)進(jìn)行卷積處理
for j=2:width-1
img2(i,j,:)=double(imggray(i-1,j-1,:))-double(imggray(i+1,j+1,:))+128;
end
end
axes(handles.axes2);%顯示結(jié)果圖
imshow(img2/255);
21.羽化
原理步驟:
羽化濾鏡就是讓圖像邊緣有朦朧的效果,羽化值越大,朦朧的范圍越寬,反之,朦朧范圍越窄。具體思想就是通過(guò)對(duì)圖像的像素值增加一個(gè)V值實(shí)現(xiàn)朦朧效果,而V = 255 × (當(dāng)前點(diǎn)Point距中點(diǎn)距離的平方)s1 / (頂點(diǎn)距中點(diǎn)的距離平方 × mSize)s2;不過(guò)乘255的效果過(guò)于明顯了,所以該功能換成了乘128,當(dāng)前點(diǎn)距中點(diǎn)的距離平方就是通過(guò)遍歷像素點(diǎn),用中點(diǎn)橫縱坐標(biāo)減當(dāng)前點(diǎn)橫縱坐標(biāo)得到dx,dy,再將dx,dy的平方和相加得到。通過(guò)滑動(dòng)條可控制羽化值mSize從而控制朦朧效果。
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-487917.html
?
濾鏡-羽化
% --- Executes on button press in pushbuttonyuhua.
function pushbuttonyuhua_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonyuhua (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
road=handles.road;%讀取圖像
img=imread(road);
[m,n,k]=size(img);%獲取原圖像大小
mSize = 0.6;%羽化值
centerX = n/2;%頂點(diǎn)距中點(diǎn)的距離平方
centerY = m/2;
diff = (centerX*centerX + centerY*centerY) * mSize;
for i=1:m
for j=1:n
dx = centerX - j;%當(dāng)前點(diǎn)距中點(diǎn)的距離
dy = centerY - i;
dstSq = dx * dx + dy * dy;%當(dāng)前點(diǎn)距中點(diǎn)距離的平方
V = 128 * dstSq / diff;
img8(i,j,1) = img(i,j,1) + V;%額外增加V實(shí)現(xiàn)朦朧效果
img8(i,j,2) = img(i,j,2) + V;
img8(i,j,3) = img(i,j,3) + V;
end
end
img2=cat(3,img8(:,:,1),img8(:,:,2),img8(:,:,3));%將三個(gè)通道連接起來(lái)
axes(handles.axes2);%顯示圖像
imshow(img2);
滑動(dòng)條-羽化
% --- Executes on slider movement.
function slideryuhua_Callback(hObject, eventdata, handles)
% hObject handle to slideryuhua (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
mSize=get(hObject,'Value');
road=handles.road;%讀取圖像
img=imread(road);
[m,n,k]=size(img);%獲取原圖像大小
centerX = n/2;%頂點(diǎn)距中點(diǎn)的距離平方
centerY = m/2;
diff = (centerX*centerX + centerY*centerY) * mSize;
for i=1:m
for j=1:n
dx = centerX - j;%當(dāng)前點(diǎn)距中點(diǎn)的距離
dy = centerY - i;
dstSq = dx * dx + dy * dy;%當(dāng)前點(diǎn)距中點(diǎn)距離的平方
V = 128 * dstSq / diff;
img8(i,j,1) = img(i,j,1) + V;%額外增加V實(shí)現(xiàn)朦朧效果
img8(i,j,2) = img(i,j,2) + V;
img8(i,j,3) = img(i,j,3) + V;
end
end
img2=cat(3,img8(:,:,1),img8(:,:,2),img8(:,:,3));%將三個(gè)通道連接起來(lái)
axes(handles.axes2);%顯示圖像
imshow(img2);
msgbox('羽化濾鏡設(shè)置成功');%顯示彈窗
到了這里,關(guān)于Matlab簡(jiǎn)易版美圖秀秀(GUI界面實(shí)現(xiàn))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!