MATLAB hide datacursor box when using datacursor - matlab

I'm using MATLAB to create a GUI. I have an image on which the datacursormode is enabled. This works fine, every time I click a new point is added. For every point MATLAB displays a box with the coordinates (or whatever other text, I modified it using an update function). But how can I remove this text box, I just want a point to be added, no extra information should be displayed?
Thanks!

datacursormode on is used to enable data tip display on a graphic object. In other words that text box you want to hide. What the reason then to use datacursor?
Are you using UpdateFcn of data cursor to "add pixel" (you probably mean to change pixels color)? Consider using ButtonDownFcn callback function instead.
function interactive_image(im)
fh = figure;
hImage = imshow(im);
set(hImage,'ButtonDownFcn',#myfunction)
end
function output_txt = myfunction(obj,eventdata,handles)
pos = get(gca,'CurrentPoint')
x = get(obj,'CData');
x(uint32(pos(1,2)),uint32(pos(1,1))) = 0;
set(obj,'CData',x)
end
UPDATE:
According to your comment you better to use IMPOINT function available in Image Processing Toolbox.

Related

How can I save a GUI window in Matlab(e.g. as png)?

I created a GUI inside Matlab;
To make it easy understandable let's say there is just a static and a non static textfield and also just one push button
In the nonstatic text field one can enter a number a and by pressing the push button we get an answer in the static text field, let's say just 2*a as an answer;
Now I want to have a screenshot of the GUI where there you can see the number a in the non static field and the result in the static text field;
So just how it is shown after pressing the button...
How can I achieve that?
Thanks in advance.. :)
Ok so it's actually more simple than I thought and theScreenCapture option might be overkill in your situation.
What you need is the getframe function, which captures the content of an axes or figure of your choice, here the GUI.
Here is the code for a simple example that you can copy and paste. Upon pressing the button, a new window appears with the content of the GUI. You can replace that part of the code with a call to imwrite to save the image in png format. The useful code is:
%// Capture content of current figure
F = getframe(gcf);
The image data is stored in the cdata property of the structure F, which you can save eg with.
%// imwrite(F.cdata,...)
So the whole code is the following:
function TestGUI
clc
hfigure = figure('Position',[300 300 300 100],'Units','normalized','name','MyFigure');
handles.Edit1= uicontrol('Style','edit','String','','Position',[40 50 50 30]);
handles.Edit2= uicontrol('Style','edit','String','','Position',[100 50 50 30]);
handles.SnapShot= uicontrol('Style','push','String','Snapshot','Position',[160 50 70 30],'Callback',#(s,e) SnapShot_callback);
guidata(hfigure,handles);
%// Callback for button 1
function SnapShot_callback
handles = guidata(hfigure);
%// Double entry from edit box 1. (do whatever you want)
x = str2double(get(handles.Edit1,'String'));
NewX = 2*x;
set(handles.Edit2,'String',NewX);
%// Take the screenshot!
F = getframe(gcf);
figure()
imshow(F.cdata); %// Image data is in F.cdata. You can replace by imwrite(F.cdata,...)
%// Update handles structure.
guidata(hfigure,handles);
end
end
and a screenshot from my computer: on the left is the initial GUI and on the right is the new figure generated with the content of that GUI.

Matlab opens new figure in Callback despite hold

I am creating a GUI with MATLAB's GUIDE. Say, the GUI consists of an axis called axis1
and a slider called slider1. Further say I wanted to plot something (e.g. a box) into axis1 and change the box's height with the slider.
I tried doing this by adding a listener to the slider like so:
hListener = addlistener(handles.slider1,'Value','PostSet',#(src,evnt)boxHeightChange(src,evnt, handles.figure1));
in the GUI's opening function. I further defined:
function boxHeightChange(src, event, figname)
handles = guidata(figname);
% delete "old" box
delete(handles.plottedHandle);
% bring axis in focus
axes(handles.axes1);
% plot the new box (with changed size)
hold on; boxHandle = plotTheBox(event.AffectedObject.Value); hold off
handles.plottedHandle = boxHandle;
% update saved values
guidata(figname, handles);
end
This works, but always opens a new figure to plot the resizable box into instead of drawing into handles.axes1. I do not understand why, since I call axes(handles.axes1); and hold on;
Any idea that might explain the behavior?
I will post a solution to my own question.
Apparently the Callback of a listener is not declared as a "GUI Callback" which is why the GUI can not be accessed from within boxHeightChange if the GUI option "command-line accessibility" is not set to "On".
That means: In GUIDE go to Tools -> GUI options and set "Command-line accessibility" to "On".
Most plotting functions let you pass a name value pair 'Parent', ah where ah specifies the axes to plot on. I think this is the best way to handle your problem. Your actual plotting command seems to be wrapped in the plotTheBox function, so you will have to pass the axes handle in somehow.
Your plot command will look something like this:
plot(a,'Parent',handles.axes1)
You solved the problem a different way on your own, but I think you should do it my way because it's more explicit and it's less likely to lead to unforeseen problems.

Control Point Registration using matlab -'fitgeotrans' function

I'm new to Matlab and I'm trying to do a Control Point Registration using their guide: http://www.mathworks.com/help/images/point-mapping.html
It works fine until the 'fitgeotrans' function where I get an error that saying:
'Undefined function or variable 'input_points'.
I read the Matlab help and the previous "cpselect" function gives me two nX2 arrays with the coordinates which are saved (by the 'cpselect' function) in two array variables 'input_points' and 'base_points'. So, I really don't understand why the next function can't "See" them and considers them 'Undefined'.
My code is attached bellow. Thank you for your help.
function [Y] =EBL
ReferenceImg=imread('GFI.jpg'); %This is the fixed image
CroppedImg=imcrop(ReferenceImg); %Crop the image
close %close the imcrop window
ResizedIReferenceImg= imresize(CroppedImg,[1000 1000]); %re-size the fixed image
t=imagesc(ResizedIReferenceImg); %Set transparency of fixed image
set(t,'AlphaData',0.5);
hold on
I = imread('GF.bmp'); %This is the moving picture
MovingImg = imrotate(I,-5,'nearest','crop'); % Rotate the moving picture cw
ResizedMovingImg= imresize(MovingImg,[1000 1000]); %re-size the moving image
h=imagesc(ResizedMovingImg); %Set transparency of moving image
set(h,'AlphaData',0.6);
close
cpselect(ResizedMovingImg,ResizedIReferenceImg); %Alignment
tform = fitgeotrans(input_points,base_points,'NonreflectiveSimilarity');
The problem is that MATLAB doesn't, by default, wait for you to be done before moving on from cpselect. So it simply moves on from cpselect to tform before you have a chance to actually select any points, at which point input_points doesn't yet exist. You have to set the Wait parameter, and doing so also affects the outputs. With Wait on, call cpselect something like this:
[input_points,base_points] = cpselect(MovingImg,ReferenceImg,'Wait', true);
When calling cpselect in this way, you will not have the "Export Points to Workspace" option. Instead, the selected points will be output into the variables input_points,base_points when the cpselect window is closed.

Interactively change color of data point in Matlab plot

I am working on Matlab Plot. I have two problems.
1) After plotting, when user selects data point color of that data point should change
2) I need to get the x and y values of that data point
Any Ideas?
Use the Data Cursor in the toolbar.
For the first case to get the user input you may try ginput And for the second case, if you are showing an image then please use imtool. It will show you the pixel location as well as pixel value.
The question is 4 years old, but a full answer may be helpful to someone so here it goes...
Plot your data:
plot(rand(5,1),'.b','MarkerSize',40) % Large blue dots just to make it clear
hold on
Create a datacursor object:
dcm_obj = datacursormode(gcf);
Set a custom update function to the data cursor:
set(dcm_obj,'UpdateFcn',#dcfun)
and then define the function:
function txt = dcfun(~,event_obj)
pos = event_obj.Position;
delete(findall(gcf,'Tag','DEL'))
plot(gca,pos(1),pos(2),'.r','Markersize',40,'Tag','DEL')
txt = cell(2,1);
txt{1} = ['x: ',num2str(pos(1))];
txt{2} = ['y: ',num2str(pos(2))];
Now just click the data cursor tool in the toolbar, and click on a data point.

MATLAB ButtonDownFcn

I have a project of "Optical Character Recognition" in MATLAB and i need your help:
how do i recognize when the user press the mouse on an image?
i trying to do this with ButtonDownFcn but even when i just printing message
the message is not printed.
i want to allow the user to select the license plate from the image.
how can i do this and save the Pixels of the selected area?
thanks in advance.
Addressing your two questions:
I'm guessing that you are trying to set the 'ButtonDownFcn' of the figure window, which won't work how you expect it to. If you want to do something when the user clicks on an image, you should make sure that you're setting the 'ButtonDownFcn' of the image, and not the figure window or the axes object. Note this line in the figure property documentation (emphasis added by me):
Executes whenever you press a mouse button while the pointer is in the figure window, but not over a child object (i.e., uicontrol, uipanel, axes, or axes child).
This is why you have to set a 'ButtonDownFcn' for each object you want it to work for. Setting it for the figure window won't make it work automatically for each object in the figure. Here's an example that sets the 'ButtonDownFcn' for the figure and an image object:
img = imread('peppers.png'); %# Load a sample image
hFigure = figure; %# Create a figure window
hImage = image(img); %# Plot an image
set(hFigure,'ButtonDownFcn',... %# Set the ButtonDownFcn for the figure
#(s,e) disp('hello'));
set(hImage,'ButtonDownFcn',... %# Set the ButtonDownFcn for the image
#(s,e) disp('world'));
Notice how clicking inside and outside the image displays a different message, since each calls the 'ButtonDownFcn' for a different object. Notice also that if you click on the tick mark label of one of the axes, nothing is displayed. This is because the axes object has its own 'ButtonDownFcn', which wasn't set to anything.
If you have access to the Image Processing Toolbox you can use the function IMFREEHAND to have the user draw an ROI (region of interest) in the image. Then you can use the createMask method to create a binary mask of the image with ones for pixels inside the ROI and zeroes for pixels outside the ROI.