Control Point Registration using matlab -'fitgeotrans' function - matlab

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.

Related

Activate a specific figure when returning from a nested function

I have a plotting function inside imshow() command.
The plot and the image should be in separate figures.
At the return from the inside function the current figure is of the plot thus imshow() puts the image onto the same figure of the plot and kills the plot.
What can be done to make imshow() open or get to an existing its own figure, while keeping such manner of nested function calling?
Well, apparently, I've found an answer by the time I was done with the question, but because I find it interesting enough and find nothing answering it I'm writing the answer as well.
The algo is like this:
open a figure before imshow(nested_function());
upon starting the nested_function() save the handle to the
previous figure like fh_prev = gcf; for example
do any plots along the nested_function()
before return from the nested_function() activate the previous
figure with the command figure(fh_prev);

how to easy select the pixel's position and pixel value in matlab?

In order to classify remote sensing image by using k-means method in matlab, I want to select some samples in an image with an mouse clicking on it, but I don't know how to code that, could you tell me some method to accomplish it?
you can use impoint, for example:
figure, imshow('pout.tif');
h = impoint(gca,[]);
position = wait(h);
this interactively place a point. Use wait to block the MATLAB command line. Double-click on the point to resume execution of the MATLAB command line.
position is specifying the coordinates of the point [X Y].

How to force drawnow Matlab GUI to draw in new window?

I have used drawnow to draw the characters of the mnist dataset.. which outputs the following output
when i created GUI with matlab and calling drawnow to display images after loading it draws the figure on the open window giving the following output
my question is how to force it to draw in new window ?
drawnow only asks Matlab to flush the event queue and update figure windows; it doesn't determine how and where things are plotted. It's hard to tell since you don't include any code, but in your case it looks like you just plot the character images and the GUI elements into the same figure.
You can control which figure window a graphics operation refers to by setting the "current figure", whose handle is always contained in the variable gcf (graphics: current figure).
You generate a new figure and make it current by calling
figure
If you want to later make this figure current again, you need to save its handle:
fa = figure;
You then make a figure with a given handle current again by
figure(fa)
Some rough sketch of a possible program:
% generate figure windows
fa = figure;
fb = figure;
% plot something in figure a and make the screen update
figure(fa)
plot(...)
drawnow
% put a UI element into figure b and make the screen update
figure(fb)
uicontrol(...)
drawnow

Geometric transformation using MATLAB Function block

I tried to apply a geometric transformation to a video using MATLAB Function block in Simulink. I connected a 'From Multimedia File' block to MATLAB Function block ant its output to a 'To Video Display' block. Inside MATLAB Function Block this code:
function outputImage = fcn(inputImage,theta)
coder.extrinsic('affine2d')
coder.extrinsic('imwarp')
outputImage = coder.nullcopy(inputImage);
tform = affine2d([cosd(theta) -sind(theta) 0; sind(theta) cosd(theta) 0; 0 0 1]);
outputImage = imwarp(inputImage,tform);
where angle theta is a constant block of 10. The code above is an example from 'affine2d' function examples. It returns me following error:
Size mismatch for MATLAB expression 'imwarp'. Expected = 120x160x3 Actual = 146x179x3 Block MATLAB Function (#108) While executing: State During Action
but I don't understand where is the error (why Actual=146x179x3). The input video is a RGB video file ('vipmen.avi') from CV toolbox demos.
EDIT: I solved it: I used Apply Geometric Transformation block for translation and a Rotate block for rotation with:
H = [1 0; 0 1; Ox-X Oy-Y] %where Ox,Oy are image framework origin coordinates and X,Y are offset coordinates.
Indeed after translation, the "new" image framework origin is the (X,Y) point and the Rotate block rotates it about (X,Y).
The Computer Vision System Toolbox includes a block called Apply Geometric Transformation, which will do what you need and save you the trouble of using MATLAB function block.
Edit:
The Apply Geometric Transformation block has been deprecated as of R2015b release. Please use the Warp block instead.
As an image is warped/rotated the number of rows and/or columns in it will change.
You'll need to modify your code so that outputImage is variable sized.
To do this open the code (for the MATLAB Function Block) in the editor and open up the "Edit Data" dialog.
Select outputImage in the left hand column, then
check the "variable size" check box
enter something like [400 400 3] as the size
You have to make a best guess for the maximum row and column size that you expect.
Note also that the Matlab function block must have a discrete sample time for it to use variable sized signals.
To set this, right click on the block and select Block Parameters, then set a sample time.
I solved trying to understand what happens. The deformed image has a dimension different from Output=coder.nullcopy(X) so I calculated the dimension of the new deformed image, create a blank image with Output=coder.nullcopy(ones(new_row,new_colomn,3)) and that works

MATLAB hide datacursor box when using datacursor

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.