Message Boards Message Boards

0
|
2692 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Implementing own interpolation algorithms?

Posted 8 years ago

Hey there, I am pretty new to Mathematica, but I want to implement my own interpolation algorithms. To test them I built a rotate function.

inImage is a matrix containing the position of the Destination Image. rot is the angle of rotation. Offset just is an Offset to rotate around a specific Point.

rotate[inImage_, rot_, 
  Offset_] := {Offset[[1]] + 
   Sqrt[(inImage[[1]] - Offset[[1]])^2 + (inImage[[2]] - 
         Offset[[2]])^2]*
    Cos[ArcTan[(inImage[[1]] - Offset[[1]]), (inImage[[2]] - 
         Offset[[2]])] - rot], 
  Offset[[2]] + 
   Sqrt[(inImage[[1]] - Offset[[1]])^2 + (inImage[[2]] - 
         Offset[[2]])^2]*
    Sin[ArcTan[(inImage[[1]] - Offset[[1]]), (inImage[[2]] - 
         Offset[[2]])] - rot]}

As output i get an array containing the coordinates of the source Image. Now i need a function to build up my destination Image. I tried the following, but i don't know why i cant use the output of my rotate function as part specification.

InImage is the SourceImage and NewPoints is the result of my first Image.

NeaNeigh[NewPoints_, 
   InImage_] := {InImage[[Round[NewPoints[[1]]], 
     Round[NewPoints[[2]]]]]};

Any advice how to fix this?

4 Replies

Thanks for your generald advice, i will clean up my code and try to use better variable names.

Basically rotate is rotating a vector yes. I want to use this to rotate an image. InImage as an input for rotate contains every vector of my destination image. After rotating all those, i have a list with the corresponding vectors of my source image. For example PunkteNeu[[1,1]] is cointaining the position in my source image which will be on position [[1,1]] in my destination image after the rotation. So rotate is working fine, but i need to build a function to do the final interpolation. Nearest Neighbour is fine for now.

My first thoughts gave me something like that:

nearestNeighbourInterpolation[punkteNeu_,sourceImage_]:=sourceImage[[Round[punkteNeu[[1]]],Round[punkteNeu[[2]]]]];

But this wont work. Any Advice here?

Perhaps your code got corrupted with copy-and-paste, with a lot of missing underscores and stars. This is my first attempt at a first clean-up:

(*rotate function*)

rotate[inImage_, rot_, 
   Offset_] := {Offset[[1]] + 
    Sqrt[(inImage[[1]] - Offset[[1]])^2 + (inImage[[2]] - 
          Offset[[2]])^2]*
     Cos[ArcTan[(inImage[[1]] - Offset[[1]]), (inImage[[2]] - 
          Offset[[2]])] - rot], 
   Offset[[2]] + 
    Sqrt[(inImage[[1]] - Offset[[1]])^2 + (inImage[[2]] - 
          Offset[[2]])^2]*
     Sin[ArcTan[(inImage[[1]] - Offset[[1]]), (inImage[[2]] - 
          Offset[[2]])] - rot]};
(*Nearest Neighbour Interpolation*)

NearNeigh[NewPoints_, 
   InImage_] := {InImage[[Round[NewPoints[[1]]], 
     Round[NewPoints[[2]]]]]};

(*Module to rotate an Image by a specific Angle,InterpMethod is \
unused atm*)

Rotier[InImage_, Rotatio_, InterpMethod_] := 
 Module[{OutImage, PunkteNeu, PunkteAlt, ImageHeight, ImageWidth, 
   Offset, Corners, MinX, MaxX, MinY, MaxY},
  {ImageWidth, ImageHeight} = ImageDimensions[InImage];
  (*I want to rotate around middle for now*) 
  Offset = {ImageWidth/2. + 0.5, ImageHeight/2. + 0.5};
  (*Here i check the Corners of the DestinationImage*) 
  Corners = {{1, ImageWidth, 1, ImageWidth}, {1, 1, ImageHeight, 
     ImageHeight}};
  Corners = rotate[Corners, Rotatio, Offset];
  MinX = Floor[Min[Corners[[1, All]]]]; 
  MaxX = Ceiling[Max[Corners[[1, All]]]]; 
  MinY = Floor[Min[Corners[[2, All]]]]; 
  MaxY = Ceiling[Max[Corners[[2, All]]]];
  (*Here i create a Matrix with the coordinates of the destination \
Image so i can inversewarp the image*) 
  PunkteAlt = {Table[i, {i, MinX, MaxX}, {j, MinY, MaxY}], 
    Table[i, {j, MinX, MaxX}, {i, MinY, MaxY}]};
  (*I rotate every coordinate of the destination Image to get the \
coresponding souce image coordinates*)

  PunkteNeu = rotate[PunkteAlt, Rotatio, Offset];
  (*Because i get noninteger values i have to interpolate.but this \
doesn't work atm*) OutImage = NearNeigh[PunkteNeu, InImage];
  Return[OutImage];];(*End Module Rotier*)

Still, I can't figure out how you are going to use these functions. In rotate you seem to assume that InImage is a vector, because you make numerical computations with its first and second element. In Rotier you take the ImageDimensions of InImage, which assumes that InImage is an image, which has no first and second element.

A general advice: inside the code it is better to avoid variable names that start with a capital letter, to avoid conflit with built-in functions. For example, Offset, with capitalized initial, is a built-in function.

POSTED BY: Gianluca Gorni

The rotation function itself works fine. I only got problems with my interpolation. Here is all of my Code. So maybe you can see any mistakes here.

(rotate function)

rotate[inImage, rot, Offset_] := {Offset[[1]] + Sqrt[(inImage[[1]] - Offset[[1]])^2 + (inImage[[2]] - Offset[[2]])^2]* Cos[ArcTan[(inImage[[1]] - Offset[[1]]), (inImage[[2]] - Offset[[2]])] - rot], Offset[[2]] + Sqrt[(inImage[[1]] - Offset[[1]])^2 + (inImage[[2]] - Offset[[2]])^2]* Sin[ArcTan[(inImage[[1]] - Offset[[1]]), (inImage[[2]] - Offset[[2]])] - rot]};

(Nearest Neighbour Interpolation)

NearNeigh[NewPoints, InImage] := {InImage[[Round[NewPoints[[1]]], Round[NewPoints[[2]]]]]};

(Module to rotate an Image by a specific Angle, InterpMethod is unused atm)

Rotier[InImage, Rotatio, InterpMethod_] := Module[{OutImage, PunkteNeu, PunkteAlt, ImageHeight, ImageWidth, Offset, Corners, MinX, MaxX, MinY, MaxY},

{ImageWidth, ImageHeight} = ImageDimensions[InImage];

(I want to rotate around middle for now)

Offset = {ImageWidth/2. + 0.5, ImageHeight/2. + 0.5};

(Here i check the Corners of the DestinationImage)

Corners = {{1, ImageWidth, 1, ImageWidth}, {1, 1, ImageHeight, ImageHeight}};

Corners = rotate[Corners, Rotatio, Offset];

MinX = Floor[Min[Corners[[1, All]]]]; MaxX = Ceiling[Max[Corners[[1, All]]]]; MinY = Floor[Min[Corners[[2, All]]]]; MaxY = Ceiling[Max[Corners[[2, All]]]];

(Here i create a Matrix with the coordinates of the destination Image so i can inversewarp the image)

PunkteAlt = {Table[i, {i, MinX, MaxX}, {j, MinY, MaxY}], Table[i, {j, MinX, MaxX}, {i, MinY, MaxY}]};

(I rotate every coordinate of the destination Image to get the coresponding souce image coordinates)

PunkteNeu = rotate[PunkteAlt, Rotatio, Offset];

(Because i get noninteger values i have to interpolate. but this doesn't work atm)

OutImage = NearNeigh[PunkteNeu, InImage];

Return[OutImage];

]; (End Module Rotier)

By "image" you mean a 2-vector, I presume? Perhaps you can streamline your rotate function using high-level commands such as AngleVector and EuclideanDistance:

rotate[inImage_, rot_, Offset_] := 
 AngleVector[Offset, {EuclideanDistance[inImage, Offset],
   ArcTan @@ (inImage - Offset) - rot}]

To build up a list of "images" you may consider using Table, Append, Join...

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract