Matlab期末大作业

文章目录


登录界面:
Matlab期末大作业_第1张图片

function varargout = untitled1(varargin)
% UNTITLED1 MATLAB code for untitled1.fig
%      UNTITLED1, by itself, creates a new UNTITLED1 or raises the existing
%      singleton*.
%
%      H = UNTITLED1 returns the handle 0to a new UNTITLED1 or the handle to
%      the existing singleton*.
%
%      UNTITLED1('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in UNTITLED1.M with the given input arguments.
%
%      UNTITLED1('Property','Value',...) creates a new UNTITLED1 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before untitled1_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to untitled1_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 untitled1

% Last Modified by GUIDE v2.5 05-Jun-2023 21:42:41

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @untitled1_OpeningFcn, ...
                   'gui_OutputFcn',  @untitled1_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 untitled1 is made visible.
function untitled1_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 untitled1 (see VARARGIN)

% Choose default command line output for untitled1
handles.output = hObject;
%设置背景
picture = imread('.\picture\beijing.png');
image(picture);
set(handles.axes1);
uistack(handles.axes1,'bottom');


% Update handles structure
guidata(hObject, handles);

% UIWAIT makes untitled1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = untitled1_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 denglu.
function denglu_Callback(hObject, eventdata, handles)
% hObject    handle to denglu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Get the username and password from the textboxes
username = get(handles.yonghu, 'String');
password = get(handles.mima, 'String');
% Check if either textbox is empty
if isempty(username) || isempty(password)
    errordlg('请输入账号与密码.', 'Error');
    return;
end
fid = fopen('personal_info.txt', 'r');
found_match = false;

% 循环读取文件内容
while ~feof(fid)
    % Read the next line of text from the file
    line = fgetl(fid);
    
    % Split the line by whitespace
    split_line = strsplit(line);
    
    % Check if the username and password match
    if strcmp(username, split_line{1}) && strcmp(password, split_line{2})
        % 如果找到匹配项,设置标记并退出循环
        found_match = true;
        break;
    end
end

% 处理查找结果
if found_match
    % 如果找到了匹配项,显示登陆成功的消息
    msgbox('恭喜,登陆成功');
    % Close the file and return
    fclose(fid);
    % Clear the text boxes
    set(handles.yonghu, 'String', '');
    set(handles.mima, 'String', '');
    close(untitled1);
    pause(1);
    run('gui.m');
else
    % 如果没有找到匹配项,则显示失败消息
    msgbox('错误:用户名或密码不正确!');
end

% --- Executes on button press in tuichu.
function tuichu_Callback(hObject, eventdata, handles)
% hObject    handle to tuichu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
button=questdlg('确定退出吗?','退出程序','Yes','No','Yes');%内容,标题,选项(2个),默认选项
if strcmp(button,'Yes')  
    disp('关闭GUI界面');
    uiresume(handles.figure1);
    close
else
    quit cancel;
end



function yonghu_Callback(hObject, eventdata, handles)
% hObject    handle to yonghu (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 yonghu as text
%        str2double(get(hObject,'String')) returns contents of yonghu as a double
% global nName;
% global cName;
% nName=str2double(get(hObject,'String'));
% cName=get(hObject,'String');
% if(isnan(nName) || length(cName)~=5)
%     set(hObject,'String','');
%     errordlg('账号必须为5位数字!','错误提醒','modal');
%     return;
% end



% --- Executes during object creation, after setting all properties.
function yonghu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to yonghu (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 mima_Callback(hObject, eventdata, handles)
% hObject    handle to mima (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 mima as text
%        str2double(get(hObject,'String')) returns contents of mima as a double


% --- Executes during object creation, after setting all properties.
function mima_CreateFcn(hObject, eventdata, handles)
% hObject    handle to mima (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 during object creation, after setting all properties.
function uipanel4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to uipanel4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called



% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- 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
% I1=imread('background.png');
% hAxis = axes('Position',[0.4,0.07692307692307693,1,1]);
% imshow(I1,'Parent',hAxis)

% --- Executes on button press in register_button.
function register_button_Callback(hObject, eventdata, handles)
% hObject    handle to register_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% set(handles.uipanel5,'visible','on');
% set(handles.yonghu,'visible','off');
% set(handles.mima,'visible','off');
% set(handles.text2,'visible','off');
% set(handles.text3,'visible','off');
% set(handles.register_button,'visible','off');
% set(handles.denglu,'visible','off');
% set(handles.tuichu,'visible','off');
close(untitled1);
run('zhuce.m');
function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (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 edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (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 edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (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 edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (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 pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% 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)
set(handles.uipanel5,'visible','off');
set(handles.yonghu,'visible','on');
set(handles.mima,'visible','on');
set(handles.text2,'visible','on');
set(handles.text3,'visible','on');
set(handles.register_button,'visible','on');
set(handles.denglu,'visible','on');
set(handles.tuichu,'visible','on');
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% 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)
nusername = get(handles.edit6, 'String');
npassword = get(handles.edit7, 'String');
% Check if either textbox is empty
if isempty(nusername) || isempty(npassword)
    errordlg('请输入账号和密码.', 'Error');
    return;
end
% Open the file for appending
n2password=get(handles.edit8,'string');
if strcmp(npassword,n2password);
    fid = fopen('personal_info.txt', 'a');
% Write the new account info to the file
    fprintf(fid, '%s %s\n', nusername, npassword);
    fclose(fid);
    msgbox('账号创建成功!');
end
% Clear the text boxes
set(handles.edit3, 'String', '');
set(handles.edit4, 'String', '');
set(handles.edit5, 'String', '');


% --- Executes during object creation, after setting all properties.
function axes2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes2 (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 axes2

注册界面:
Matlab期末大作业_第2张图片

function varargout = zhuce(varargin)
% ZHUCE MATLAB code for zhuce.fig
%      ZHUCE, by itself, creates a new ZHUCE or raises the existing
%      singleton*.
%
%      H = ZHUCE returns the handle to a new ZHUCE or the handle to
%      the existing singleton*.
%
%      ZHUCE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ZHUCE.M with the given input arguments.
%
%      ZHUCE('Property','Value',...) creates a new ZHUCE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before zhuce_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to zhuce_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 zhuce

% Last Modified by GUIDE v2.5 05-Jun-2023 22:27:43

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @zhuce_OpeningFcn, ...
                   'gui_OutputFcn',  @zhuce_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 zhuce is made visible.
function zhuce_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 zhuce (see VARARGIN)

% Choose default command line output for zhuce
handles.output = hObject;
picture = imread('.\picture\beijing.png');
image(picture);
set(handles.axes1);
uistack(handles.axes1,'bottom');
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes zhuce wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = zhuce_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 edit6_Callback(hObject, eventdata, handles)
% hObject    handle to edit6 (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 edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (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 edit7_Callback(hObject, eventdata, handles)
% hObject    handle to edit7 (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 edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7 (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 edit8_Callback(hObject, eventdata, handles)
% hObject    handle to edit8 (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 edit8 as text
%        str2double(get(hObject,'String')) returns contents of edit8 as a double


% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit8 (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 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)
close(zhuce);
run('untitled1.m');

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% 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)
nusername = get(handles.edit6, 'String');
npassword = get(handles.edit7, 'String');
% Check if either textbox is empty
if isempty(nusername) || isempty(npassword)
    errordlg('请输入账号和密码.', 'Error');
    return;
end
% Open the file for appending
n2password=get(handles.edit8,'string');
if strcmp(npassword,n2password);
    fid = fopen('personal_info.txt', 'a');
% Write the new account info to the file
    fprintf(fid, '%s %s\n', nusername, npassword);
    msgbox('账号创建成功!');
    fclose(fid);
    
end
% Clear the text boxes
set(handles.edit6, 'String', '');
set(handles.edit7, 'String', '');
set(handles.edit8, 'String', '');

系统主界面:
Matlab期末大作业_第3张图片

function varargout = gui(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_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


function gui_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% 设置 axes 的默认显示
%guidata(hObject, handles);
% 加载初始背景图片并显示
initialBgImgPath = '.\picture\beijing.png'; % 替换为实际的初始背景图片路径
handles.backgroundImage = imread(initialBgImgPath);
imshow(handles.backgroundImage, 'Parent', handles.axes5,'InitialMagnification', 'fit');
set(handles.axes5, 'DataAspectRatio', [size(handles.backgroundImage, 1) size(handles.backgroundImage, 1) 2]);
guidata(hObject, handles);

function varargout = gui_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;
% --- 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)
% 加载新的背景图片并显示
% 获取背景图片文件夹下所有图片文件的路径
folderPath = '.\picture'; % 请将指定文件夹路径替换为实际值
bgImgs = dir(fullfile(folderPath, '*.png'));
if isempty(bgImgs)
    error('No background images found in the specified folder.');
end
% 从图片文件列表中随机选择一张背景图片
randInd = randi([1, length(bgImgs)]);
[~, imName, imExt] = fileparts(bgImgs(randInd).name);
imPath = fullfile(folderPath, [imName, imExt]);
% 显示新的背景图片
handles.backgroundImage = imread(imPath);
imshow(handles.backgroundImage, 'Parent', handles.axes5);
set(handles.axes5, 'DataAspectRatio', [size(handles.backgroundImage, 1) size(handles.backgroundImage, 1) 2]);














% --------------------------------------------------------------------
%文件
function open_Callback(hObject, eventdata, handles)%打开图片
global im   %定义一个全局变量im
global im2
[filename,pathname]=...
    uigetfile({'*.*';'*.bmp';'*.tif';'*.png'},'select picture');  %选择图片路径
str=[pathname filename];  %合成路径+文件名
im=imread(str);   %读取图片
im2=im;
axes(handles.axes1);  %使用第一个axes
imshow(im);  %显示图片
set(handles.edit5,'string',pathname);
function save_Callback(hObject, eventdata, handles)%保存图片
global BW 
set(handles.axes2,'HandleVisibility','ON');
axes(handles.axes2);
[filename,pathname]=uiputfile({'*.jpg';'*.bmp';'*.tif';'*.*'},'save image as');
file=strcat(pathname,filename);
BW=getimage(gca);
imwrite(BW,file);
set(handles.axes2,'HandleVisibility','Off');
function quit_Callback(hObject, ~, handles)%退出操作
close(gcf)  %关闭当前Figure窗口句柄

%菜单栏的调回函数,实际不使用
function t1_Callback(hObject, eventdata, handles)

function t2_Callback(hObject, eventdata, handles)

function t3_Callback(hObject, eventdata, handles)

function t4_Callback(hObject, eventdata, handles)

function t5_Callback(hObject, eventdata, handles)

function t6_Callback(hObject, eventdata, handles)

function t7_Callback(hObject, eventdata, handles)

function t8_Callback(hObject, eventdata, handles)

function t9_Callback(hObject, eventdata, handles)


% --------------------------------------------------------------------
% 图像类型变换
function rgb2gray_Callback(hObject, eventdata, handles)%原图-灰度
global im
global BW  %定义全局变量  
axes(handles.axes2); 
BW=rgb2gray(im);
im=BW;
imshow(BW);

function im2bw_Callback(hObject, eventdata, handles)%原图-二值
global im
global BW  %定义全局变量  
axes(handles.axes2); 
BW=im2bw(im);
im=BW;
imshow(BW);


% --------------------------------------------------------------------
% 边缘检测
function roberts_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
BW=edge(im,'roberts',0.04);
imshow(BW);

function sobel_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
BW=edge(im,'sobel',0.04);
imshow(BW);

function prewitt_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
BW=edge(im,'prewitt',0.04);
imshow(BW);

function log_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
BW=edge(im,'log',0.003);
imshow(BW);

% --------------------------------------------------------------------
%图像变换
function DFT_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2); 
I1=double(im);
I2=fft2(I1);
I3=fftshift(I2);
I3=log(abs(I3));
BW=I3;
imshow(BW,[]);

function DCT_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2); 
I1=double(im);
I2=dct2(I1);
I3=log(abs(I2));
BW=I3;
imshow(BW);

% --------------------------------------------------------------------
% 图像旋转
function rotate_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量 
% A=getimage(handles.axes1);
A=im;
axes(handles.axes2); 
prompt={'度数:'};
def={'90'};
answer=inputdlg(prompt,'请输入:',1,def);
if ~isempty(answer)
a = str2num(answer{1});%将字符串类型转化成数值类型
J=imrotate(A,360-a);%imrotate 函数将变量 A 表示的图像旋转 a 度,并将结果存储在变量 J 中
BW=J;
imshow(BW);
end

function Initial_Callback(hObject, eventdata, handles)%初始化
global im
global im2
global BW  %定义全局变量 
BW=im2;
im=im2;
axes(handles.axes2); 
imshow(BW);

% --------------------------------------------------------------------
% 图像噪声添加
%高斯噪声
function gaussian_Callback(hObject, eventdata, handles)
global im;
global BW;  %定义全局变量 
axes(handles.axes2); 

prompt={'均值:','标准差:'};
def={'0.05','0.1'};
answer=inputdlg(prompt,'请输入高斯参数:',1,def);
if ~isempty(answer)
    a = str2num(answer{1});
    b = str2num(answer{2});
    
I=im2double(im);
f = imnoise(I,'gaussian',str2num(answer{1}),str2num(answer{2}));
BW=f;
imshow(BW);
end



function salt_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量 
axes(handles.axes2); 

prompt={'Density:',};
def={'0.1'};
answer=inputdlg(prompt,'请输入椒盐参数:',1,def);
if ~isempty(answer)
    a = str2num(answer{1});
    
I=im2double(im);
J=imnoise(I,'salt & pepper',str2num(answer{1}));
BW=J;
imshow(BW);
end






function speckle_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量 

axes(handles.axes2); 
prompt={'Sigma:'};
def={'0.04'};
answer=inputdlg(prompt,'请输入乘法参数:',1,def);
if ~isempty(answer)
    a = str2num(answer{1});
I=im2double(im);
J=imnoise(I,'speckle',str2num(answer{1}));
BW=J;
imshow(BW);
end


function poisson_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量 
I=im2double(im);
J=imnoise(I,'poisson');
BW=J;
axes(handles.axes2); 
imshow(BW);

% --------------------------------------------------------------------
% 图像滤波
function medilt_Callback(hObject, eventdata, handles)%中值滤波
global BW  %定义全局变量 
J=medfilt2(BW, [3,3]);
BW=J;
axes(handles.axes2); 
imshow(BW);

function wiener_Callback(hObject, eventdata, handles)%自适应滤波
global BW  %定义全局变量 
J=wiener2(BW,[3,3]);
BW=J;
axes(handles.axes2); 
imshow(BW);

function filter2_Callback(hObject, eventdata, handles)%均值滤波
global BW  %定义全局变量 
M1=ones(3);
M1=M1/9;
J=filter2(M1,BW);
BW=J;
axes(handles.axes2); 
imshow(BW);

% --------------------------------------------------------------------
%图像灰度变化
function plotchange_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
A=im2double(im);%将灰度图像im转为双精度浮点数
    a=0.3;%0.3 0.7 0.5 0.9
    b=0.7;
    c=0.1;
    d=0.9;
    %0.3 0.7 0.1 0.9
    B=A;
    [m,n]=size(B);
    Mg=max(max(B));
    Mf=max(max(A));
    for (i=1:m)
      for (j=1:n)
        if(A(i,j)>=0&&A(i,j)<=a)
             B(i,j)=(c/a)*A(i,j);
        end
        if(A(i,j)>=a&&A(i,j)<=b)
            B(i,j)=(((d-c)/(b-a))*(A(i,j)-a))+c;
        end
        if(A(i,j)>=b&&A(i,j)<=1)
             B(i,j)=(((Mg-d)/(Mf-b))*(A(i,j)-b))+d;
        end
      end
    end
   BW=B;
   imshow(BW);

function imhist_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
 BW=im;
 imhist(BW);
 
function histeq_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
BW=histeq(im);
imhist(BW);

% --------------------------------------------------------------------
function histeqafter_Callback(hObject, eventdata, handles)
global im
global BW  %定义全局变量
axes(handles.axes2);   %使用第二个axes
imshow(BW);



% --- Executes during object creation, after setting all properties.
function gaus_edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to gaus_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



% --- Executes during object creation, after setting all properties.
function gaus_edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to gaus_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 during object creation, after setting all properties.
function jiaoyan_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to jiaoyan_edit (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 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)


% --- Executes on button press in pushbutton2.
%function pushbutton2_Callback(hObject, eventdata, handles)
% 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)





% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over text4.
function text4_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to text4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
function varargout = MP3(varargin)
% MP3 MATLAB code for MP3.fig
%      MP3, by itself, creates a new MP3 or raises the existing
%      singleton*.
%
%      H = MP3 returns the handle to a new MP3 or the handle to
%      the existing singleton*.
%
%      MP3('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MP3.M with the given input arguments.
%
%      MP3('Property','Value',...) creates a new MP3 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before MP3_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to MP3_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 MP3
 
% Last Modified by GUIDE v2.5 07-Jun-2023 16:32:28
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @MP3_OpeningFcn, ...
                   'gui_OutputFcn',  @MP3_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 MP3 is made visible.
function MP3_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 MP3 (see VARARGIN)
% Choose default command line output for MP3
handles.output = hObject;

 % 初始化全局变量
global pause_position;
pause_position = 0;

% Update handles structure
guidata(hObject, handles);
 
% UIWAIT makes MP3 wait for user response (see UIRESUME)
% uiwait(handles.MP3);
 
 
% --- Outputs from this function are returned to the command line.
function varargout1 = MP3_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
varargout1{1} = handles.output;
 
 


%{
% --------------------------------------------------------------------
function m_file_mp3_Callback(hObject, eventdata, handles)
% hObject    handle to m_file_mp3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global abc;%全局变量用于传递MP3文件数据
[FileName,PathName] = uigetfile('*.mp3','Select mp3 File');%用于选择打开MP3文件
abc = fullfile(PathName,FileName);%将文件数据传递给abc变量
set(handles.text_mp3,'String',FileName);%将MP3文件名称在静态文本上显示
%}


% --- Executes on button press in test1.
%继续播放
function test1_Callback(hObject, eventdata, handles)
% hObject    handle to test1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global abc;
global pause_position;
handles.activex1.URL = abc;
handles.activex1.controls.currentPosition = pause_position;
handles.activex1.controls.play();
guidata(hObject, handles);
set(handles.activex1,'URL',abc);%在activex1控件上播放MP3文件


%暂停播放
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% 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)
global pause_position;
pause_position = handles.activex1.controls.currentPosition;
handles.activex1.controls.pause();
guidata(hObject, handles);

%选择音频
% --- 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)
global abc;%全局变量用于传递MP3文件数据
dirName = '.\songs'; % 指定文件夹路径
mp3List = dir(fullfile(dirName, '*.mp3'));
[mp3List.folder] = deal(dirName); % 将mp3List结构体数组中的所有元素的folder属性赋值为dirName

% 如果mp3List非空,即文件夹中有mp3文件时
if ~isempty(mp3List)
    mp3Index = randi(numel(mp3List)); % 随机生成一个1到numel(mp3List)之间的整数
    abc = fullfile(mp3List(mp3Index).folder, mp3List(mp3Index).name); % 将随机获取的mp3文件数据传递给abc变量
    set(handles.text_mp3, 'String', mp3List(mp3Index).name); % 将随机获取的mp3文件名称在静态文本上显示
    set(handles.activex1,'URL',abc);%在activex1控件上播放MP3文件
else
    abc = ''; % 如果文件夹中没有mp3文件,清空abc变量
    set(handles.text_mp3, 'String', 'No mp3 file found.'); % 显示未找到mp3文件的提示信息
end



% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over text14.
function text14_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to text14 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
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)


% --------------------------------------------------------------------
%平移x y
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)
global im;
global BW;
A=im;
axes(handles.axes2);   %使用第二个axes
prompt={'平移的x值:','平移的y值:'};
def={'150','220'};
answer=inputdlg(prompt,'请输入:',1,def);
if ~isempty(answer)
    a = str2num(answer{1});
    I5=imtranslate(A,[150,100]);%水平移动150 垂直移动100
BW=I5;
imshow(BW);
end


% --------------------------------------------------------------------
%水平镜像
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)
global BW;
I2=flipdim(BW,2);  
axes(handles.axes2);   %使用第二个axes
imshow(I2);





% --------------------------------------------------------------------
%垂直镜像
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)
global BW;
I1=flipdim(BW,1);  %图像的水平镜像
axes(handles.axes2); 
imshow(I1);


% --------------------------------------------------------------------
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)


% --------------------------------------------------------------------
%理想低通滤波
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)
global BW;
I=im2double(BW);
M=2*size(I,1);  %滤波器行数
N=2*size(I,2);  %滤波器列数
u=-M/2:(M/2-1);
v=-N/2:(M/2-1);
[U,V]=meshgrid(u,v);
D=sqrt(U.^2+V.^2);
D0=60;  %截止频率
H=double(D<=D0); %理想低通滤波
J=fftshift(fft2(I,size(H,1),size(H,2))); %时域图像转换到频域
K=J.*H;  %滤波处理
L=ifft2(ifftshift(K)); %傅里叶反变换
L=L(1:size(I,1),1:size(I,2));
axes(handles.axes2); 
imshow(L);



% --------------------------------------------------------------------
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)
global BW;
I=im2double(BW);
M=2*size(I,1);  %滤波器行数
N=2*size(I,2);  %滤波器列数
u=-M/2:(M/2-1);
v=-N/2:(M/2-1);
[U,V]=meshgrid(u,v);
D=sqrt(U.^2+V.^2);
D0=50;  %截止频率
n=6; %滤波器的阶数
H=1./(1+(D./D0).^(2*n)); %巴特沃斯滤波器
J=fftshift(fft2(I,size(H,1),size(H,2))); %时域图像转换到频域
K=J.*H;  %滤波处理
L=ifft2(ifftshift(K)); %傅里叶反变换
L=L(1:size(I,1),1:size(I,2));
axes(handles.axes2); 
imshow(L);


% --------------------------------------------------------------------
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)



function edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (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 edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (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 during object creation, after setting all prope.rties.
function axes5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes5 (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 axes5

你可能感兴趣的:(matlab,课程设计,数学建模)