【图像处理】基于matlab GUI图像滤镜(马赛克+蓝色透镜+素描)【含Matlab源码 1145期】
一、获取代码方式
获取代码方式1:
完整代码已上传我的资源:【图像处理】基于matlab GUI图像滤镜(马赛克+蓝色透镜+素描)【含Matlab源码 1145期】
获取代码方式2:
通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。
备注:
订阅紫极神光博客付费专栏,可免费获得1份代码(有效期为订阅日起,三天内有效);
二、简介
1 图像马赛克
图像打码其实也是图像卷积操作中,空间域滤波的一种方式,用一定大小的滤波器对马赛克范围内像素进行操作。实现过程:将需要打马范围按照滤波器大小划分为多个区块,取滤波器范围内像素,求取均值,再将均值赋值给范围内每一个像素,滤波器再滑到下一个区块。
2 素描效果
PhotoShop里面把彩色图片打造成素描的效果仅仅需要几步操作:
1.去色(转为灰度图);
2.复制去色图层,并且反色(反色相当于Y(i,j) = 255 - X(i,j));
3.对反色图像进行高斯模糊;
4.模糊后的图像叠加模式选择颜色减淡效果。减淡公式:C = MIN( A +(A×B)/(255-B, 255),其中C为混合结果,A为去色后的像素点,B为高斯模糊后的像素点。
三、部分源代码
%
%2021/07/21
function varargout = filter_pic(varargin)
% FILTER_PIC MATLAB code for filter_pic.fig
% FILTER_PIC, by itself, creates a new FILTER_PIC or raises the existing
% singleton*.
%
% H = FILTER_PIC returns the handle to a new FILTER_PIC or the handle to
% the existing singleton*.
%
% FILTER_PIC('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FILTER_PIC.M with the given input arguments.
%
% FILTER_PIC('Property','Value',...) creates a new FILTER_PIC or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before filter_pic_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to filter_pic_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 filter_pic
% Last Modified by GUIDE v2.5 21-Jun-2021 11:37:31
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @filter_pic_OpeningFcn, ...
'gui_OutputFcn', @filter_pic_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{:});xw
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before filter_pic is made visible.
function filter_pic_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 filter_pic (see VARARGIN)
% Choose default command line output for filter_pic
handles.output = hObject;
global begin_down begin_start openflag ;
begin_down=0;
begin_start=0;
openflag=0;
set(handles.axes2,'visible','off');
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes filter_pic wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = filter_pic_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
varargout{1} = handles.output;
% --- Executes on button press in Open.
function Open_Callback(hObject, eventdata, handles)
% hObject handle to Open (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%
global openflag begin_start;
[filename,pathname]=uigetfile({'*.bmp;*.jpg;*.gif','(*.bmp;*.jpg;*.gif)';'*.bmp','(*.bmp)';'*.jpg','(*.jpg)';'*.gif','(*.gif)';},'打开图片');
if isequal(filename,0)||isequal(pathname,0)
return;
else
openflag=1;
begin_start=0;
RGB=imread([pathname,filename]);
RGB = imresize(RGB, [352 500]); %调整图像大小
cla(handles.axes1);
axes(handles.axes1);
imshow(RGB);
handles.image=RGB;
guidata(hObject,handles);
end
% --- Executes on button press in Start.
function Start_Callback(hObject, eventdata, handles)
% hObject handle to Start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global begin_start openflag ;
if openflag ;
begin_start=1;
handles.begin_point=get(gca,'currentpoint'); %先初始化开始的点的坐标,否则程序会报错
handles.axes1_point=get(handles.axes1,'Position'); %图片坐标情况
guidata(hObject,handles);
else
errordlg('Please Open a File');
end
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global begin_down begin_start;
pt_begine = get(hObject,'CurrentPoint'); %坐标合理
%disp(pt_begine(1,1));
if begin_start...
&&pt_begine(1,1)>handles.axes1_point(1)...
&& pt_begine(1,2)>handles.axes1_point(2) ...
&& pt_begine(1,1)<handles.axes1_point(1)+ handles.axes1_point(3)...
&& pt_begine(1,2)<handles.axes1_point(2)+handles.axes1_point(4)
%disp(pt_begine(1,1));
begin_down=1;
% %set(handles.axes2,'visible',on);
% RGB=handles.image;
% y0=pt_begine(1,2);
% x0=pt_begine(1,1);
% x0_width=100;
% y0_height=100;
% %RGB_filter=imcrop(RGB,[x0-handles.axes1_point(1) y0-handles.axes1_point(2) x0_width y0_height]);
% %RGB_filter=imcrop(RGB,[x0-handles.axes1_point(1) handles.axes1_point(4)-(y0-handles.axes1_point(2)) x0_width y0_height]);
% RGB_filter=imcrop(RGB,[(x0-x0_width-handles.axes1_point(1)) handles.axes1_point(4)-( y0+y0_height-handles.axes1_point(2)) x0_width y0_height]);
% % Gray=rgb2gray(RGB_filter);
% % R=RGB_filter(:,:,1); G=RGB_filter(:,:,2); B=RGB_filter(:,:,3);
% % diff_R=20; diff_G=0; diff_B=0; % 设置红、绿、蓝三种颜色提取阈值(越大越严格)
% %
% % Image_B=RGB_filter;
% % BP_R=RGB_filter(:,:,1);BP_G=RGB_filter(:,:,2);BP_B=RGB_filter(:,:,3);
% % XYB=~((B-R)>diff_B&(B-G)>diff_B); % 提取绿色条件是G分量与R、B分量差值大于设定
% % Mask_B=Gray(XYB); % 灰照片掩膜
% % BP_R(XYB)=Mask_B; BP_G(XYB)=Mask_B; BP_B(XYB)=Mask_B; % 使得非蓝色区域变为灰色
% % Image_B(:,:,1)=BP_R; Image_B(:,:,2)=BP_G; Image_B(:,:,3)=BP_B;
% RGB_filter(:,:,1)=0;
% cla(handles.axes2);
% %set(handles.axes2,'Position',[x0 y0-y0_height x0_width y0_height]);
% set(handles.axes2,'Position',[x0-x0_width y0 x0_width y0_height]);
% axes(handles.axes2);
% imshow(RGB_filter);
% % set(handles.axes2,'Position',[1 1 30 30]);
% guidata(hObject,handles);
else
begin_down=0;
end
% --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global begin_down;
if begin_down
pt_process = get(hObject,'CurrentPoint'); %获取当前点坐标
RGB=handles.image;
x0=pt_process(1,1);
y0=pt_process(1,2);
x0_width=100;
y0_height=100;
if x0<handles.axes1_point(1)+x0_width
x0=handles.axes1_point(1)+x0_width;
else if x0>handles.axes1_point(1)+handles.axes1_point(3)
x0=handles.axes1_point(1)+handles.axes1_point(3);
end
end
if y0<handles.axes1_point(2)
y0=handles.axes1_point(2);
else if y0>handles.axes1_point(2)+handles.axes1_point(4)-y0_height;
y0=handles.axes1_point(2)+handles.axes1_point(4)-y0_height;
end
end
%set(handles.axes2,'visible',off);
%RGB_filter=imcrop(RGB,[x0-handles.axes1_point(1) y0-handles.axes1_point(2) x0_width y0_height]);
%RGB_filter=imcrop(RGB,[x0-handles.axes1_point(1) handles.axes1_point(4)-(y0-handles.axes1_point(2)) x0_width y0_height]);
set(handles.axes2,'visible','on');
RGB_filter=imcrop(RGB,[(x0-x0_width-handles.axes1_point(1)) handles.axes1_point(4)-( y0+y0_height-handles.axes1_point(2)) x0_width y0_height]);
%图像处理方法
maflag=get( handles.ma_radiobutton,'Value' );
blueflag=get( handles.blue_radiobutton,'Value');
writeflag=get( handles.write_radiobutton,'Value');
if maflag
pix_grp = 10;
height = size(RGB_filter,1);
width = size(RGB_filter,2);
mosaic = imresize(RGB_filter,[floor(height/pix_grp) floor(width/pix_grp)]);
RGB_filter = imresize(mosaic,[height width],'nearest');
else if writeflag
size_info=size(RGB_filter);
height=size_info(1);
width=size_info(2);
spec_img=zeros(height,width,3);
%img_temp=rgb2gray(RGB_filter);
for i=2:height-1
for j=2:width-1
spec_img(i,j,:)=double(RGB_filter(i-1,j-1,:))-double(RGB_filter(i+1,j+1,:))+128;
end
end
RGB_filter= spec_img/255;
else blueflag;
RGB_filter(:,:,1)=0;
end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
四、运行结果
五、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.
[5]陈浩,方勇,朱大洲,王成,陈子龙.基于蚁群算法的玉米植株热红外图像边缘检测[J].农机化研究. 2015,37(06)
文章来源: qq912100926.blog.csdn.net,作者:海神之光,版权归原作者所有,如需转载,请联系作者。
原文链接:qq912100926.blog.csdn.net/article/details/118978329
- 点赞
- 收藏
- 关注作者
评论(0)