long find_n_superpix(int superpix_resolution, gsl_vector_ulong *pixnum_array, gsl_vector_int *resolution_array, unsigned long n_mask) { gsl_vector_ulong *superpix_array; unsigned long superpixnum; unsigned long n_superpix; unsigned long i,j,k; /* Necessary for constructing the superpixel structure, this routine determines the total number of superpixels which contain the array of masks. Each of these superpixels will later be used to group the masks contained therein into a single resolution structure. The superpixel structure contains pointers to all of these sub-structures. */ n_superpix = 0; superpix_array = gsl_vector_ulong_alloc(n_mask); for (i=0;i<n_mask;i++) superpix(resolution_array->data[i],pixnum_array->data[i], superpix_resolution,&superpix_array->data[i]); gsl_sort_vector_ulong(superpix_array); n_superpix = 1; for (i=1;i<n_mask;i++) { if (superpix_array->data[i] != superpix_array->data[i-1]) { n_superpix++; } } return n_superpix; }
void make_superpix_struct(int superpix_resolution, gsl_vector_ulong *pixnum_array, gsl_vector_int *resolution_array, unsigned long n_pixel, superpixnum_struct *superpix_struct, unsigned long n_superpix) { unsigned long i,j, k; gsl_vector_ulong *superpix_start_array, *superpix_finish_array; gsl_vector_ulong *superpix_region_array, *tmp_pixnum_array, *superpix_array; gsl_vector_ulong *tmp_superpix_array; gsl_vector_int *tmp_resolution_array; gsl_permutation *pixel_index; /* Similar to the resolution structure, the superpix structure groups a list of masks by common superpixel index. These masks are then stored in a resolution structure, futher sorting them by resolution. The combination of these two structures allows one to quickly exclude the vast majority of the masks for any sort of filtering algorithm and check only those masks most likely to contain a given point. This routine serves as a wrapper for the various steps required to fill in the superpixel structure, requiring only that the user call find_n_superpix first (in order to allocate the superpixel structure), and then call this routine. */ superpix_region_array = gsl_vector_ulong_alloc(n_superpix); superpix_start_array = gsl_vector_ulong_alloc(n_superpix); superpix_finish_array = gsl_vector_ulong_alloc(n_superpix); superpix_array = gsl_vector_ulong_alloc(n_pixel); tmp_superpix_array = gsl_vector_ulong_alloc(n_pixel); tmp_pixnum_array = gsl_vector_ulong_alloc(n_pixel); tmp_resolution_array = gsl_vector_int_alloc(n_pixel); pixel_index = gsl_permutation_alloc(n_pixel); for (i=0;i<n_pixel;i++) { superpix(resolution_array->data[i],pixnum_array->data[i], superpix_resolution,&superpix_array->data[i]); /* printf("%d\n",superpix_array->data[i]); */ } gsl_sort_vector_ulong_index(pixel_index,superpix_array); for (i=0;i<n_pixel;i++) { k = pixel_index->data[i]; tmp_resolution_array->data[i] = resolution_array->data[k]; tmp_pixnum_array->data[i] = pixnum_array->data[k]; tmp_superpix_array->data[i] = superpix_array->data[k]; } for (i=0;i<n_pixel;i++) { resolution_array->data[i] = tmp_resolution_array->data[i]; pixnum_array->data[i] = tmp_pixnum_array->data[i]; superpix_array->data[i] = tmp_superpix_array->data[i]; } gsl_vector_ulong_free(tmp_superpix_array); gsl_vector_ulong_free(tmp_pixnum_array); gsl_vector_int_free(tmp_resolution_array); gsl_permutation_free(pixel_index); /* printf("Finding superpix bounds...\n"); */ find_superpix_bounds(superpix_array,n_pixel,superpix_region_array, superpix_start_array,superpix_finish_array); for (k=0;k<n_superpix;k++) { /* printf("%d %d %d\n",superpix_start_array->data[k], superpix_finish_array->data[k],superpix_region_array->data[k]); */ superpix_struct[k].n_pixel = superpix_finish_array->data[k] - superpix_start_array->data[k] + 1; superpix_struct[k].resolution = superpix_resolution; superpix_struct[k].superpixnum = superpix_region_array->data[k]; tmp_pixnum_array = gsl_vector_ulong_alloc(superpix_struct[k].n_pixel); tmp_resolution_array = gsl_vector_int_alloc(superpix_struct[k].n_pixel); pixel_index = gsl_permutation_alloc(superpix_struct[k].n_pixel); j = 0; for (i=superpix_start_array->data[k]; i<=superpix_finish_array->data[k];i++) { tmp_resolution_array->data[j] = resolution_array->data[i]; j++; } gsl_sort_vector_int_index(pixel_index,tmp_resolution_array); for (i=0;i<superpix_struct[k].n_pixel;i++) { j = pixel_index->data[i] + superpix_start_array->data[k]; tmp_pixnum_array->data[i] = pixnum_array->data[j]; tmp_resolution_array->data[i] = resolution_array->data[j]; /* printf("%i %u\n",tmp_resolution_array->data[i], tmp_pixnum_array->data[i]); */ } superpix_struct[k].n_res = find_n_res(tmp_resolution_array, superpix_struct[k].n_pixel); /* printf("Found %i resolutions in superpixel %u\n", superpix_struct[k].n_res,k); */ if (!(superpix_struct[k].res_struct= malloc(superpix_struct[k].n_res*sizeof(resolution_struct)))) { printf("Couldn't allocate memory...\n"); exit(1); } make_resolution_struct(tmp_pixnum_array, tmp_resolution_array, superpix_struct[k].n_pixel, superpix_struct[k].res_struct, superpix_struct[k].n_res); gsl_vector_ulong_free(tmp_pixnum_array); gsl_vector_int_free(tmp_resolution_array); gsl_permutation_free(pixel_index); } gsl_vector_ulong_free(superpix_region_array); gsl_vector_ulong_free(superpix_start_array); gsl_vector_ulong_free(superpix_finish_array); gsl_vector_ulong_free(superpix_array); }
int get_parent_pixels(int pix_c, int pix_p[], char scheme){ int m,n,res,base_pix,i,j; unsigned long pixp; //double res_d; if(pix_c<0){ fprintf(stderr, "error in get_parent_pixels: %d is not a valid pixel number\n",pix_c); return(1); } res=get_res(pix_c, scheme); if(res==-1) return (1); if(scheme=='s'){ // this scheme divides up the sphere by rectangles in az and el, and is numbered // such that the resolution is encoded in each pixel number. The whole sky is pixel 0, // pixels 1, 2, 3, and 4 are each 1/4 of the sky (resolution 1), pixels 5-20 are each // 1/16 of the sky (resolution 2), etc. base_pix=pix_c-pixel_start(res,scheme); m=base_pix % (int)(pow(2,res)); n=(base_pix-m)/pow(2,res); for(i=res;i>=0;i--){ //put pixel number into array pix_p[i]=pixel_start(i,scheme)+(int)(pow(2,i))*n+m; //make child pixel into next parent pixel n=n/2; m=m/2; } return(0); } else if(scheme=='d'){ assign_parameters(); //printf("res = %d\n", res); //printf("res1 (1) = %d\n", res1); //if (res >= 1) { // res_d = (double)(log((double)res)/log(2.0))+1; // res1 = (int)(res_d + 0.1); //} //printf("res1 (2) = %d\n", res1); pix_p[res]=pix_c; for(i=res;i>2;i--){ //printf("args to superpix: %d, %d, %d\n", (int)pow(2,i-1), pix_p[i]-pixel_start(i, scheme), (int)pow(2,i-2)); superpix((int)pow(2,i-2), (unsigned long)pix_p[i]-(unsigned long)pixel_start(i, scheme), (int)pow(2,i-3), &pixp); //printf("pixp = %d\n", (int)pixp); pix_p[i-1] = (int)pixp + pixel_start(i-1, scheme); } for(j=0;j<=5;j++){ for(i=118+j*72;i<=152+j*72;i+=2){ if(pix_p[2]==i || pix_p[2]==i+1 || pix_p[2]==i+36 || pix_p[2]==i+37) pix_p[1]=(i-118-j*36)/2+1; } } for(i=550;i<=585;i++){ if(pix_p[2]==i) { pix_p[1]=(i-114)/4; } } pix_p[0]=0; return(0); } else{ fprintf(stderr, "error in get_parent_pixels: pixel scheme %c not recognized.\n", scheme); return(1); } }