gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master e3acd15 4/4: ImageWarp can rotate and scale wi


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master e3acd15 4/4: ImageWarp can rotate and scale without matrix
Date: Tue, 4 Oct 2016 18:25:15 +0000 (UTC)

branch: master
commit e3acd1588117931ff52e80edfb8775bd22f2f635
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    ImageWarp can rotate and scale without matrix
    
    Writing the matrix manually on the command-line can be both annoying and
    buggy, even a 2 by 2 matrix. For scaling you have to check if the values
    are placed properly and the two values on the diagonal are equal. For
    rotation you have to calculate the sine and cosine and repeat them which is
    very annoying and horribly buggy.
    
    So with this commit two new options are added to ImageWarp: `--roate' and
    `--scale'. They only take one value (the angle in degrees and the scaling
    factor respectively) and create the matrix internally.
    
    Currently only one such operations can be defined in each run of
    ImageWarp. So although this commit was done in the spirit of task #13562
    ("Separate warp options"), it does not complete it. In that task, we
    planned to allow the user to sequentially give any number of scaling,
    rotation, translation and etc operations to be done on the image. But that
    job is a little too complicated for now (I don't have time).
    
    However, the infra-structure is now in place to do the work easily: the
    separate matrices are read in `ui.c'. To make task #13562 a reaily, we just
    have to build 3 by 3 matices for each step and apply the FITS position
    correction before and after all subsequent options. A linked list can also
    keep the separate values.
---
 bin/imgwarp/args.h |   42 ++++++++++++++++++++------
 bin/imgwarp/main.h |    4 +++
 bin/imgwarp/ui.c   |   85 +++++++++++++++++++++++++++++++++++++++++-----------
 doc/gnuastro.texi  |   38 +++++++++++++++++------
 4 files changed, 134 insertions(+), 35 deletions(-)

diff --git a/bin/imgwarp/args.h b/bin/imgwarp/args.h
index e985855..dbaa94a 100644
--- a/bin/imgwarp/args.h
+++ b/bin/imgwarp/args.h
@@ -71,7 +71,7 @@ const char doc[] =
 
 /* Available letters for short options:
 
-   c e f g i j k l p r s t u v w x y
+   c e f g i j k l p t u v w x y
    A B C E F G H I J L M O Q R T U W X Y Z
 
    Number keys used: <=502
@@ -95,14 +95,6 @@ static struct argp_option options[] =
       1
     },
     {
-      "align",
-      'a',
-      0,
-      0,
-      "Align the image and celestial axes.",
-      1
-    },
-    {
       "hstartwcs",
       501,
       "INT",
@@ -129,6 +121,30 @@ static struct argp_option options[] =
       2
     },
     {
+      "align",
+      'a',
+      0,
+      0,
+      "Align the image and celestial axes.",
+      1
+    },
+    {
+      "rotate",
+      'r',
+      "FLT",
+      0,
+      "Rotate by the given angle in degrees.",
+      1
+    },
+    {
+      "scale",
+      's',
+      "FLT",
+      0,
+      "Scale the image by the given factor.",
+      1
+    },
+    {
       "nowcscorrection",
       'n',
       0,
@@ -240,6 +256,14 @@ parse_opt(int key, char *arg, struct argp_state *state)
       p->up.align=1;
       p->up.alignset=1;
       break;
+    case 'r':
+      gal_checkset_any_float(arg, &p->up.rotate, "rotate", key, SPACK, NULL, 
0);
+      p->up.rotateset=1;
+      break;
+    case 's':
+      gal_checkset_any_float(arg, &p->up.scale, "scale", key, SPACK, NULL, 0);
+      p->up.scaleset=1;
+      break;
 
 
 
diff --git a/bin/imgwarp/main.h b/bin/imgwarp/main.h
index 3b04619..3c850ed 100644
--- a/bin/imgwarp/main.h
+++ b/bin/imgwarp/main.h
@@ -50,7 +50,11 @@ struct uiparams
   char     *matrixstring;  /* String containing transform elements.    */
 
   int              align;  /* ==1: Align the image.                    */
+  float           rotate;  /* Rotation degrees in degrees.             */
+  float            scale;  /* Scaling factor.                          */
   int           alignset;
+  int          rotateset;
+  int           scaleset;
 
   int    matrixstringset;
   int    maxblankfracset;
diff --git a/bin/imgwarp/ui.c b/bin/imgwarp/ui.c
index 127d565..f43d616 100644
--- a/bin/imgwarp/ui.c
+++ b/bin/imgwarp/ui.c
@@ -145,6 +145,20 @@ readconfig(char *filename, struct imgwarpparams *p)
                                        SPACK, filename, lineno);
           up->alignset=1;
         }
+      else if(strcmp(name, "rotate")==0)
+        {
+          if(up->rotateset) continue;
+          gal_checkset_any_float(value, &up->rotate, name, key, SPACK,
+                                 filename, lineno);
+          up->rotateset=1;
+        }
+      else if(strcmp(name, "scale")==0)
+        {
+          if(up->scaleset) continue;
+          gal_checkset_any_float(value, &up->scale, name, key, SPACK,
+                                 filename, lineno);
+          up->scaleset=1;
+        }
 
 
 
@@ -186,9 +200,12 @@ printvalues(FILE *fp, struct imgwarpparams *p)
   fprintf(fp, "\n# Output parameters:\n");
   if(up->matrixstringset)
     GAL_CHECKSET_PRINT_STRING_MAYBE_WITH_SPACE("matrix", up->matrixstring);
-
   if(up->alignset)
     fprintf(fp, CONF_SHOWFMT"%d\n", "align", up->align);
+  if(up->rotateset)
+    fprintf(fp, CONF_SHOWFMT"%.3f\n", "rotate", up->rotate);
+  if(up->scaleset)
+    fprintf(fp, CONF_SHOWFMT"%.3f\n", "scale", up->scale);
 
   if(cp->outputset)
     GAL_CHECKSET_PRINT_STRING_MAYBE_WITH_SPACE("output", cp->output);
@@ -220,8 +237,6 @@ checkifset(struct imgwarpparams *p)
   int intro=0;
   if(cp->hduset==0)
     GAL_CONFIGFILES_REPORT_NOTSET("hdu");
-  if(up->matrixstringset==0)
-    GAL_CONFIGFILES_REPORT_NOTSET("matrix");
   if(up->maxblankfracset==0)
     GAL_CONFIGFILES_REPORT_NOTSET("maxblankfrac");
 
@@ -256,12 +271,6 @@ readmatrixoption(struct imgwarpparams *p)
   size_t counter=0;
   char *t, *tailptr;
 
-  /* Check if a matrix string is actually present. */
-  if(p->up.matrixstring==NULL)
-    error(EXIT_FAILURE, 0, "No warping matrix string has been and no "
-          "other means of estimating the warping matrix (a file or other "
-          "options have been specified");
-
   /* Allocate the necessary space. */
   errno=0;
   p->matrix=malloc(9*sizeof *p->matrix);
@@ -344,7 +353,7 @@ makealignmatrix(struct imgwarpparams *p)
   errno=0;
   p->matrix=malloc(4*sizeof *p->matrix);
   if(p->matrix==NULL)
-    error(EXIT_FAILURE, errno, "%lu bytes for p->matrix",
+    error(EXIT_FAILURE, errno, "%lu bytes for p->matrix in makealignmatrix",
           4*sizeof *p->matrix);
   p->ms0=p->ms1=2;
 
@@ -409,18 +418,51 @@ makealignmatrix(struct imgwarpparams *p)
 
 
 
+/* Create rotation matrix */
+void
+makebasicmatrix(struct imgwarpparams *p)
+{
+  struct uiparams *up=&p->up;
+
+  /* Allocate space for the matrix: */
+  errno=0;
+  p->matrix=malloc(4*sizeof *p->matrix);
+  if(p->matrix==NULL)
+    error(EXIT_FAILURE, errno, "%lu bytes for p->matrix in makerotationmatrix",
+          4*sizeof *p->matrix);
+  p->ms0=p->ms1=2;
+
+  /* Put in the elements */
+  if(up->rotateset)
+    {
+      p->matrix[1] = sin( up->rotate*M_PI/180 );
+      p->matrix[2] = p->matrix[1] * -1.0f;
+      p->matrix[3] = p->matrix[0] = cos( up->rotate*M_PI/180 );
+    }
+  else if (up->scaleset)
+    {
+      p->matrix[1] = p->matrix[2] = 0.0f;
+      p->matrix[0] = p->matrix[3] = up->scale;
+    }
+}
+
+
+
+
+
 /* Fill in the warping matrix elements based on the options/arguments */
 void
 preparematrix(struct imgwarpparams *p)
 {
   double *tmp;
   int mcheck=0;
-
+  struct uiparams *up=&p->up;
 
   /* Make sure that none of the matrix creation options/arguments are given
      together. Note that a matrix string is optional (will only be used if
      there is no matrix file). */
-  mcheck=p->up.alignset + (p->up.matrixname!=NULL);
+  mcheck = ( up->alignset + up->rotateset + up->scaleset +
+             (up->matrixname!=NULL) );
   if( mcheck > 1 )
     error(EXIT_FAILURE, 0, "More than one method to define the warping "
           "matrix has been given. Please only specify one.");
@@ -428,20 +470,29 @@ preparematrix(struct imgwarpparams *p)
 
   /* Read the input image WCS structure. We are doing this here because
      some of the matrix operations might need it. */
-  gal_fits_read_wcs(p->up.inputname, p->cp.hdu, p->hstartwcs,
+  gal_fits_read_wcs(up->inputname, p->cp.hdu, p->hstartwcs,
                     p->hendwcs, &p->nwcs, &p->wcs);
 
 
   /* Depending on the given situation make the matrix. */
-  if(p->up.alignset)
+  if(up->alignset)
     makealignmatrix(p);
+  else if( up->rotateset || up->scaleset)
+    makebasicmatrix(p);
   else
     {
-      if(p->up.matrixname)
-        gal_txtarray_txt_to_array(p->up.matrixname, &p->matrix,
+      if(up->matrixname)
+        gal_txtarray_txt_to_array(up->matrixname, &p->matrix,
                                   &p->ms0, &p->ms1);
       else
-        readmatrixoption(p);
+        {
+          /* Check if a matrix string is actually present. */
+          if(up->matrixstringset==0)
+            error(EXIT_FAILURE, 0, "no warping matrix string has been and "
+                  "no other means of making the warping matrix (a file or "
+                  "other options have been specified");
+          readmatrixoption(p);
+        }
   }
 
 
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index c33e2b8..ccc01bf 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -9075,18 +9075,23 @@ One line examples:
 
 @example
 $ astimgwarp matrix.txt image.fits
+$ astimgwarp --align rawimage.fits
+$ astimgwarp --scale 1.82 image.fits
+$ astimgwarp --rotate=37.92 image.fits
 $ astimgwarp --matrix=0.2,0,0.4,0,0.2,0.4,0,0,1 image.fits
 $ astimgwarp --matrix="0.7071,-0.7071  0.7071,0.7071" image.fits
 @end example
 
-ImageWarp can accept two arguments, one (the input image) is mandatory
-if any processing is to be done. An optional argument is a plain text
-file that will keep the warp/transform matrix, see @ref{Warping
-basics}. There is also the @option{--matrix} option from which the
-matrix can be literally specified on the command-line. If both are
-present when calling ImageWarp, the contents of the plain text file
-have higher precedence. The general options to all Gnuastro programs
-can be seen in @ref{Common options}.
+ImageWarp can accept two arguments, one (an image) is mandatory if any
+processing is to be done. An optional argument is a plain text file, which
+must contain the warp/transform matrix, see @ref{Warping basics}. There is
+also the @option{--matrix} option from which the matrix can be literally
+specified on the command-line. If both are present when calling ImageWarp,
+the contents of the plain text file have higher precedence. You can also
+specify standard transformations without an explicit matrix as discussed
+below. If multiple transformations are specified, then ImageWarp will
+inform you and abort. The general options to all Gnuastro programs can be
+seen in @ref{Common options}.
 
 @cindex WCSLIB
 @cindex World Coordinate System
@@ -9155,13 +9160,28 @@ general homography matrix of @ref{Warping basics}, it 
should be called
 with @command{--matrix=a,b,c,d,e,f,g,h,1}.
 
 @item -a
address@hidden --align
address@hidden --align
 Align the image and celestial (WCS) axes, such that the vertical image
 direction (when viewed in SAO ds9) corresponds to the declination and the
 horizontal axis is the inverse of the Right Ascension (RA). The inverse of
 the RA is chosen so the image can correspond to what you would actually see
 on the sky and is common in most survey images.
 
address@hidden -r
address@hidden --rotate
+(@option{=FLT}) Rotate the input image by the given angle in degrees. Note
+that commonly, the WCS structure of the image is set such that the RA is
+the inverse of the image horizontal axis which increases towards the right
+in the FITS standard and as viewed by SAO ds9. So the default center for
+rotation is on the right of the image. If you want to rotate about other
+points, you have to define your own transformation matrix by first shifting
+the central point, then applying your rotation and then returning the
+center back to the original position, see @ref{Merging multiple warpings}.
+
address@hidden -s
address@hidden --scale
+(@option{=FLT}) Scale the input image by the given factor.
+
 @item --hstartwcs
 (@option{=INT}) Specify the first header keyword number (line) that
 should be used to read the WCS information, see the full explanation in



reply via email to

[Prev in Thread] Current Thread [Next in Thread]