IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 6, 2005, 6:43:52 PM (21 years ago)
Author:
Paul Price
Message:

Importing current working PAP version (many bug fixes since this branch) straight on top of this version, and tidying up a bit

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pois/src/poisConvolveImage.c

    r3801 r5717  
    77/* Convolve an image by the kernel */
    88psImage *poisConvolveImage(const psImage *input, // Input image, to be convolved
    9                            const psImage *mask, // Mask image
    10                            const psVector *solution, // The solution vector, containing the coefficients
    11                            const psArray *kernelParams, // The kernel parameters
    12                            const poisConfig *config // The configuration
     9                           const psImage *mask, // Mask image
     10                           const psVector *solution, // The solution vector, containing the coefficients
     11                           const psArray *kernelParams, // The kernel parameters
     12                           const poisConfig *config // The configuration
    1313    )
    1414{
    15     assert(solution->n == kernelParams->n + 1); // Extra parameter for background
     15    assert(solution->n == kernelParams->n + 1); // Extra parameter for background
    1616    assert(solution->type.type == PS_TYPE_F64);
    1717    assert(input->numCols == mask->numCols && input->numRows == mask->numRows);
    1818    assert(mask->type.type == PS_TYPE_U8);
    1919    assert(input->type.type == PS_TYPE_F32);
    20    
    21     int nx = input->numCols;            // The size of the image in x
    22     int ny = input->numRows;            // The size of the image in y
    23     int xKernel = config->xKernel;      // The half-size of the kernel in x
    24     int yKernel = config->yKernel;      // The half-size of the kernel in y
    25     float background = solution->data.F64[solution->n - 1]; // The difference in background
    26     int numParams = kernelParams->n;    // Number of kernel parameters
     20
     21    int nx = input->numCols;            // The size of the image in x
     22    int ny = input->numRows;            // The size of the image in y
     23    float nxHalf = 0.5 * (float)nx;     // Half the size of the image
     24    float nyHalf = 0.5 * (float)ny;     // Half the size of the image
     25    int xKernel = config->xKernel;      // The half-size of the kernel in x
     26    int yKernel = config->yKernel;      // The half-size of the kernel in y
     27    float background = solution->data.F64[solution->n-1]; // The difference in background
     28    int numParams = kernelParams->n;    // Number of kernel parameters
    2729
    2830    psImage *convolved = psImageAlloc(nx, ny, PS_TYPE_F32); // The convolved image, to be returned
    2931
    30     psDPolynomial2D *poly = psDPolynomial2DAlloc(config->spatialOrder + 1, config->spatialOrder + 1,
    31                                                  PS_POLYNOMIAL_ORD); // Polynomial
     32    psPolynomial2D *poly = psPolynomial2DAlloc(config->spatialOrder + 1, config->spatialOrder + 1,
     33                                               PS_POLYNOMIAL_ORD ); // Polynomial
    3234    for (int i = 0; i < config->spatialOrder + 1; i++) {
    33         for (int j = 0; j < config->spatialOrder + 1; j++) {
    34             poly->coeff[i][j] = 1.0;
    35             poly->mask[i][j] = 1;       // Mask all coefficients; unmask to evaluate
    36         }
     35        for (int j = 0; j < config->spatialOrder + 1; j++) {
     36            poly->coeff[i][j] = 1.0;
     37            poly->mask[i][j] = 1;       // Mask all coefficients; unmask to evaluate
     38        }
    3739    }
    3840
    3941    // Convolve only the middle part
    4042    for (int y = yKernel; y < ny - yKernel; y++) {
    41         for (int x = xKernel; x < nx - xKernel; x++) {
    42             if (! mask->data.U8[y][x] & (POIS_MASK_BAD | POIS_MASK_NEAR_BAD)) {
    43                 double conv = background; // Convolved value
     43        for (int x = xKernel; x < nx - xKernel; x++) {
     44            if (! mask->data.U8[y][x] & (POIS_MASK_BAD | POIS_MASK_NEAR_BAD)) {
     45                double conv = background; // Convolved value
    4446
    45 #if 0
    46                 float imageX = (x - nxHalf) / nxHalf; // Normalised position in x
    47                 float imageY = (y - nyHalf) / nyHalf; // Normalised position in y
    48 #endif
    49                
    50                 // Iterate over the kernel basis functions
    51                 for (int k = 0; k < numParams; k++) {
    52                     poisKernelBasis *kernel = kernelParams->data[k]; // The kernel basis function
    53                     int u = kernel->u;
    54                     int v = kernel->v;
    55                    
    56 #if 0
    57                     int xOrder = kernel->xOrder;
    58                     int yOrder = kernel->yOrder;
    59                     // Evaluate the polynomial
    60                     poly->mask[xOrder][yOrder] = 0;
    61                     double polyVal = psDPolynomial2DEval(poly, imageX, imageY);
    62                     poly->mask[xOrder][yOrder] = 1;
    63 #else
    64                     double polyVal = 1.0;
    65 #endif
     47                float imageX = (x - nxHalf) / nxHalf; // Normalised position in x
     48                float imageY = (y - nyHalf) / nyHalf; // Normalised position in y
    6649
    67                     if (k == 0) {
    68                         conv += solution->data.F64[k] * input->data.F32[y - v][x - u] * polyVal;
    69                     } else {
    70                         conv += solution->data.F64[k] *
    71                             (input->data.F32[y - v][x - u] * polyVal - input->data.F32[y][x]);
    72                     }
    73                 }
    74                 convolved->data.F32[y][x] = (float)conv;
    75             }
    76         }
    77            
    78         /* Pad the rest with zeros */
    79         for (int x = 0; x < xKernel; x++) {
    80             convolved->data.F32[y][x] = 0.0;
    81         }
    82         for (int x = nx - xKernel; x < nx; x++) {
    83             convolved->data.F32[y][x] = 0.0;
    84         }
     50                // Iterate over the kernel basis functions
     51                for (int k = 0; k < numParams; k++) {
     52                    poisKernelBasis *kernel = kernelParams->data[k]; // The kernel basis function
     53                    int u = kernel->u;
     54                    int v = kernel->v;
     55                    int xOrder = kernel->xOrder;
     56                    int yOrder = kernel->yOrder;
     57
     58                    double polyVal = 1.0;
     59                    if (xOrder != 0 || yOrder != 0) {
     60                        // Evaluate the polynomial
     61                        poly->mask[xOrder][yOrder] = 0;
     62                        polyVal = psPolynomial2DEval(poly, imageX, imageY);
     63                        poly->mask[xOrder][yOrder] = 1;
     64                    }
     65
     66                    if (k == 0) {
     67                        conv += solution->data.F64[k] * input->data.F32[y - v][x - u] * polyVal;
     68                    } else {
     69                        conv += solution->data.F64[k] *
     70                            (input->data.F32[y - v][x - u] * polyVal - input->data.F32[y][x]);
     71                    }
     72                }
     73                convolved->data.F32[y][x] = (float)conv;
     74            }
     75        }
     76
     77        /* Pad the rest with zeros */
     78        for (int x = 0; x < xKernel; x++) {
     79            convolved->data.F32[y][x] = 0.0;
     80        }
     81        for (int x = nx - xKernel; x < nx; x++) {
     82            convolved->data.F32[y][x] = 0.0;
     83        }
    8584    }
    8685
    8786    for (int y = 0; y < yKernel; y++) {
    88         for (int x = 0; x < nx; x++) {
    89             convolved->data.F32[y][x] = 0.0;
    90         }
     87        for (int x = 0; x < nx; x++) {
     88            convolved->data.F32[y][x] = 0.0;
     89        }
    9190    }
    9291    for (int y = ny - yKernel; y < ny; y++) {
    93         for (int x = 0; x < nx; x++) {
    94             convolved->data.F32[y][x] = 0.0;
    95         }
     92        for (int x = 0; x < nx; x++) {
     93            convolved->data.F32[y][x] = 0.0;
     94        }
    9695    }
    9796
Note: See TracChangeset for help on using the changeset viewer.