请选择 进入手机版 | 继续访问电脑版

Hello Mat

 找回密码
 立即注册
查看: 4662|回复: 0

手写数字GUI界面

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2021-7-4 11:56:56 | 显示全部楼层 |阅读模式
直接运行改文件,界面,手写,点击【OK】即可保存图像:numpic.bmp
环境:64bit + win10 + matlab2019a
  1. function halcom_cn

  2. mIconCData      =   [];         % The icon CData edited by this GUI of dimension
  3.                                 % [mIconHeight, mIconWidth, 3]
  4. mIsEditingIcon  =   false;      % Flag for indicating whether the current mouse
  5.                                 % move is used for editing color or not
  6. % Variables for supporting custom property/value pairs

  7. mIconWidth      =   28;         % Use input property 'iconwidth' to initialize
  8. mIconHeight     =   28;         % Use input property 'iconheight' to initialize

  9. hMainFigure     =   figure(...
  10.                     'Units','characters',...
  11.                     'MenuBar','none',...
  12.                     'Toolbar','none',...
  13.                     'Position',[71.8 34.7 106 36.15],...
  14.                     'WindowStyle', 'normal',...
  15.                     'WindowButtonDownFcn', @hMainFigureWindowButtonDownFcn,...
  16.                     'WindowButtonUpFcn', @hMainFigureWindowButtonUpFcn,...
  17.                     'WindowButtonMotionFcn', @hMainFigureWindowButtonMotionFcn);
  18. hIconEditPanel  =    uipanel(...
  19.                     'Parent',hMainFigure,...
  20.                     'Units','characters',...
  21.                     'Clipping','on',...
  22.                     'Position',[1.8 4.3 68.2 27.77]);
  23. hIconEditAxes   =   axes(...
  24.                     'Parent',hIconEditPanel,...
  25.                     'vis','off',...
  26.                     'Units','characters',...
  27.                     'Position',[2 1.15 64 24.6]);
  28. hOKButton       =   uicontrol(...
  29.                     'Parent',hMainFigure,...
  30.                     'Units','characters',...
  31.                     'Position',[65.8 0.62 17.8 2.38],...
  32.                     'String','OK',...
  33.                     'Callback',@hOKButtonCallback);
  34.    
  35. mIconCData = ones(mIconHeight, mIconWidth, 3);
  36. localUpdateIconPlot();
  37. % localUpdateIconPlot();

  38. % Make the GUI on screen
  39. set(hMainFigure,'visible', 'on');
  40. movegui(hMainFigure,'onscreen');

  41. % Make the GUI blocking
  42. uiwait(hMainFigure);

  43. % Ip = preproc_image(mIconCData)';

  44. % save Ip.mat Ip
  45. % figure,
  46. % load('Ip.mat')
  47. % imshow(Ip,[])

  48.     %------------------------------------------------------------------
  49.     function hMainFigureWindowButtonDownFcn(hObject, eventdata)
  50.         if (ancestor(gco,'axes') == hIconEditAxes)
  51.             mIsEditingIcon = true;
  52.             localEditColor();
  53.         end
  54.     end
  55.     function hMainFigureWindowButtonUpFcn(hObject, eventdata)
  56.         mIsEditingIcon = false;
  57.     end
  58.     function hMainFigureWindowButtonMotionFcn(hObject, eventdata)
  59.         if (ancestor(gco,'axes') == hIconEditAxes)
  60.             localEditColor();
  61.         end
  62.     end

  63.     function localEditColor
  64.         if mIsEditingIcon
  65.             pt = get(hIconEditAxes,'currentpoint');

  66.             x = max(1, min(ceil(pt(1,1)), mIconWidth));
  67.             y = max(1, min(ceil(pt(1,2)), mIconHeight));

  68.             % update color of the selected block
  69.             m = get(gcf,'SelectionType');
  70.             if m(1) == 'n', % left button pressed
  71.                 mIconCData(y, x,:) = 0;
  72.                 if y<mIconHeight,   mIconCData(y+1,x,:) = .8*mIconCData(y+1,x,:); end
  73.                 if x<mIconWidth,    mIconCData(y,x+1,:) = .8*mIconCData(y,x+1,:); end
  74.                 if y>1,             mIconCData(y-1,x,:) = .8*mIconCData(y-1,x,:); end
  75.                 if x>1,             mIconCData(y,x-1,:) = .8*mIconCData(y,x-1,:); end
  76.             else
  77.                 mIconCData(y, x,:) = 1;
  78.             end
  79.             localUpdateIconPlot();
  80.             
  81.         end
  82.     end

  83.     %------------------------------------------------------------------
  84.     function localUpdateIconPlot   
  85.         if isempty(mIconCData)
  86.              mIconCData = nan(mIconHeight, mIconWidth, 3);
  87.         end
  88.         
  89.         rows = size(mIconCData, 1);
  90.         cols = size(mIconCData, 2);
  91.                
  92.         % update icon edit pane
  93.         set(hIconEditPanel, 'Title',['Icon Edit Pane (', num2str(rows),' X ', num2str(cols),')']);
  94.         
  95.         s = findobj(hIconEditPanel,'type','surface');        
  96.         if isempty(s)
  97.             gridColor = get(0, 'defaultuicontrolbackgroundcolor') - 0.2;
  98.             gridColor(gridColor<0)=0;
  99.             s=surface('edgecolor',gridColor,'parent',hIconEditAxes);
  100.         end        
  101.         %set xdata, ydata, zdata in case the rows and/or cols change
  102.         set(s,'xdata',0:cols,'ydata',0:rows,'zdata',zeros(rows+1,cols+1),'cdata',localGetIconCDataWithNaNs());

  103.         set(hIconEditAxes,'drawmode','fast','xlim',[-.5 cols+.5],'ylim',[-.5 rows+.5]);
  104.         axis(hIconEditAxes, 'ij', 'off');        
  105.     end

  106.     %------------------------------------------------------------------
  107.         function cdwithnan = localGetIconCDataWithNaNs()
  108.                 cdwithnan = mIconCData;
  109.                 cdwithnan(:,end+1,:) = NaN;
  110.                 cdwithnan(end+1,:,:) = NaN;
  111.     end
  112.    
  113.     %------------------------------------------------------------------
  114.     function hOKButtonCallback(hObject, eventdata)
  115.     % Callback called when the OK button is pressed
  116.         uiresume;
  117.         number = preproc_image(mIconCData)';
  118.         save number.mat number
  119.         imwrite(number,'numpic.bmp','bmp')
  120. %         delete(hMainFigure);
  121.     end

  122. end

  123. function out = preproc_image(id)
  124.     %Preprocess single image
  125.     Inorm = id(:,:,1);
  126.     Inorm(~isfinite(Inorm)) = 1;
  127.     Inorm = abs(Inorm-1)';
  128.     out = zeros(32);
  129.     out(3:30,3:30) = Inorm;
  130.     if sum(out(:))>0,
  131.         out = reshape(mapstd(out(:)'), 32, 32);
  132.     end
  133.    
  134. end

复制代码




算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-18 08:46 , Processed in 0.223643 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表