GUI

来源:互联网 发布:怎么在淘宝举报店铺 编辑:程序博客网 时间:2024/06/04 20:09

第一个例子 GUI for beginners

首先打开 Graphic User Interface

输入 input 1,2 是 edit text

结果是 txt

执行键是 push button

function botton_Callback(hObject, eventdata, handles)    x = str2double(get(handles.input1, 'string'));    y = str2double(get(handles.input2, 'string'));    product = multiply (x, y);    set(handles.product, 'string', product);function product = multiply(x, y) % multiply 原程序    product = x*y;

第二个例子

function pushbutton1_Callback(hObject, eventdata, handles)    handles.output = hObject;    [a b] = uigetfile({'*.*'})    img = imread([b a]);    grayy = rgb2gray(img);    gr = graythresh(grayy);    handles.bw=im2bw(gray,gr);    imshow(img,'Parent', handles.axes1);    guidata(hObject, handles);function pushbutton3_Callback(hObject, eventdata, handles)    handles.output = hObject;    inverse_binary = not(handles.bw);    [handles.L handles.Num_object] = bwlabel(inverse_binary);    set(handles.text2, 'string', handles.Num_object);    imshow(handles.L, 'Parent', handles.axes2);

callback 正常用途全放在callback下

画表格

axes(handles.axes1)plot(sin(0:.1:10))

加法

a1 = str2double(get(handle.q1, 'String')) + str2double(get(handle.q2, 'String'))set(handles.q4, 'String', q4)

graythresh

sh = graythresh(image)img = im2bw(image, sh)

功能就是用 graythresh 找到一个阈值,然后用im2bw应用于原图像,使原图像变为二值图像。

uigetfile

用于调入电脑中某个文件

打开图像文件 [a b] = uigetfile({'*.*'})

button group

global imglobam BWstr = get(hObject, 'string'); %取得所选按钮的名称axes(handles.axes2) % 指定运行的坐标轴    switch str        case 'Initial'        imshow(im)    case 'sobel'        BW = edge(im, 'sobel');        imshow(BW);    end 

退出按钮设计

右键点击退出按钮,选择 view callbacks/callback

在空白处输入 close(gcf)

存储图片按钮设计

右键单击存储图片按钮, 选择 view callbacks/callback,

在空白处输入

global BW[filename, pathname, filterindex] = uiputfile({'*.bmp'; '*.tif'; '*.png'}, 'save picture');    if     filterindex == 0     return    else    str = [pathname filename]    axes(handles.axes2)    imwrite(BW,str);    end

sprintf

可以暂时理解为输出

string 就是字符串 不可变 ( 数字 + 字符 )

im2bw

这个函数 首先要设置阈值 im2bw(image, 阈值)

阈值可以通过 graythresh (image)来获取, image 需要时灰度图像

imopen

开运算, 属于形态学, 可以使边界平滑,消除细小的尖刺,断开窄小的连接,保持面积大小不变等

首先要定义一个形状函数

a = imread(‘image’);
b = strel(‘square’,2);
c = imopen(a, b);

drawnow

drawnow 是用来刷新屏幕的

bwperim

只返回图像边缘 就只有边缘那一圈白的

bw = bwperim(image);[r c] = find(bw == 1); % 找出外围的一圈edge

getframe

set(gcf, 'color', [1 1 1]) % 设置背景图像为白色F = getframe(gcf) % 获取整个窗口内容的图像

完整的例子

clear;clc;clf;x=0:pi/100:50;y=sin(x).*x;plot(x,y)set(gcf,'color',[1 1 1]) %设置背景色为白色F=getframe(gcf); % 获取整个窗口内容的图像F1=getframe; % 获取坐标轴为界的图像imwrite(F.cdata,'test1.png') %将F.cdata写入test1.png图片imwrite(F1.cdata,'test2.png')F2 = getframe(gcf,[150 150 200 200]); % 截取一部分图像figureimshow(F2.cdata); % 显示截取后的图片

frame2im

imwrite(I,'路径\名称.格式','格式');  格式很多 可以是bmp jpg 等 imwrite(I,'044.jpg','jpg'); 

a

imwrite(imind,cm,'gif','comet2.gif', 'Loopcount',inf,'DelayTime',0.1);

inmind,cm 是输出图片之前定义的[inmind, cm]

输出为 gif 文件

名称是 comet2.gif

loopcount inf 可以让这个gif一直播放

delaytime 是播放时间控制

x = 0:0.01:2*pi;omega = 0.5;k = 1;for t=1:50    plot(x, sin(omega*t + k*x), 'linewidth', 2, 'color', 'red');    ylim([-1 1]);    xlim([0 2*pi]);    grid on;    % gif utilities    set(gcf,'color','w'); % set figure background to white    drawnow;    frame = getframe(1);    im = frame2im(frame);    [imind,cm] = rgb2ind(im,256);    outfile = 'sinewave.gif';    % On the first loop, create the file. In subsequent loops, append.    if t==1        imwrite(imind,cm,outfile,'gif','DelayTime',0,'loopcount',inf);    else        imwrite(imind,cm,outfile,'gif','DelayTime',0,'writemode','append');    endend