Changeset 16479 for trunk/psModules/src/imcombine/pmStackReject.c
- Timestamp:
- Feb 14, 2008, 1:33:09 PM (18 years ago)
- File:
-
- 1 edited
-
trunk/psModules/src/imcombine/pmStackReject.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psModules/src/imcombine/pmStackReject.c
r16476 r16479 11 11 #define PIXEL_LIST_BUFFER 100 // Number of pixels to add to list at a time 12 12 13 psPixels *pmStackReject(const psPixels *in, const psRegion *valid, float threshold,14 const psArray *s ubRegions, const psArray*kernels)13 psPixels *pmStackReject(const psPixels *in, float threshold, const psArray *regions, 14 const psArray *solutions, const pmSubtractionKernels *kernels) 15 15 { 16 16 PS_ASSERT_PIXELS_NON_NULL(in, NULL); 17 17 PS_ASSERT_FLOAT_LARGER_THAN_OR_EQUAL(threshold, 0.0, NULL); 18 18 PS_ASSERT_FLOAT_LESS_THAN_OR_EQUAL(threshold, 1.0, NULL); 19 PS_ASSERT_ARRAY_NON_NULL(subRegions, NULL); 20 PS_ASSERT_ARRAY_NON_NULL(kernels, NULL); 21 PS_ASSERT_ARRAYS_SIZE_EQUAL(subRegions, kernels, NULL); 19 PS_ASSERT_ARRAY_NON_NULL(regions, NULL); 20 PS_ASSERT_ARRAY_NON_NULL(solutions, NULL); 21 PS_ASSERT_ARRAYS_SIZE_EQUAL(regions, solutions, NULL); 22 PS_ASSERT_PTR_NON_NULL(kernels, NULL); 22 23 23 24 // Get the original image size 24 int numRegions = subRegions->n; // Number of regions25 int numRegions = regions->n; // Number of regions 25 26 int numCols = 0, numRows = 0; // Size of original image 26 27 int minCols = INT_MAX, minRows = INT_MAX; // Minimum coordinate for image --- should be 0,0 27 int size = 0; // Size of kernel28 28 for (int i = 0; i < numRegions; i++) { 29 psRegion * subRegion = subRegions->data[i]; // Region of interest30 if ( subRegion->x0 < minCols) {31 minCols = subRegion->x0;29 psRegion *region = regions->data[i]; // Region of interest 30 if (region->x0 < minCols) { 31 minCols = region->x0; 32 32 } 33 if ( subRegion->y0 < minRows) {34 minRows = subRegion->y0;33 if (region->y0 < minRows) { 34 minRows = region->y0; 35 35 } 36 if ( subRegion->x1 > numCols) {37 numCols = subRegion->x1;36 if (region->x1 > numCols) { 37 numCols = region->x1; 38 38 } 39 if (subRegion->y1 > numRows) { 40 numRows = subRegion->y1; 41 } 42 43 pmSubtractionKernels *kernel = kernels->data[i]; // Kernel of interest 44 if (size == 0) { 45 size = kernel->size; 46 } else if (kernel->size != size) { 47 psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Kernel sizes are not identical: %d vs %d", 48 size, kernel->size); 49 return NULL; 39 if (region->y1 > numRows) { 40 numRows = region->y1; 50 41 } 51 42 } 52 53 // Adjust the size for the size of the subimage 54 if (valid) { 55 minCols = PS_MAX(valid->x0, minCols); 56 minRows = PS_MAX(valid->y0, minRows); 57 numCols = PS_MIN(valid->x1, numCols); 58 numRows = PS_MIN(valid->y1, numRows); 43 if (minCols != 0 || minRows != 0) { 44 psError(PS_ERR_BAD_PARAMETER_VALUE, true, 45 "Some error with image regions --- minimum coordinate is not 0,0"); 46 return NULL; 59 47 } 60 48 61 psImage *mask = psPixelsToMask(NULL, in, psRegionSet(minCols, numCols - 1, minRows, numRows - 1), 62 0x01); // Mask 49 psImage *mask = psPixelsToMask(NULL, in, psRegionSet(0, numCols, 0, numRows), 0x01); // Mask image 63 50 psImage *image = psImageCopy(NULL, mask, PS_TYPE_F32); // Floating-point version, so we can convolve 64 51 psFree(mask); … … 68 55 pmReadout *inRO = pmReadoutAlloc(NULL); // Readout with input image 69 56 inRO->image = image; 70 inRO->col0 = minCols;71 inRO->row0 = minRows;72 57 for (int i = 0; i < numRegions; i++) { 73 psRegion *region = subRegions->data[i]; // Region of interest 74 if (valid && (region->x0 > valid->x1 || region->x1 < valid->x0 || 75 region->y0 > valid->y1 || region->y1 < valid->y0)) { 76 // Region is outside of our sub-image 77 continue; 78 } 79 pmSubtractionKernels *kernel = kernels->data[i]; // Kernel of interest 80 if (!pmSubtractionConvolve(convRO, NULL, inRO, NULL, NULL, 0, region, kernel, true)) { 58 psRegion *region = regions->data[i]; // Region of interest 59 if (!pmSubtractionConvolve(convRO, NULL, inRO, NULL, NULL, 0, region, kernels, true)) { 81 60 psError(PS_ERR_UNKNOWN, false, "Unable to convolve mask image in region %d.", i); 82 61 psFree(convRO); … … 89 68 90 69 // Image of the kernel at the centre of the region 91 float xNorm = (region->x0 + 0.5 * (region->x1 - region->x0) - kernel->numCols/2.0) / 92 (float)kernel->numCols; 93 float yNorm = (region->y0 + 0.5 * (region->y1 - region->y0) - kernel->numRows/2.0) / 94 (float)kernel->numRows; 95 psImage *image = pmSubtractionKernelImage(kernel, xNorm, yNorm, false); 96 if (!image) { 70 float xNorm = (region->x0 + 0.5 * (region->x1 - region->x0) - numCols/2.0) / (float)numCols; 71 float yNorm = (region->y0 + 0.5 * (region->y1 - region->y0) - numRows/2.0) / (float)numRows; 72 psImage *kernel = pmSubtractionKernelImage(kernels, xNorm, yNorm, false); 73 if (!kernel) { 97 74 psError(PS_ERR_UNKNOWN, false, "Unable to generate kernel image."); 98 75 psFree(convRO); … … 101 78 } 102 79 float sum = 0.0; 103 for (int y = 0; y < image->numRows; y++) {104 for (int x = 0; x < image->numCols; x++) {105 sum += image->data.F32[y][x];80 for (int y = 0; y < kernel->numRows; y++) { 81 for (int x = 0; x < kernel->numCols; x++) { 82 sum += kernel->data.F32[y][x]; 106 83 } 107 84 } 108 psFree( image);85 psFree(kernel); 109 86 110 // Range for normalisation 111 int yMin = PS_MAX(minRows, region->y0) - inRO->row0; 112 int yMax = PS_MIN(numRows - 1, region->y1) - inRO->row0; 113 int xMin = PS_MAX(minCols, region->x0) - inRO->col0; 114 int xMax = PS_MIN(numCols - 1, region->x1) - inRO->col0; 115 psTrace("psModules.imcombine", 2, "Normalising convolved mask image by %f over %d:%d,%d:%d\n", 116 sum, xMin, xMax, yMin, yMax); 117 for (int y = yMin; y <= yMax; y++) { 118 for (int x = xMin; x <= xMax; x++) { 119 convRO->image->data.F32[y][x] /= sum; 120 } 121 } 87 psImage *subConv = psImageSubset(convRO->image, *region); // Sub-image of convolved image 88 psBinaryOp(subConv, subConv, "*", psScalarAlloc(1.0 / sum, PS_TYPE_F32)); 122 89 } 123 90 psFree(inRO); … … 127 94 // Threshold the convolved image 128 95 psPixels *bad = psPixelsAllocEmpty(PIXEL_LIST_BUFFER); // List of pixels that should be masked 129 for (int y = 0; y < convolved->numRows; y++) {130 for (int x = 0; x < convolved->numCols; x++) {96 for (int y = 0; y < numRows; y++) { 97 for (int x = 0; x < numCols; x++) { 131 98 if (convolved->data.F32[y][x] > threshold) { 132 99 bad = psPixelsAdd(bad, PIXEL_LIST_BUFFER, x, y); … … 136 103 psFree(convolved); 137 104 138 // Now, grow the mask to include everything that touches a bad pixel in the convolution139 mask = psPixelsToMask(NULL, bad, psRegionSet(0, numCols - 1, 0, numRows - 1), 0xff);140 assert(mask->numCols == numCols && mask->numRows == numRows);105 // Now, we want to convolve the original pixels properly 106 mask = psPixelsToMask(NULL, bad, psRegionSet(0, numCols, 0, numRows), 0xff); 107 int size = kernels->size; // Size of kernels 141 108 for (int i = 0; i < bad->n; i++) { 142 109 int xPix = bad->data[i].x, yPix = bad->data[i].y; // Coordinates of interest … … 148 115 for (int y = yMin; y <= yMax; y++) { 149 116 for (int x = xMin; x <= xMax; x++) { 150 assert(x < mask->numCols && y < mask->numRows);151 117 mask->data.PS_TYPE_MASK_DATA[y][x] = 0xff; 152 118 }
Note:
See TracChangeset
for help on using the changeset viewer.
