Homographic image transformation issue for sattelite images - matlab

I want to apply homography to the satellite images. I found this post quite helpful. So I decided to use the same Matlab code.
im = imread('cameraman.tif');
n = [0;0;-1];
d = Inf
theta = 60*pi/180;
R = [ 1 0 0 ;
0 cos(theta) -sin(theta);
0 sin(theta) cos(theta)];
t = [0;0;0];
K=[300 0 0;
0 300 0;
0 0 1];
H=K*R/K-1/d*K*t*n'*K;
img=imagehomog(im,H','c');
figure;imshow(img)
but the output is just the small box.
I am using MATLAB 2015b
EDIT
Homography using imtransform and maketform
n = [0;0;-1];
d = Inf;
im = imread('cameraman.tif');
theta = 60*pi/180;
R = [ 1 0 0 ;
0 cos(theta) -sin(theta);
0 sin(theta) cos(theta)];
t = [0;0;0];
K=[300 0 0;
0 300 0;
0 0 1];
H=K*R/K-1/d*K*t*n'*K;
tform = maketform('projective',H');
imT = imtransform(im,tform);
imshow(imT)
Output
How can I do it from the center. Something like this

Related

Building newton iteration in MATLAB

I seem to get the error "Warning: Matrix is singular to working precision." when trying to get delta_x. It should be using 5x1 and 5x5 matrices.
clc; close all; clear all;
phi = 1;
delta_x = 1;
error = 10e-15;
x = [ 0; 0; 0; 0; 0];
n =1;
B =0.025;
while norm(phi)>= error && norm(delta_x) >= error
G = [ 40e3 -20e3 -20e3 0 1; -20e3 20e3 0 0 0; -20e3 0 20e3 0 0; 0 0 0 0 0; 0 0 0 0 0];
fx = [ 0;
B*((-x(4)-0.7)*(x(2)-x(4))-(((x(2)-x(4))^2)/2));
B*((-x(4)-0.7)*(x(3)-x(4))-(((x(3)-x(4))^2)/2));
-B*((-x(4)-0.7)*(x(2)-x(4))-(((x(2)-x(4))^2)/2))- B*((-x(4)-0.7)*(x(3)-x(4))-(((x(3)-x(4))^2)/2));
0];
b = [ 0; 0; 0; 200e-6; 2.5];
dfx = [ 0 0 0 0 0;
0 -B*(0.7+x(2)) 0 B*(0.7+x(4)) 0;
0 0 -B*(0.7+x(3)) B*(0.7+x(4)) 0;
0 B*(0.7+x(2)) B*(0.7+x(3)) -2*B*(0.7+x(2)) 0;
0 0 0 0 0];
phi = G*x + fx - b;
m = G + dfx;
delta_x = -m\phi;
x = x+delta_x;
norm_delta_x(n) = norm(delta_x);
norm_phi(n) = norm(phi);
n = n+1;
end
The dimensions of matrices 5x1 and 5x5 are fine, but what you are doing in the step delta_x = -m\phi is solving for an inverse of matrix m. Since m is a matrix that is singular (try running det(m) and you will get a zero), an inverse matrix does not exist. Matlab sees this and notifies you by telling you "Matrix is singular to working precision".

MATLAB Yalmip: unable to run optimizer; error in eliminatevariables

Bottomline:
Matlab throws the errors below and it is not obvious to me what is the root cause. The problem seems to reside in the input arguments, but I cannot figure out exactly what it is. I would greatly appreciate any help in finding it.
Index exceeds matrix dimensions.
Error in eliminatevariables (line 42)
aux(model.precalc.index2) = value(model.precalc.jj2);
Error in optimizer/subsref (line 276)
[self.model,keptvariablesIndex] =
eliminatevariables(self.model,self.model.parameterIndex,thisData(:),self.model.parameterIndex);
Error in SCMv0_justrun (line 68)
[solutions,diagnostics] = controller{inputs};
Background:
I am trying to program a Model Predictive Control but I am not very familiar yet with either Yalmip or mathematical optimization algorithms. I have made sure that the defined inputs and the actual inputs have the same dimensions, hence why I am surprised that the error has to do with matrix dimensions.
The error originates in when my code calls the optimizer.
My code is based on: https://yalmip.github.io/example/standardmpc/
Here is my code (the first part of the code is only needed to define the optimization problem and is marked between "%%%%%"; the error occurs near the end):
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yalmip('clear')
clear all
% Model data
A = eye(3);
B = [1 0 -1 0 0 0 0; 0 1 0 -1 0 0 0; 0 0 0 0 1 -1 -1];
nx = 3; % Number of states
nu = 7; % Number of inputs
% MPC data
Q = [10 10 20]';
R = [10 10 1 1 5 3 3]';
C = [50 0; 0 30];
N = 90;
ny = 2;
E = [0 0 0 0 0 1 0; 0 0 0 0 0 0 1];
u = sdpvar(repmat(nu,1,N),repmat(1,1,N));
x = sdpvar(repmat(nx,1,N+1),repmat(1,1,N+1));
r = sdpvar(repmat(ny,1,N+1),repmat(1,1,N+1));
d = sdpvar(ny,1);
pastu = sdpvar(nu,1);
dx = 0.05;
Gx=[-1*eye(3);eye(3)];
gx = [0 0 0 500 500 1000]';
COVd = [zeros(5,7);0 0 0 0 0 10 0; 0 0 0 0 0 0 10];
COVx = zeros(nx,nx);
auxa = eye(5);
auxb = zeros(5,2);
Gu = [-1*eye(7,7); auxa auxb;0 0 0 0 0 1 1];
gu = [zeros(7,1); 200; 200; 50; 50; 100; 500];
Ga = [0 0 0.5 0.5 -1 0 0];
constraints = [];
objective = 0;
for k = 1:N
r{k} = r{k} + d;
objective = objective + Q'*x{k} + R'*u{k} + (r{k}-E*u{k})'*C*(r{k}-E*u{k});
constraints = [constraints, x{k+1} == A*x{k}+B*u{k}];
COVx = A*COVx*A' + B*COVd*B';
COVGx = Gx*COVx*Gx';
StDevGx = sqrt(diag(COVGx));
chance = gx - norminv(1-dx/(length (gx)*N))*StDevGx;
constraints = [constraints, Ga*u{k}==0, Gu*u{k}<=gu, Gx*x{k}<=gx-chance];
end
objective = objective + Q'*x{N+1};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
parameters_in = {x{1},[r{:}],d,pastu};
solutions_out = {[u{:}], [x{:}]};
controller = optimizer(constraints, objective,[],parameters_in,solutions_out);
x = 100*ones(nx,1);
clf;
disturbance = randn(ny,1)*10;
oldu = zeros(nu,1);
hold on
for i = 1:150
future_r = [4*sin((i:i+N)/40);3*sin((i:i+N)/20)];% match dimensions of r
inputs = {x,future_r,disturbance,oldu};
[solutions,diagnostics] = controller{inputs};
U = solutions{1};oldu = U(1);
X = solutions{2};
if diagnostics == 1
error('The problem is infeasible');
end
x = A*x+B*u;
end
It's a bug in the latest version of YALMIP.

Generating a 3D binary mask of geometric shapes in Matlab

I would like to generate a 3D binary mask which represents an ellipsoid with centers xc,yc,zc and radiuces xr,yr,zr.
I noticed that the function ellipsoid generates a mesh of points given these parameters. However, I want the data to be represented by a binary matrix (in my case, of size [100,100,100]), and not a mesh.
My Parameters are:
mask = zeros(100,100,100);
xc = 50; yc = 50; zc = 50;
xr = 15; yr = 15; zr = 15;
Thanks in advance!
To generate a binary mask of shapes which can use an equation you can follow the steps:
Generate a mesh (with ndgrid). Make sure the domain limits includes the volume/surface mask, and choose the mesh resolution according to your needs.
Use the volume/surface equation to generate a binary mask, by doing a simple logical comparison of the coordinates with the equation.
Simple 2D example:
Let's define a simple ellipse (in 2D).
%% // Simple 2D example
xc = 5 ; yc = 6 ; %// ellipse center = (5,6)
xr = 3 ; yr = 2 ; %// ellipse radiuses
xbase = linspace(0,10,11) ; %// temporary variable used to send to "ndgrid"
[xm,ym] = ndgrid( xbase , xbase ) ; %// generate base mesh
mask = ( ((xm-xc).^2/(xr.^2)) + ((ym-yc).^2/(yr.^2)) <= 1 ) %// get binary mask
Gives you the binary mask:
mask =
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 1 1 0 0 0
0 0 0 0 0 1 1 1 0 0 0
0 0 0 0 1 1 1 1 1 0 0
0 0 0 0 0 1 1 1 0 0 0
0 0 0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
Granted you can hardly recognise an ellipse in the mask but I kept the resolution low to be able to display it as text. You can easily increase the resolution by defining a finer mesh.
3D Ellipsoid:
Well, it's exactly the same method, except we are going to add one dimension to the mesh, and use the 3D equation of the ellipsoid. So for your precise case:
%% // 3D ellipsoid
xc = 50; yc = 50; zc = 50;
xr = 15; yr = 15; zr = 15;
xbase = linspace(1,100,100) ;
[xm,ym,zm] = ndgrid( xbase , xbase , xbase) ;
mask = ( ((xm-xc).^2/(xr.^2)) + ((ym-yc).^2/(yr.^2)) + ((zm-zc).^2/(zr.^2)) <= 1 ) ;
I cannot show you a text output with these kind of 3D arrays, but your mask is now a 3D logical array containing true inside the ellipsoid and false elsewhere.
If I understood your question correctly, this should work:
mask = zeros(100, 100, 100);
%//your ellipsoid properties
xc = 50; yc = 50; zc = 50;
xr = 15; yr = 15; zr = 15;
for x=1:100
for y=1:100
for z=1:100
if ( ((x-xc)/xr)*((x-xc)/xr) + ((y-yc)/yr)*((y-yc)/yr) + ((z-zc)/zr)*((z-zc)/zr) < 1 )
mask(x,y,z) = 1; %//set elements within ellipsoid to 1
end
end
end
end
You can do this with Heaviside functions, this probably needs a bit more thought to get exactly what you want but as a start,
close all
clear all
%Setup Domain
maxdomain = 100;
mindomain = 0.;
step = 1.0;
X = mindomain:step:maxdomain;
Y = mindomain:step:maxdomain;
Z = mindomain:step:maxdomain;
[x,y,z] = meshgrid(X,Y,Z);
xc = 50; yc = 50; zc = 50;
xr = 15; yr = 15; zr = 15;
r2 = xr/2;
r = sqrt((x-xc).^2/xr + (y-yc).^2/yr + (z-zc).^2/zr);
u = heaviside(r-r2);
%Plot Surface of sphere
p = patch(isosurface(x,y,z,u));
isonormals(x,y,z,u,p)
set(p,'FaceColor','red','EdgeColor','none');
camlight ; alpha(0.6);
xlabel('x'); ylabel('y'); zlabel('z');
daspect([1,1,1]); view(3);
axis tight; camlight; camlight(-80,-10);
lighting gouraud;
which for your values above looks like,
and forxr = 15; yr = 45; zr = 15;,
The Heaviside function can be defined using,
function [out]=heaviside(x)
out=0.5.*(sign(x)+1.0);
end
if the Symbolic Math Toolbox is not available.

Why does my 3 axes system coordinate orientation change x with y values?

I am using Matlab and Euler Angles in order to reorient a 3axes coordinate system. Specifically,
Rz = [cos(ψ) sin(ψ) 0;-sin(ψ) cos(ψ) 0;0 0 1];
Ry = [cos(φ) 0 -sin(φ);0 1 0;sin(φ) 0 cos(φ)];
Rx = [1 0 0;0 cos(θ) -sin(θ);0 sin(θ) cos(θ)];
Rtotal = Rz*Ry*Rz
Then I loop through my old system coordinates (x,y,z) and make a vector coord_old. Then I get the reoriented system with (xn,yn,zn)
for i=1:size(num,1)
coord_old = [x(i,1);y(i,1);z(i,1)];
coord_new = Rtotal*coord_old;
xn(i,1) = coord_new(1,1);
yn(i,1) = coord_new(2,1);
zn(i,1) = coord_new(3,1);
end
My issue is that when θ,φ,ψ≃0 then x->-y and y->x and when θ,φ≃0 and ψ=90 then x and y will not rotate! That means that when x,y should rotate they don't and when they shouldn't rotate they stay as they were!
--EDIT--
For example, when ψ=20.0871, φ=0.0580 and θ=0.0088 I get these results
See that x->-y and y->x while z doesn't change at all!
Any thoughts?
Ok, I see two main problems here:
Rtotal = Rz*Ry*Rz is probably not what you want since Rz is multiplied twice. I think you mean Rtotal = Rz*Ry*Rx.
Your rotation matrix seems to be incorrect. Check this Wikipedia artice to get the correct signs.
Here a corrected rotation matrix:
Rz = [cos(psi) -sin(psi) 0; sin(psi) cos(psi) 0; 0 0 1];
Ry = [cos(phi) 0 sin(phi); 0 1 0; -sin(phi) 0 cos(phi)];
Rx = [1 0 0; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)];
Rtotal = Rz*Ry*Rx;
With this matrix I get the correct results:
x=1; y=2; z=3;
psi=0; phi=0; theta=0;
[xn,yn,zn] >> 1 2 3
x=1; y=2; z=3;
psi=90/180*pi; phi=0; theta=0;
[xn,yn,zn] >> -2 1 3
And here a full graphical example of a cube in 3d-space:
% Create cube (not in origin)
DVert = [0 0 0; 0 1 0; 1 1 0; 1 0 0 ; ...
0 0 1; 0 1 1; 1 1 1; 1 0 1];
DSide = [1 2 3 4; 2 6 7 3; 4 3 7 8; ...
1 5 8 4; 1 2 6 5; 5 6 7 8];
DCol = [0 0 1; 0 0.33 1; 0 0.66 1; ...
0 1 0.33; 0 1 0.66; 0 1 1];
% Rotation angles
psi = 20 /180*pi; % Z
phi = 45 /180*pi; % Y
theta = 0 /180*pi; % X
% Rotation matrix
Rz = [cos(psi) -sin(psi) 0; sin(psi) cos(psi) 0; 0 0 1];
Ry = [cos(phi) 0 sin(phi); 0 1 0; -sin(phi) 0 cos(phi)];
Rx = [1 0 0; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)];
Rtotal = Rz*Ry*Rz;
% Apply rotation
DVertNew = Rtotal * DVert';
% Plot cubes
figure;
patch('Faces',DSide,'Vertices',DVert,'FaceColor','flat','FaceVertexCData',DCol);
patch('Faces',DSide,'Vertices',DVertNew','FaceColor','flat','FaceVertexCData',DCol);
% Customize view
grid on;
axis equal;
view(30,30);
When I use your code and insert 0 for all angles, I get Rtotal:
Rtotal =
1 0 0
0 1 0
0 0 1
This is the identity matrix and will not change your values.
You have an error in your matrix multiplication. I think you should multiply: Rtotal*coord_old. I think you are missing the _old. depending on what is in you coordvariable, this may be the bug.
When I run:
for i=1:size(1,1)
coord_old = [1;2;3];
coord_new = Rtotal*coord_old;
xn(i,1) = coord_new(1,1);
yn(i,1) = coord_new(2,1);
zn(i,1) = coord_new(3,1);
end
I get the correct result:
coord_new =
1
2
3
Thank you both #Steffen and #Matt. Unfortunately, my reputation is not high enough to vote Up your answers!
The problem was not with Rtotal as #Matt correctly stated. It should be as it was Rz*Ry*Rx. However, both your ideas helped me test my code with simple examples (5 sets of coordinates and right hand rule), and realize where my (amateur) mistake was.
I had forgotten I had erased parts of codes where I was expressing my angles to degrees... I should be using sind & cosd instead of sin and cos.

"Contour not rendered for non-finite ZData"

I'm trying to plot a frequency characteristic equation using ezplot, but Matlab gives the following warning, "Contour not rendered for non-finite ZData". I have used this command to plot frequency equations previously but now I get a warning and the plot display is empty and it does not change the axis range as well. Can someone please help. Would be much appreciated.
Here's the code i'm using.
% Transfer Matrix for Case-I, thin rotor
clear all;
clc;
EI = 1626;
l = 0.15;
m = 0.44108;
It = 2.178*10^-4;
I_p = 2.205*10^-5;
Itr = 0.24;
I_pr = 0.479;
syms p n;
F = [1 l*1i l^2/(2*EI)*1i l^3/(6*EI);
0 1 l/EI -l^2/(2*EI)*1i;
0 0 1 -l*1i;
0 0 0 l];
P = [ 1 0 0 0;
0 1 0 0;
0 -It*p^2+I_p*n*p 1 0;
-m*p^2 0 0 1];
P_r = [1 0 0 0;
0 1 0 0;
0 -Itr*p^2+I_pr*n*p 1 0;
-m*p^2 0 0 1];
A = F*P*F*P*F*P*F;
B = P_r*F*P*F*P*F;
r = A(1,2)/A(1,4);
a12_p = 0;
a22_p = A(2,2)-r*A(2,4);
a32_p = A(3,2)-r*A(3,4);
a42_p = A(4,2)-r*A(4,4);
Ap(2,2) = a22_p;
Ap(3,2) = a32_p;
Ap(4,2) = a42_p;
Ap(4,4) = 1;
C = B*Ap;
M = [C(3,2) C(3,4);
C(4,2) C(4,4)];
sol = det(M);
ezplot(sol,[-2*10^10 2*10^10]);
The sol is displayed if u ask for it but the plot doesn't display.
Thanks in advance for ur help !! Much appreciated.