www.pudn.com > Binary.rar > MYBINARY.M


function varargout = mybinary(varargin) 
% MYBINARY M-file for mybinary.fig 
%      MYBINARY, by itself, creates a new MYBINARY or raises the existing 
%      singleton*. 
% 
%      H = MYBINARY returns the handle to a new MYBINARY or the handle to 
%      the existing singleton*. 
% 
%      MYBINARY('CALLBACK',hObject,eventData,handles,...) calls the local 
%      function named CALLBACK in MYBINARY.M with the given input arguments. 
% 
%      MYBINARY('Property','Value',...) creates a new MYBINARY or raises the 
%      existing singleton*.  Starting from the left, property value pairs are 
%      applied to the GUI before mybinary_OpeningFunction gets called.  An 
%      unrecognized property name or invalid value makes property application 
%      stop.  All inputs are passed to mybinary_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 mybinary 
 
% Last Modified by GUIDE v2.5 17-Jun-2003 16:00:47
 
% Begin initialization code - DO NOT EDIT 
 
gui_Singleton = 1; 
gui_State = struct('gui_Name',       mfilename, ... 
                   'gui_Singleton',  gui_Singleton, ... 
                   'gui_OpeningFcn', @mybinary_OpeningFcn, ... 
                   'gui_OutputFcn',  @mybinary_OutputFcn, ... 
                   'gui_LayoutFcn',  [] , ... 
                   'gui_Callback',   []); 
if nargin & isstr(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 mybinary is made visible. 
function mybinary_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 mybinary (see VARARGIN) 
 
% Choose default command line output for mybinary 
handles.output = hObject; 
 
% Update handles structure 
guidata(hObject, handles); 
 
% UIWAIT makes mybinary wait for user response (see UIRESUME) 
% uiwait(handles.figure1); 
 
 
% --- Outputs from this function are returned to the command line. 
function varargout = mybinary_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 during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

% --- Executes during object creation, after setting all properties. 
function filename_CreateFcn(hObject, eventdata, handles) 
% hObject    handle to filename (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 
    set(hObject,'BackgroundColor','white'); 
else 
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); 
end 
 
 
 
function filename_Callback(hObject, eventdata, handles) 
% hObject    handle to filename (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 filename as text 
%        str2double(get(hObject,'String')) returns contents of filename as a double 
filename=get(hObject,'string'); 
handles.my_filename=filename; 
guidata(hObject, handles); 

% --- Executes on slider movement.


% --- Executes during object creation, after setting all properties.
function Binaryshow_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Binaryshow (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function Binaryshow_Callback(hObject, eventdata, handles)
% hObject    handle to Binaryshow (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 Binaryshow as text
%        str2double(get(hObject,'String')) returns contents of Binaryshow as a double
%string=data; 


% --- Executes on button press in read.
function Read_Callback(hObject, eventdata, handles)
% hObject    handle to read (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA) 
filename=handles.my_filename;
axes(handles.axes1); 
IM=imread(filename,'tif'); 
handles.my_image=IM; 
guidata(hObject, handles); 
imshow(IM);

% --- Executes on button press in Threshold.
function Threshold_Callback(hObject, eventdata, handles)
% hObject    handle to Threshold (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
IM=handles.my_image;
level=graythresh(IM); 
handles.my_level=level; 
guidata(hObject, handles); 
 
bw1=im2bw(IM,level); 
handles.newbw=bw1; 
guidata(hObject, handles); 
axes(handles.axes2); 
imshow(bw1); 
set(handles.slider1,'Value',level*255); 
set(handles.Binaryshow,'String',num2str(fix(255*level)));
% --- Executes on button press in Seedfill.


function slider1_Callback(hObject, eventdata, handles) 
% hObject    handle to slider1 (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,'Value') returns position of slider 
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider 
%slider_step(1) = 1; 
%slider_step(2) = 0; 
%level=handles.my_level; 
%set(handles.slider1,'sliderstep',slider_step,'max',255,'min',0,'Value',128 
%); 
newlevel=get(handles.slider1,'Value'); 
newlevel1=newlevel; 
newlevel=newlevel/255; 
%newlevel1=double(newlevel1); 
newlevel1=fix(newlevel1); 
%set(handles.Binaryshow,'String',num2str(get(handles.slider1,'Value'))); 
set(handles.Binaryshow,'String',num2str(newlevel1)); 
IM=handles.my_image; 
 
bw1=im2bw(IM,newlevel); 
handles.newbw=bw1; 
guidata(hObject, handles); 
axes(handles.axes2); 
imshow(bw1); 
 
function Seedfill_Callback(hObject, eventdata, handles) 
% hObject    handle to Seedfill (see GCBO) 
% eventdata  reserved - to be defined in a future version of MATLAB 
% handles    structure with handles and user data (see GUIDATA) 
bw1=handles.newbw; 
bw1=imcomplement(imfill(imcomplement(bw1),'holes')); 
%bw2=bwmorph(bw1,'hbreak',5); 
%bw3=bwmorph(bw2,'clean',5); 
axes(handles.axes2); 
imshow(bw1); 
handles.newbw=bw1; 
guidata(hObject, handles);


% --- Executes on button press in Inversecolor.
function Inversecolor_Callback(hObject, eventdata, handles)
% hObject    handle to Inversecolor (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA) 
IM=handles.my_image; 
bw1=handles.newbw; 
sx=size(IM); 
MM=sx(1); 
NN=sx(2); 
i=1:MM; 
j=1:NN; 
xx(i,j)=1; 
bw1=xx-bw1;
%bw1=bwmorph(bw1,'hbreak',5); 
%bw3=bwmorph(bw2,'clean',5); 
axes(handles.axes2); 
imshow(bw1);
handles.newbw=bw1; 
guidata(hObject, handles);
% --- Executes on button press in Select.
function Select_Callback(hObject, eventdata, handles)
% hObject    handle to Select (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA) 
bw1=handles.newbw; 
%bw1=bwmorph(bw1,'hbreak',5); 
%bw1=bwmorph(bw1,'remove',1); 
%c=[50 60 128]; 
%r=[30 90 128]; 
bw1=bwselect(bw1,8); 
axes(handles.axes2); 
imshow(bw1);
handles.newbw=bw1; 
guidata(hObject, handles);

% --- Executes on button press in Erode.
function Erode_Callback(hObject, eventdata, handles)
% hObject    handle to Erode (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
bw1=handles.newbw; 
bw1=bwmorph(bw1,'erode'); 
axes(handles.axes2); 
imshow(bw1); 
handles.newbw=bw1; 
guidata(hObject, handles);

% --- Executes on button press in Dilation.
function Dilation_Callback(hObject, eventdata, handles)
% hObject    handle to Dilation (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

bw1=handles.newbw; 
bw1=bwmorph(bw1,'dilate',1); 
axes(handles.axes2); 
imshow(bw1); 
handles.newbw=bw1; 
guidata(hObject, handles);


% --- Executes on button press in Save.
function Save_Callback(hObject, eventdata, handles)
% hObject    handle to Save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

bw1=handles.newbw; 
savebw=bw1; 
savebw=255*savebw; 
savebw=uint8(savebw); 
imwrite(savebw,'trans.tif');


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


% --------------------------------------------------------------------
function Open_Callback(hObject, eventdata, handles)
% hObject    handle to Open (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename1, pathname, filterindex] = uigetfile(... 
    {'*.tif;*.bmp;*.jpg','All Image Files(*.tif,*.bmp,*.jpg)'; 
     '*.tif','TIF image file(*.tif)';... 
     '*.bmp','BMP image file(*.bmp)';... 
     '*.jpg','JPEG image file(*.jpg)'},... 
     'Open an image'); 
if ~isequal(filename1, 0) 
    filename=fullfile(pathname,filename1); 
    axes(handles.axes1); 
    IM=imread(filename); 
    handles.my_image=IM; 
    guidata(hObject, handles); 
    imshow(IM);;    
end

% --------------------------------------------------------------------
function Saveas_Callback(hObject, eventdata, handles)
% hObject    handle to Saveas (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%filename2 = uiputfile('*.tif'); 
filterindex1=1; 
[filename2, pathname1, filterindex1] = uiputfile(... 
    {'*.tif;*.bmp','All Image Files(*.tif,*.bmp)'; 
     '*.tif','TIF image file(*.tif)';... 
     '*.bmp','BMP image file(*.bmp)'},... 
     'Save image','transw.tif');
if ~isequal(filename2, 0) 
    filenameg=fullfile(pathname1,filename2); 
    bw1=handles.newbw; 
    savebw=bw1; 
    savebw=255*savebw; 
    savebw=uint8(savebw); 
    %if filterindex1==1    
    imwrite(savebw,filenameg);  
       %elseif filterindex1==2 
       %imwrite(savebw,filenameg,'bmp'); 
       %elseif filterindex1==3 
       %imwrite(savebw,filenameg,'jpg'); 
       %end 
end 
%disp(filterindex1);
% --------------------------------------------------------------------
function Close_Callback(hObject, eventdata, handles)
% hObject    handle to Close (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],... 
                     ['Close ' get(handles.figure1,'Name') '...'],... 
                     'Yes','No','Yes'); 
if strcmp(selection,'No') 
    return; 
end 
 
delete(handles.figure1)
% --------------------------------------------------------------------


% --------------------------------------------------------------------
function Processing_Callback(hObject, eventdata, handles)
% hObject    handle to Processing (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------


% --------------------------------------------------------------------
function Help_Callback(hObject, eventdata, handles)
% hObject    handle to Help (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Chelp_Callback(hObject, eventdata, handles)
% hObject    handle to Chelp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA) 
HelpPath = which('helpmybinary.htm'); 
web(HelpPath);
%open();

% --------------------------------------------------------------------
function Version_Callback(hObject, eventdata, handles)
% hObject    handle to Version (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
open('Version.fig');

% --------------------------------------------------------------------
function Thin_Callback(hObject, eventdata, handles)
% hObject    handle to Thin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
bw1=handles.newbw; 
bw1=bwmorph(bw1,'thin',2); 
axes(handles.axes2); 
imshow(bw1); 
handles.newbw=bw1; 
guidata(hObject, handles);

% --------------------------------------------------------------------
function Thicken_Callback(hObject, eventdata, handles)
% hObject    handle to Thicken (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
bw1=handles.newbw; 
bw1=bwmorph(bw1,'thicken',2); 
axes(handles.axes2); 
imshow(bw1); 
handles.newbw=bw1; 
guidata(hObject, handles);

% --------------------------------------------------------------------
function Skelecton_Callback(hObject, eventdata, handles)
% hObject    handle to Skelecton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
bw1=handles.newbw; 
bw1=bwmorph(bw1,'skel',5); 
axes(handles.axes2); 
imshow(bw1); 
handles.newbw=bw1; 
guidata(hObject, handles);

% --------------------------------------------------------------------
function Shrink_Callback(hObject, eventdata, handles)
% hObject    handle to Shrink (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
bw1=handles.newbw; 
bw1=bwmorph(bw1,'shrink',5); 
axes(handles.axes2); 
imshow(bw1); 
handles.newbw=bw1; 
guidata(hObject, handles);