Changeset 18844
- Timestamp:
- Aug 1, 2008, 8:32:27 AM (18 years ago)
- File:
-
- 1 edited
-
trunk/psModules/src/objects/pmFootprintFind.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psModules/src/objects/pmFootprintFind.c
r18828 r18844 4 4 * @author RHL, Princeton & IfA; EAM, IfA 5 5 * 6 * @version $Revision: 1. 2$ $Name: not supported by cvs2svn $7 * @date $Date: 2008-08-01 00:00:17 $6 * @version $Revision: 1.3 $ $Name: not supported by cvs2svn $ 7 * @date $Date: 2008-08-01 18:32:27 $ 8 8 * Copyright 2006 Institute for Astronomy, University of Hawaii 9 9 */ … … 21 21 #include "pmFootprint.h" 22 22 23 // ** this is not defined on all systems?24 void *memset (void *s, int c, size_t n);25 26 23 // XXX EAM : why use WSPAN in here rather than pmSpan? 27 typedef struct { /* run-length code for part of object*/28 int id; /* ID for object */29 int y; /* Row wherein WSPAN dwells */30 int x0, x1; /* inclusive range of columns */24 typedef struct { /* run-length code for part of object*/ 25 int id; /* ID for object */ 26 int y; /* Row wherein WSPAN dwells */ 27 int x0, x1; /* inclusive range of columns */ 31 28 } WSPAN; 32 29 … … 53 50 */ 54 51 static int 55 resolve_alias(const int *aliases, /* list of aliases */56 int id)/* alias to look up */52 resolve_alias(const int *aliases, /* list of aliases */ 53 int id) /* alias to look up */ 57 54 { 58 int resolved = id; /* resolved alias */55 int resolved = id; /* resolved alias */ 59 56 60 57 while(id != aliases[id]) { … … 71 68 */ 72 69 psArray * 73 pmFootprintsFind(const psImage *img, // image to search74 const float threshold,// Threshold75 const int npixMin)// minimum number of pixels in an acceptable pmFootprint70 pmFootprintsFind(const psImage *img, // image to search 71 const float threshold, // Threshold 72 const int npixMin) // minimum number of pixels in an acceptable pmFootprint 76 73 { 77 int i0; /* initial value of i */78 int id; /* object ID */79 int in_span; /* object ID of current WSPAN */80 int nspan = 0; /* number of spans */81 int nobj = 0; /* number of objects found */82 int x0 = 0; /* unpacked from a WSPAN */83 int *tmp; /* used in swapping idc/idp */74 int i0; /* initial value of i */ 75 int id; /* object ID */ 76 int in_span; /* object ID of current WSPAN */ 77 int nspan = 0; /* number of spans */ 78 int nobj = 0; /* number of objects found */ 79 int x0 = 0; /* unpacked from a WSPAN */ 80 int *tmp; /* used in swapping idc/idp */ 84 81 85 82 assert(img != NULL); 86 83 87 bool F32 = false; // is this an F32 image?84 bool F32 = false; // is this an F32 image? 88 85 if (img->type.type == PS_TYPE_F32) { 89 86 F32 = true; 90 87 } else if (img->type.type == PS_TYPE_S32) { 91 88 F32 = false; 92 } else { // N.b. You can't trivially add more cases here; F32 is just a bool89 } else { // N.b. You can't trivially add more cases here; F32 is just a bool 93 90 psError(PS_ERR_UNKNOWN, true, "Unsupported psImage type: %d", img->type.type); 94 91 return NULL; 95 92 } 96 psF32 *imgRowF32 = NULL; // row pointer if F3297 psS32 *imgRowS32 = NULL; // " " " " !F3298 93 psF32 *imgRowF32 = NULL; // row pointer if F32 94 psS32 *imgRowS32 = NULL; // " " " " !F32 95 99 96 const int row0 = img->row0; 100 97 const int col0 = img->col0; … … 107 104 int *id_s = psAlloc(2*(numCols + 2)*sizeof(int)); 108 105 memset(id_s, '\0', 2*(numCols + 2)*sizeof(int)); assert(id_s[0] == 0); 109 int *idc = id_s + 1; // object IDs in current/110 int *idp = idc + (numCols + 2); // previous row111 112 int size_aliases = 1 + numRows/20; // size of aliases[] array106 int *idc = id_s + 1; // object IDs in current/ 107 int *idp = idc + (numCols + 2); // previous row 108 109 int size_aliases = 1 + numRows/20; // size of aliases[] array 113 110 int *aliases = psAlloc(size_aliases*sizeof(int)); // aliases for object IDs 114 111 115 int size_spans = 1 + numRows/20; // size of spans[] array112 int size_spans = 1 + numRows/20; // size of spans[] array 116 113 WSPAN *spans = psAlloc(size_spans*sizeof(WSPAN)); // row:x0,x1 for objects 117 114 /* … … 120 117 for (int i = 0; i < numRows; i++) { 121 118 int j; 122 tmp = idc; idc = idp; idp = tmp; /* swap ID pointers */119 tmp = idc; idc = idp; idp = tmp; /* swap ID pointers */ 123 120 memset(idc, '\0', numCols*sizeof(int)); 124 125 imgRowF32 = img->data.F32[i]; // only one of126 imgRowS32 = img->data.S32[i]; // these is valid!127 128 in_span = 0; /* not in a span */121 122 imgRowF32 = img->data.F32[i]; // only one of 123 imgRowS32 = img->data.S32[i]; // these is valid! 124 125 in_span = 0; /* not in a span */ 129 126 for (j = 0; j < numCols; j++) { 130 double pixVal = F32 ? imgRowF32[j] : imgRowS32[j];131 if (pixVal < threshold) {132 if (in_span) {133 if(nspan >= size_spans) {134 size_spans *= 2;135 spans = psRealloc(spans, size_spans*sizeof(WSPAN));136 }137 spans[nspan].id = in_span;138 spans[nspan].y = i;139 spans[nspan].x0 = x0;140 spans[nspan].x1 = j - 1;141 142 nspan++;143 144 in_span = 0;145 }146 } else {/* a pixel to fix */147 if(idc[j - 1] != 0) {148 id = idc[j - 1];149 } else if(idp[j - 1] != 0) {150 id = idp[j - 1];151 } else if(idp[j] != 0) {152 id = idp[j];153 } else if(idp[j + 1] != 0) {154 id = idp[j + 1];155 } else {156 id = ++nobj;157 158 if(id >= size_aliases) {159 size_aliases *= 2;160 aliases = psRealloc(aliases, size_aliases*sizeof(int));161 }162 aliases[id] = id;163 }164 165 idc[j] = id;166 if(!in_span) {167 x0 = j; in_span = id;168 }127 double pixVal = F32 ? imgRowF32[j] : imgRowS32[j]; 128 if (pixVal < threshold) { 129 if (in_span) { 130 if(nspan >= size_spans) { 131 size_spans *= 2; 132 spans = psRealloc(spans, size_spans*sizeof(WSPAN)); 133 } 134 spans[nspan].id = in_span; 135 spans[nspan].y = i; 136 spans[nspan].x0 = x0; 137 spans[nspan].x1 = j - 1; 138 139 nspan++; 140 141 in_span = 0; 142 } 143 } else { /* a pixel to fix */ 144 if(idc[j - 1] != 0) { 145 id = idc[j - 1]; 146 } else if(idp[j - 1] != 0) { 147 id = idp[j - 1]; 148 } else if(idp[j] != 0) { 149 id = idp[j]; 150 } else if(idp[j + 1] != 0) { 151 id = idp[j + 1]; 152 } else { 153 id = ++nobj; 154 155 if(id >= size_aliases) { 156 size_aliases *= 2; 157 aliases = psRealloc(aliases, size_aliases*sizeof(int)); 158 } 159 aliases[id] = id; 160 } 161 162 idc[j] = id; 163 if(!in_span) { 164 x0 = j; in_span = id; 165 } 169 166 /* 170 167 * Do we need to merge ID numbers? If so, make suitable entries in aliases[] 171 168 */ 172 if(idp[j + 1] != 0 && idp[j + 1] != id) {173 aliases[resolve_alias(aliases, idp[j + 1])] =174 resolve_alias(aliases, id);175 176 idc[j] = id = idp[j + 1];177 }178 }169 if(idp[j + 1] != 0 && idp[j + 1] != id) { 170 aliases[resolve_alias(aliases, idp[j + 1])] = 171 resolve_alias(aliases, id); 172 173 idc[j] = id = idp[j + 1]; 174 } 175 } 179 176 } 180 177 181 178 if(in_span) { 182 if(nspan >= size_spans) {183 size_spans *= 2;184 spans = psRealloc(spans, size_spans*sizeof(WSPAN));185 }186 187 assert(nspan < size_spans);/* we checked for space above */188 spans[nspan].id = in_span;189 spans[nspan].y = i;190 spans[nspan].x0 = x0;191 spans[nspan].x1 = j - 1;192 193 nspan++;179 if(nspan >= size_spans) { 180 size_spans *= 2; 181 spans = psRealloc(spans, size_spans*sizeof(WSPAN)); 182 } 183 184 assert(nspan < size_spans); /* we checked for space above */ 185 spans[nspan].id = in_span; 186 spans[nspan].y = i; 187 spans[nspan].x0 = x0; 188 spans[nspan].x1 = j - 1; 189 190 nspan++; 194 191 } 195 192 } … … 215 212 */ 216 213 psArray *footprints = psArrayAlloc(nobj); 217 int n = 0; // number of pmFootprints214 int n = 0; // number of pmFootprints 218 215 219 216 if(nspan > 0) { 220 217 id = spans[0].id; 221 218 i0 = 0; 222 for (int i = 0; i <= nspan; i++) { /* nspan + 1 to catch last object */223 if(i == nspan || spans[i].id != id) {224 pmFootprint *fp = pmFootprintAlloc(i - i0, img);225 226 for(; i0 < i; i0++) {227 pmFootprintAddSpan(fp, spans[i0].y + row0,228 spans[i0].x0 + col0, spans[i0].x1 + col0);229 }230 231 if (fp->npix < npixMin) {232 psFree(fp);233 } else {234 footprints->data[n++] = fp;235 }236 }237 238 id = spans[i].id;219 for (int i = 0; i <= nspan; i++) { /* nspan + 1 to catch last object */ 220 if(i == nspan || spans[i].id != id) { 221 pmFootprint *fp = pmFootprintAlloc(i - i0, img); 222 223 for(; i0 < i; i0++) { 224 pmFootprintAddSpan(fp, spans[i0].y + row0, 225 spans[i0].x0 + col0, spans[i0].x1 + col0); 226 } 227 228 if (fp->npix < npixMin) { 229 psFree(fp); 230 } else { 231 footprints->data[n++] = fp; 232 } 233 } 234 235 id = spans[i].id; 239 236 } 240 237 }
Note:
See TracChangeset
for help on using the changeset viewer.
