实例介绍
【实例截图】
【核心代码】
function varargout = image_processing(varargin) % IMAGE_PROCESSING MATLAB code for image_processing.fig % IMAGE_PROCESSING, by itself, creates a new IMAGE_PROCESSING or raises the existing % singleton*. % % H = IMAGE_PROCESSING returns the handle to a new IMAGE_PROCESSING or the handle to % the existing singleton*. % % IMAGE_PROCESSING('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in IMAGE_PROCESSING.M with the given input arguments. % % IMAGE_PROCESSING('Property','Value',...) creates a new IMAGE_PROCESSING or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before image_processing_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to image_processing_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help image_processing % Last Modified by GUIDE v2.5 22-Apr-2017 15:16:04 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @image_processing_OpeningFcn, ... 'gui_OutputFcn', @image_processing_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before image_processing is made visible. function image_processing_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to image_processing (see VARARGIN) % Choose default command line output for image_processing handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes image_processing wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = image_processing_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure axis off; varargout{1} = handles.output; % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) global M; ima_gray=image_gray(M); imshow(ima_gray); % 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) % --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) global M; ima_gray=image_gray(M); [i,j]=size(ima_gray); prompt = {'请输入阈值:'}; dlg_title = '提示'; num_lines = 1; def = {'5'}; value_i = inputdlg(prompt,dlg_title,num_lines); threshold_value=str2double(value_i); for a=1:i for b=1:j if ima_gray(a,b)<threshold_value ima_gray(a,b)=0; else ima_gray(a,b)=1; end end end ima_gray=mat2gray(ima_gray); imshow(ima_gray); % 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) % --- Executes on button press in pushbutton3. function pushbutton3_Callback(hObject, eventdata, handles) global M; ima_red=M(:,:,1); ima_green=M(:,:,2); ima_blue=M(:,:,3); img_red=mid_filter(ima_red,6); img_green=mid_filter(ima_green,6); img_blue=mid_filter(ima_blue,6); image(:,:,1)=img_red; image(:,:,2)=img_green; image(:,:,3)=img_blue; imshow(image); % hObject handle to pushbutton3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) global M; if size(M,1)<2; msgbox('请先打开图片'); end ima_red=M(:,:,1); ima_green=M(:,:,2); ima_blue=M(:,:,3); processing_red=low_pass_filter(ima_red); processing_green=low_pass_filter(ima_green); processing_blue=low_pass_filter(ima_blue); image(:,:,1)=processing_red; image(:,:,2)=processing_green; image(:,:,3)=processing_blue; imshow(image); % hObject handle to pushbutton4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in pushbutton5. function pushbutton5_Callback(hObject, eventdata, handles) global M; prompt = {'请输入滤波核大小:'}; dlg_title = '提示'; num_lines = 1; def = {'5'}; value_i = inputdlg(prompt,dlg_title,num_lines); N=str2double(value_i); d=avg_filter(M,N); imshow(d); % hObject handle to pushbutton5 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in pushbutton6. function pushbutton6_Callback(hObject, eventdata, handles) global M; %滤波核大小 ima_red=M(:,:,1); ima_green=M(:,:,2); ima_blue=M(:,:,3); prompt = {'请输入滤波器大小:'}; dlg_title = '提示'; num_lines = 1; value_i = inputdlg(prompt,dlg_title,num_lines); N=str2double(value_i); sigma=1.7; img_red=image_gaussian(ima_red,sigma,N); img_green=image_gaussian(ima_green,sigma,N); img_blue=image_gaussian(ima_blue,sigma,N); img(:,:,1)=img_red; img(:,:,2)=img_green; img(:,:,3)=img_blue; imshow(img); % hObject handle to pushbutton6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in pushbutton7. function pushbutton7_Callback(hObject, eventdata, handles) global M; ima_red=M(:,:,1); ima_green=M(:,:,2); ima_blue=M(:,:,3); histogram_red=histogram(ima_red); histogram_green=histogram(ima_green); histogram_blue=histogram(ima_blue); figure, subplot(1,3,1);plot(histogram_red),title('红色通道'); xlim([0 255]) subplot(1,3,2),plot(histogram_green),title('绿色通道'); xlim([0 255]) subplot(1,3,3),plot(histogram_blue),title('蓝色通道'); xlim([0 255]) % ima=imread('1.jpg'); % ima_gaussian=image_gaussian(ima,2,500); % hObject handle to pushbutton7 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in pushbutton8. function pushbutton8_Callback(hObject, eventdata, handles)%每个通道不一样,不能按照一个通道来 global M; ima_gray=image_gray(M); [i,j]=size(ima_gray); threshold_value=150; for a=1:i for b=1:j if ima_gray(a,b)<threshold_value ima_gray(a,b)=0; else ima_gray(a,b)=1; end end end ima_gray=mat2gray(ima_gray); IMG=ima_gray; [row,col]=size(IMG); figure,imshow(IMG);title('二值化'); for i=1:row-1 for j=1:col-1 if(IMG(i,j 1)&&IMG(i 1,j)) %若S中为1的位置全为1则为1 IMG(i,j)=1; %正向判断1 else IMG(i,j)=0; end end end figure,imshow(IMG);title('腐蚀'); % figure, % subplot(1,3,1);plot(histogram_red),title('红色通道'); % xlim([0 255]) % subplot(1,3,2),plot(histogram_green),title('绿色通道'); % xlim([0 255]) % subplot(1,3,3),plot(histogram_blue),title('蓝色通道'); % xlim([0 255]) % 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) % --- Executes on button press in pushbutton9. function pushbutton9_Callback(hObject, eventdata, handles) global M; [m,n,q]=size(M); img=double(M); %%canny边缘检测的前两步相对不复杂,所以我就直接调用系统函数了 %%高斯滤波 w=fspecial('gaussian',[5 5]); img=imfilter(img,w,'replicate'); %%sobel边缘检测 w=fspecial('sobel'); img_w=imfilter(img,w,'replicate'); %求横边缘 w=w'; img_h=imfilter(img,w,'replicate'); %求竖边缘 img=sqrt(img_w.^2 img_h.^2); %注意这里不是简单的求平均,而是平方和在开方。我曾经好长一段时间都搞错了 %%下面是非极大抑制 new_edge=zeros(m,n); for i=2:m-1 for j=2:n-1 Mx=img_w(i,j); My=img_h(i,j); if My~=0 o=atan(Mx/My); %边缘的法线弧度 elseif My==0 && Mx>0 o=pi/2; else o=-pi/2; end %Mx处用My和img进行插值 adds=get_coords(o); %边缘像素法线一侧求得的两点坐标,插值需要 M1=My*img(i adds(2),j adds(1)) (Mx-My)*img(i adds(4),j adds(3)); %插值后得到的像素,用此像素和当前像素比较 adds=get_coords(o pi);%边缘法线另一侧求得的两点坐标,插值需要 M2=My*img(i adds(2),j adds(1)) (Mx-My)*img(i adds(4),j adds(3)); %另一侧插值得到的像素,同样和当前像素比较 isbigger=(Mx*img(i,j)>M1)*(Mx*img(i,j)>=M2) (Mx*img(i,j)<M1)*(Mx*img(i,j)<=M2); %如果当前点比两边点都大置1 if isbigger new_edge(i,j)=img(i,j); end end end %%下面是滞后阈值处理 up=120; %上阈值 low=100; %下阈值 set(0,'RecursionLimit',10000); %设置最大递归深度 for i=1:m for j=1:n if new_edge(i,j)>up &&new_edge(i,j)~=255 %判断上阈值 new_edge(i,j)=255; new_edge=connect(new_edge,i,j,low); end end end imshow(new_edge==255); % 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) % --- Executes on button press in pushbutton10. function pushbutton10_Callback(hObject, eventdata, handles) global M; u=image_gray(M); F=double(M); U=double(u); [H,W]=size(u); uSobel=u; % ms=0; % ns=0; for i=2:H-1 for j=2:W-1 Gx=(U(i 1,j-1) 2*U(i 1,j) F(i 1,j 1))-(U(i-1,j-1) 2*U(i-1,j) F(i-1,j 1)); Gy=(U(i-1,j 1) 2*U(i,j 1) F(i 1,j 1))-(U(i-1,j-1) 2*U(i,j-1) F(i 1,j-1)); uSobel(i,j)=sqrt(Gx^2 Gy^2); % ms=ms uSobel(i,j); % ns=ns (uSobel(i,j)-ms)^2; end end % ms=ms/(H*W); % ns=ns/(H*W); imshow(M); figure; imshow(im2uint8(uSobel));title('Sobel处理后'); for i=1:H for j=1:W if(uSobel(i,j)<150) uSobel(i,j)=0; else uSobel(i,j)=1; end end end uSobel=mat2gray(uSobel); figure;imshow(uSobel);title('阈值细化'); % 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) % --- Executes on button press in pushbutton12. function pushbutton12_Callback(hObject, eventdata, handles) [filename, pathname] = uigetfile('*.jpg', '读取图片文件'); %选择图片文件 if isequal(filename,0) %判断是否选择 msgbox('没有选择任何图片'); else pathfile=fullfile(pathname, filename); %获得图片路径 global M; M=imread(pathfile); %将图片读入矩阵 imshow(M); end % hObject handle to pushbutton12 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in pushbutton13. function pushbutton13_Callback(hObject, eventdata, handles) button13=questdlg('你确定退出吗?','退出程序','Yes','No','Yes'); if strcmp(button13,'Yes') close all; end; % hObject handle to pushbutton13 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) function d=image_gray(src) ima_red=src(:,:,1); ima_green=src(:,:,2); ima_blue=src(:,:,3); d=0.299*ima_red 0.587*ima_green 0.114*ima_blue; function desimg=image_gaussian(originimg,sigma,N) [ori_row,ori_col]=size(originimg); N_row = 2*N 1; H = [];%求高斯模板H for i=1:N_row for j=1:N_row fenzi=double((i-N-1)^2 (j-N-1)^2); H(i,j)=exp(-fenzi/(2*sigma*sigma))/(2*pi*sigma); end end H=H/sum(H(:));%归一化 temp=[]; %模板与图像卷积实现 desimg=[ori_row,ori_col]; for ai=N 1:ori_row-N-1 for aj=N 1:ori_col-N-1 temp=0; for bi=1:N_row for bj=1:N_row temp= temp (originimg(ai bi-N,aj bj-N)*H(bi,bj)); end end desimg(ai,aj)=temp; end end desimg=uint8(desimg); function re=get_coords(angle) %angle是边缘法线角度,返回法线前后两点 sigma=0.000000001; x1=ceil(cos(angle pi/8)*sqrt(2)-0.5-sigma); y1=ceil(-sin(angle-pi/8)*sqrt(2)-0.5-sigma); x2=ceil(cos(angle-pi/8)*sqrt(2)-0.5-sigma); y2=ceil(-sin(angle-pi/8)*sqrt(2)-0.5-sigma); re=[x1 y1 x2 y2]; function nedge=connect(nedge,y,x,low) %种子定位后的连通分析 neighbour=[-1 -1;-1 0;-1 1;0 -1;0 1;1 -1;1 0;1 1]; %八连通搜寻 [m n]=size(nedge); for k=1:8 yy=y neighbour(k,1); xx=x neighbour(k,2); if yy>=1 &&yy<=m &&xx>=1 && xx<=n if nedge(yy,xx)>=low && nedge(yy,xx)~=255 %判断下阈值 nedge(yy,xx)=255; nedge=connect(nedge,yy,xx,low); end end end function d=mid_filter(ima,N) [height, width]=size(ima); %输入图像是p×q的,且p>n,q>n x1=double(ima); x2=x1; for i=1:height-N 1 for j=1:height-N 1 c=x1(i:i (N-1),j:j (N-1)); %取出x1中从(i,j)开始的n行n列元素,即模板(n×n的) e=c(1,:); %是c矩阵的第一行 for u=2:N e=[e,c(u,:)]; %将c矩阵变为一个行矩阵 end mm=median(e); %mm是中值 x2(i round((N-1)/2),j round((N-1)/2))=mm; %将模板各元素的中值赋给模板中心位置的元素 end end d=uint8(x2); %未被赋值的元素取原值 function d=avg_filter(image,n) a(1:n,1:n)=1; %a即n×n模板,元素全是1 [height, width]=size(image); %输入图像是hightxwidth的,且hight>n,width>n x1=double(image); x2=x1; for i=1:height-n 1 for j=1:width-n 1 c=x1(i:i (n-1),j:j (n-1)).*a; %取出x1中从(i,j)开始的n行n列元素与模板相乘 s=sum(sum(c)); %求c矩阵中各元素之和 x2(i (n-1)/2,j (n-1)/2)=s/(n*n); %将与模板运算后的各元素的均值赋给模板中心位置的元素 end end %未被赋值的元素取原值 d=uint8(x2); function B=low_pass_filter(image) m=double(image); f=fft2(m); f=fftshift(f); [N1,N2]=size(f); %返回矩阵的行和列 n1=round(N1/2); n2=round(N2/2); n=2;d0=50; %滤波器截止频率,滤波半径 for i=1:N1 for j=1:N2 d=sqrt((i-n1)^2 (j-n2)^2); %计算低通滤波转换函数 if d<=d0 h=1; else h=0; end y(i,j)=h*f(i,j); end end y=ifftshift(y); A=ifft2(y); B=uint8(real(A)); function nk=histogram(image) L=256; %灰度级 nk=zeros(L,1);%出现次数 [row,col]=size(image); n=row*col; %总像素个数 for i = 1:row for j = 1:col num = double(image(i,j)) 1; %获取像素点灰度级0到255所以要加上1 nk(num) = nk(num) 1; %统计nk end end
标签:
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论