diff --git a/imageproc.m b/imageproc.m new file mode 100644 index 0000000..7d56f10 --- /dev/null +++ b/imageproc.m @@ -0,0 +1,569 @@ +function varargout = imageproc(varargin) +% IMAGEPROC MATLAB code for imageproc.fig +% IMAGEPROC, by itself, creates a new IMAGEPROC or raises the existing +% singleton*. +% +% H = IMAGEPROC returns the handle to a new IMAGEPROC or the handle to +% the existing singleton*. +% +% IMAGEPROC('CALLBACK',hObject,eventData,handles,...) calls the local +% function named CALLBACK in IMAGEPROC.M with the given input arguments. +% +% IMAGEPROC('Property','Value',...) creates a new IMAGEPROC or raises the +% existing singleton*. Starting from the left, property value pairs are +% applied to the GUI before imageproc_OpeningFcn gets called. An +% unrecognized property name or invalid value makes property application +% stop. All inputs are passed to imageproc_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 imageproc + +% Last Modified by GUIDE v2.5 16-Jun-2024 16:46:30 + +% Begin initialization code - DO NOT EDIT +gui_Singleton = 1; +gui_State = struct('gui_Name', mfilename, ... + 'gui_Singleton', gui_Singleton, ... + 'gui_OpeningFcn', @imageproc_OpeningFcn, ... + 'gui_OutputFcn', @imageproc_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 imageproc is made visible. +function imageproc_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 imageproc (see VARARGIN) + +% Choose default command line output for imageproc +handles.output = hObject; + +% Update handles structure +guidata(hObject, handles); + +% UIWAIT makes imageproc wait for user response (see UIRESUME) +% uiwait(handles.figure1); + + +% --- Outputs from this function are returned to the command line. +function varargout = imageproc_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; + + +% -------------------------------------------------------------------- +function Untitled_1_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_1 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + + +% -------------------------------------------------------------------- +function Untitled_2_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_2 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + + +% -------------------------------------------------------------------- +function Untitled_5_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_5 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + + +% -------------------------------------------------------------------- +function Untitled_6_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_6 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + + +% -------------------------------------------------------------------- +function Untitled_7_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_7 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + + +% -------------------------------------------------------------------- +function Untitled_20_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_20 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +I = handles.I; +if size(I,3) == 3 + I = rgb2gray(I); +end +threshold_otsu = graythresh(I); % 获取最优阈值 +J = im2bw(I, threshold_otsu); % 二值化 + +axes(handles.axes2); +imshow(J); +title('图像分割-Otsu算法'); + + +% -------------------------------------------------------------------- +function Untitled_21_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_21 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +I = handles.I; +if size(I,3) == 3 + I = rgb2gray(I); +end + +[width,height]=size(I); +for i=1:width % 双重for循环逐个像素进行比较计算 + for j=1:height + if(I(i,j)>130) + K(i,j)=1;% 将大于全局阈值的像素点置为1(白色) + else + K(i,j)=0;% 将小于等于全局阈值的像素点置为0(黑色) + end + end +end + +axes(handles.axes2); +imshow(K); +title('图像分割-全局阈值法算法'); + +% -------------------------------------------------------------------- +function Untitled_19_Callback(hObject, eventdata, handles) +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +I = handles.I; + +if size(I,3) ~= 3 + msgbox('需要彩色图像'); + return; +end + +% 将RGB图像转换为HSI图像 +hsiImg = rgb2hsv(I); +% 提取亮度分量(I) +intensityChannel = hsiImg(:, :, 3); + +% 对亮度分量进行直方图均衡处理 +equalizedIntensityChannel = histeq(intensityChannel); + +% 更新HSI图像的亮度分量 +hsiImg(:,:,3) = equalizedIntensityChannel; + +% 将处理后的HSI图像转换回RGB图像 +J = hsv2rgb(hsiImg); + +axes(handles.axes2); +imshow(J); +title('RGB->HSI处理') + +% -------------------------------------------------------------------- +function Untitled_12_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_12 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +J = histeq(handles.I); +axes(handles.axes2); +imshow(J); +title('直方图均衡') + +% -------------------------------------------------------------------- +function Untitled_13_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_13 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +I = handles.I; +if size(I,3) == 3 + I = rgb2gray(I); +end +J = huidulashen(I); + +axes(handles.axes2); +imshow(J, []); +title('灰度拉伸') + + +function data2 = huidulashen(dataIn) +I=dataIn; +%分段 +I=im2double(I); +[M,N]=size(I); +J=zeros(M,N); +%设置参数 +X1 = 3; +Y1 = 15; +X2 = 7; +Y2 = 85; +for i = 1:M + for j = 1:N + if I(i, j) < X1 + J(i, j) = Y1 * I(i, j) / X1; + elseif I(i, j) > X2 + J(i, j) = (I(i, j) - X2) * (1 - Y2) / (1 - X2) + Y2; + else + J(i, j) = (I(i, j) - X1) * (Y2 - Y1) / (X2 - X1) + Y1; + end + end +end + +data2=J; + +% -------------------------------------------------------------------- +function Untitled_14_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_14 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +I = handles.I; + +if size(I,3) == 3 + I = rgb2gray(I); +end + +J = imadjust(I); +axes(handles.axes2); +imshow(J, []); +title('动态范围调整') + +% -------------------------------------------------------------------- +function Untitled_15_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_15 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + + +% -------------------------------------------------------------------- +function Untitled_16_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_16 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + + +% -------------------------------------------------------------------- +function Untitled_11_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_11 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +axes(handles.axes2); +imhist(handles.I); +title('直方图') + +% -------------------------------------------------------------------- +function Untitled_3_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_3 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + +[file,path] = uigetfile({'*.bmp';'*.jpg';'*.tiff';'*.gif';'*.tif';'*.*'}); +if isequal(file,0) + msgbox('用户取消选择'); + return; +end + +I = imread(fullfile(path, file)); +axes(handles.axes1) +imshow(I) +title('原图') + +handles.I = I; +guidata(hObject, handles); + + +% -------------------------------------------------------------------- +function Untitled_4_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_4 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +filter = {'*.bmp';'*.jpg';'*.tiff';'*.gif';'*.tif';'*.*'}; +[file, path] = uiputfile(filter); + +imwrite(handles.I, fullfile(path, file)); +msgbox('图像保存完毕'); + + + +% -------------------------------------------------------------------- +function Untitled_8_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_8 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +filter = {'*.bmp';'*.jpg';'*.tiff';'*.gif';'*.tif';'*.*'}; +[file, path] = uiputfile(filter); + +imwrite(handles.I, fullfile(path, file)); +msgbox('图像保存完毕'); + + +% -------------------------------------------------------------------- +function Untitled_9_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_9 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +%axes(handles.axes1) +print + +% -------------------------------------------------------------------- +function Untitled_10_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_10 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +choice=questdlg("您确定要退出吗?","提示","Yes","No","NO"); +switch choice + case "Yes" + close all + return; + case "NO" + return; +end + + +% -------------------------------------------------------------------- +function Untitled_18_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_18 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +I = handles.I; +if size(I,3) == 3 + I = rgb2gray(I); +end + +J = edge(I, 'Sobel'); +axes(handles.axes2); +imshow(J, []); +title('空间域锐化-Sobel算子') + +% -------------------------------------------------------------------- +function Untitled_17_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_17 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +I = handles.I; +h = fspecial('average',[3,3]);%3*3均值滤波 +J = imfilter(I, h); + +axes(handles.axes2); +imshow(J, []); +title('空间域平滑-均值滤波') + +% -------------------------------------------------------------------- +function Untitled_22_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_22 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +I = handles.I; +if size(I,3) == 1 + J = medfilt2(I, [3 3]); +else + J(:,:,1) = medfilt2(I(:,:,1), [3 3]); + J(:,:,2) = medfilt2(I(:,:,2), [3 3]); + J(:,:,3) = medfilt2(I(:,:,3), [3 3]); +end +axes(handles.axes2); +imshow(J, []); +title('空间域平滑-中值滤波') + + +% -------------------------------------------------------------------- +function Untitled_23_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_23 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +I = handles.I; +if size(I,3) == 3 + I = rgb2gray(I); +end + +J = edge(I, 'Prewitt'); +axes(handles.axes2); +imshow(J, []); +title('空间域锐化-Prewitt算子') + +% -------------------------------------------------------------------- +function Untitled_24_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_24 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +I = handles.I; +if size(I,3) == 3 + I = rgb2gray(I); +end + +J = edge(I, 'Roberts'); +axes(handles.axes2); +imshow(J, []); +title('空间域锐化-Roberts算子') + +% -------------------------------------------------------------------- +function Untitled_25_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_25 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +I = handles.I; +if size(I,3) == 3 + I = rgb2gray(I); +end + +J = edge(I, 'Canny'); +axes(handles.axes2); +imshow(J, []); +title('空间域锐化-Canny算子') + +% -------------------------------------------------------------------- +function Untitled_26_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_26 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) + +if ~isfield(handles, 'I') + msgbox('请先打开图像'); + return; +end + +I = handles.I; +if size(I,3) == 3 + I = rgb2gray(I); +end + +h = fspecial('laplacian', 0.2); +J = imfilter(I, h); +axes(handles.axes2); +imshow(J, []); +title('空间域锐化-Canny算子') + + +% -------------------------------------------------------------------- +function Untitled_27_Callback(hObject, eventdata, handles) +% hObject handle to Untitled_27 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA) +axes(handles.axes1) +cla reset + +axes(handles.axes2) +cla reset + +handles = rmfield(handles, 'I'); +guidata(hObject, handles); + + +% --- Executes during object creation, after setting all properties. +function axes1_CreateFcn(hObject, eventdata, handles) +% hObject handle to axes1 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles empty - handles not created until after all CreateFcns called + +% Hint: place code in OpeningFcn to populate axes1 + + +% --- Executes during object deletion, before destroying properties. +function axes1_DeleteFcn(hObject, eventdata, handles) +% hObject handle to axes1 (see GCBO) +% eventdata reserved - to be defined in a future version of MATLAB +% handles structure with handles and user data (see GUIDATA)