Matlab GUI click to display the image color value

Source: Internet
Author: User
Tags define function

First look at the effect

First, the function description. After running, open an image through the myfile menu and display it in axes. The menu bar and toolbar of figure will be opened by using the zoom-in and zoom-out functions of the image.

Interface Programming is mainly the compilation of callback functions.

Function varargout = mytest (varargin) % mytest M-file for mytest. fig % mytest, by itself, creates a new mytest or raises the existing % Singleton *. % H = mytest returns the handle to a new mytest or the handle to % the existing Singleton *. % mytest ('callback', hobject, eventdata, handles ,...) callthe local % function named callback in mytest. m with the given input arguments. % mytest ('properties', 'value ',...) creates a new mytest or raises the % existing Singleton *. starting from the left, property value pairs are % applied to the Gui before mytest_openingfcn gets called. an % unrecognized property name or invalid value makes property application % stop. all inputs are passed to mytest_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 mytest % last modified by guide v2.5 17-feb-2012 15:10:23% begin initialization code-do not editgui_singleton = 1; gui_state = struct ('gui _ name', mfilename ,... 'gui _ Singleton ', gui_singleton ,... 'gui _ openingfcn ', @ mytest_openingfcn ,... 'gui _ outputfcn ', @ mytest_outputfcn ,... 'gui _ layoutfcn ', [],... 'gui _ callback', []); If nargin & ischar (varargin {1}) gui_state.gui_callback = str2func (varargin {1}); endif 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 mytest is made visible. function mytest_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 mytest (see varargin) % choose Default command line output for mytesthandles. output = hobject; % update handles structureguidata (hobject, handles); setappdata (handles. myfigure, 'is _ open', 0); Set (handles. myfigure, 'menubar ', 'figure', 'toolbar', 'figure '); % set (handles. myaxes, 'outerposition', [0,-30, W/3, H]); % uiwait makes mytest wait for user response (see uiresume) % uiwait (handles. myfigure); % --- outputs from this function are returned to the command line. function varargout = mytest_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 structurevarargout {1} = handles. output; function myfigure_windowbuttondownfcn (hobject, eventdata, handles) % [x, y, but] = ginput (1); % IF (but = 1) % if x> 0 & Y> 0% % text (X, Y, 'q1'); % X1 = num2str (x); % Y1 = num2str (y ); % s = strcat (x1, ',', Y1); % set (handles. mytxt, 'string', S); % end % endis_open = getappdata (handles. myfigure, 'is _ open'); If is_open = 1 p = [0 0 0]; P = get (handles. myaxes, 'currentpoint'); A = get (handles. myfigure, 'userdata'); r = getappdata (handles. myfigure, 'si'); X = num2str (P (1); y = num2str (P (3); s = strcat (x, ',', y ); set (handles. mytxt, 'string', S); Tx = floor (P (3); ty = floor (P (1); disp (R. width) disp (R. height) If TX <= R. height & ty <= R. width & TX> = 1 & ty> = 1 R = a (TX, Ty, 1); G = a (TX, Ty, 2 ); B = a (TX, Ty, 3); RS = strcat (num2str (R), ',', num2str (G), ',', num2str (B )); set (handles. RGB, 'string', RS); stru = datacon (R, G, B); Set (handles. mystruct, 'string', stru); endend % dynpoint (handles. myaxes); % Col = get (handles. myaxes, 'color'); % R = num2str (sum (COL); % set (handles. mytxt, 'string', R) function myfigure_windowbuttonupfcn (hobject, eventdata, handles) % --- executes when user attempts to close myfigure. function myfigure_closerequestfcn (hobject, eventdata, handles) % hobject handle to myfigure (see gcbo) % eventdata reserved-to be defined in a future version of MATLAB % handles structure with handles and user data (see guidata) % hint: delete (hobject) closes the figuredelete (hobject ); % --- executes during object creation, after setting all properties. function myaxes_createfcn (hobject, eventdata, handles) % hobject handle to myaxes (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 myaxesaxis ([0 105 0 140]); % callback function file_callback (hobject, eventdata, handles) % hobject handle to file (see gcbo) % eventdata reserved-to be defined in a future version of MATLAB % handles structure with handles and user data (see guidata) % invoke function f_open_callback (hobject, eventdata, handles) % hobject handle to f_open (see gcbo) % eventdata reserved-to be defined in a future version of matlab [filename, pathname] = uigetfile (... {'*. BMP ;*. JPG ;*. PNG ;*. JPEG ', 'image files (*. BMP ,*. JPG ,*. PNG ,*. JPEG )';'*. * ', 'all files (*. *) '}, 'Pick an image'); If isequal (filename, 0) | isequal (pathname, 0), return; endfpath = [pathname filename]; A = imread (fpath); % andles structure with handles and user data (see guidata) r = imfinfo (fpath); setappdata (handles. myfigure, 'si', R); Set (handles. myfigure, 'userdata', a); F = image (a); setappdata (handles. myfigure, 'is _ open', 1); % define function f_exit_callback (hobject, eventdata, handles) % hobject handle to f_exit (see gcbo) % eventdata reserved-to be defined in a future version of MATLAB % handles structure with handles and user data (see guidata) Close (findobj ('tag', 'myfigure ')); % Close Main Window

The above is the main program, where the function datacon is the database connection Function

% The pixel position of the image is determined by first obtaining the position of the mouse in the axes and then indexing the color.

% P = get (handles. myaxes, 'currentpoint ');

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.