How to use cursor and ginput() in GUI?

3 次查看(过去 30 天)
Aidin
Aidin 2013-12-1
I am trying to use cursor from toolbar menu and assign ginput() to it so the user can select a point and later on the plot should be updated accordingly, but now when i point and click on any part of the image, instead of plot and image to be updated a small black square appear on top left of the picture, here is the pointer callback:
function Pointer_ClickedCallback(hObject, eventdata, handles)
% hObject handle to Pointer (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
centersNew=handles.centers;
radiiNew=handles.radius;
A = ginput(1);
B=centersNew;
%compute Euclidean distances:
distances = sqrt(sum(bsxfun(@minus, B, A).^2,2));
%find the smallest distance and use that as an index into B:
closest = B(distances==min(distances),:);
disp(closest);
if (A ~= 0)
for i=1:length(centersNew)
if centersNew(i,1) == closest(1,1)
if centersNew(i,2) == closest(1,2)
centersNew(i,1)=0;
centersNew(i,2)=0;
radiiNew(i,1)=0;
end
end
end
end
centersNew2=snip(centersNew,'0');
radiiNew2=snip(radiiNew,'0');
axes(handles.axes1);
imshow(image);
hold on
viscircles(centersNew2, radiiNew2,'EdgeColor','g');
if (A ~= 0)
cc=num2str(length(centersNew));
end
set(handles.Cell_c,'String',cc);
guidata(hObject, handles);
I have also put the whole guide code, (to test use 30 and 50 as min and max cell radius) sample picture:http://i40.tinypic.com/2hri9si.jpg
function varargout = FYP_GUI_test1(varargin)
% FYP_GUI_TEST1 MATLAB code for FYP_GUI_test1.fig
% FYP_GUI_TEST1, by itself, creates a new FYP_GUI_TEST1 or raises the existing
% singleton*.
%
% H = FYP_GUI_TEST1 returns the handle to a new FYP_GUI_TEST1 or the handle to
% the existing singleton*.
%
% FYP_GUI_TEST1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FYP_GUI_TEST1.M with the given input arguments.
%
% FYP_GUI_TEST1('Property','Value',...) creates a new FYP_GUI_TEST1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before FYP_GUI_test1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to FYP_GUI_test1_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 FYP_GUI_test1
% Last Modified by GUIDE v2.5 27-Nov-2013 10:38:40
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @FYP_GUI_test1_OpeningFcn, ...
'gui_OutputFcn', @FYP_GUI_test1_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 FYP_GUI_test1 is made visible.
function FYP_GUI_test1_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 FYP_GUI_test1 (see VARARGIN)
% Choose default command line output for FYP_GUI_test1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes FYP_GUI_test1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = FYP_GUI_test1_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 load_image.
function load_image_Callback(hObject, eventdata, handles)
% hObject handle to load_image (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global image image2
[path,user_cance]=imgetfile();
if user_cance
msgbox(sprintf('Error'),'Error','Error');
return
end
image=imread(path);
image=im2double(image); %converts to double
image2=image; %for backup process :)
axes(handles.axes1);
imshow(image);
% --- Executes on button press in M_cell_size.
function M_cell_size_Callback(hObject, eventdata, handles)
% hObject handle to M_cell_size (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h = imdistline(gca);
api = iptgetapi(h);
fcn = makeConstrainToRectFcn('imline',...
get(gca,'XLim'),get(gca,'YLim'));
api.setDragConstraintFcn(fcn);
function Min_r_Callback(hObject, eventdata, handles)
% hObject handle to Min_r (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 Min_r as text
% str2double(get(hObject,'String')) returns contents of Min_r as a double
% --- Executes during object creation, after setting all properties.
function Min_r_CreateFcn(hObject, eventdata, handles)
% hObject handle to Min_r (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 Max_r_Callback(hObject, eventdata, handles)
% hObject handle to Max_r (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 Max_r as text
% str2double(get(hObject,'String')) returns contents of Max_r as a double
% --- Executes during object creation, after setting all properties.
function Max_r_CreateFcn(hObject, eventdata, handles)
% hObject handle to Max_r (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 Process.
function Process_Callback(hObject, eventdata, handles)
% hObject handle to Process (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Min_r=str2num(get(handles.Min_r,'String'));
Max_r=str2num(get(handles.Max_r,'String'));
global image
img = rgb2gray(image);
threshold = graythresh(img);
img = im2bw(img,threshold);
img = ~img;
img = imfill(img,'holes');
%figure, imshow(img);
img = bwareaopen(img,150);
%figure, imshow(img);
%d = imdistline;
[centers, radii] = imfindcircles(img,[Min_r Max_r],'Sensitivity',0.95,'Edge',0.8);
%figure, imshow(img);
%figure, imshow(img1);
hold on
viscircles(centers, radii,'EdgeColor','g')
%***this section is for removing false detection of overlap cells***
[centersNew, radiiNew]=RemoveOverLap(centers,radii,40,2);
axes(handles.axes1);
imshow(image);
hold on
viscircles(centersNew, radiiNew,'EdgeColor','g');
cc=num2str(length(centersNew));
% %***this section is for manual Override for deselecting cells***
%
% A = ginput(1);
% B=centersNew;
% %compute Euclidean distances:
% distances = sqrt(sum(bsxfun(@minus, B, A).^2,2));
%
% %find the smallest distance and use that as an index into B:
% closest = B(distances==min(distances),:);
% disp(closest);
%
% if (A ~= 0)
% for i=1:length(centersNew)
% if centersNew(i,1) == closest(1,1)
% if centersNew(i,2) == closest(1,2)
% centersNew(i,1)=0;
% centersNew(i,2)=0;
% radiiNew(i,1)=0;
% end
% end
% end
% end
%
% centersNew2=snip(centersNew,'0');
% radiiNew2=snip(radiiNew,'0');
%
%
% axes(handles.axes1);
% imshow(image);
% hold on
% viscircles(centersNew2, radiiNew2,'EdgeColor','g');
% if (A ~= 0)
% cc=num2str(length(centersNew));
% end
%
%
%
handles.radius=radiiNew;
handles.centers=centersNew;
set(handles.Cell_c,'String',cc);
%handles.count=length(centersNew);
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function Cell_c_CreateFcn(hObject, eventdata, handles)
% hObject handle to Cell_c (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --------------------------------------------------------------------
function Pointer_ClickedCallback(hObject, eventdata, handles)
% hObject handle to Pointer (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
centersNew=handles.centers;
radiiNew=handles.radius;
A = ginput(1);
B=centersNew;
%compute Euclidean distances:
distances = sqrt(sum(bsxfun(@minus, B, A).^2,2));
%find the smallest distance and use that as an index into B:
closest = B(distances==min(distances),:);
disp(closest);
if (A ~= 0)
for i=1:length(centersNew)
if centersNew(i,1) == closest(1,1)
if centersNew(i,2) == closest(1,2)
centersNew(i,1)=0;
centersNew(i,2)=0;
radiiNew(i,1)=0;
end
end
end
end
centersNew2=snip(centersNew,'0');
radiiNew2=snip(radiiNew,'0');
axes(handles.axes1);
imshow(image);
hold on
viscircles(centersNew2, radiiNew2,'EdgeColor','g');
if (A ~= 0)
cc=num2str(length(centersNew));
end
set(handles.Cell_c,'String',cc);
guidata(hObject, handles);

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Visual Exploration 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by