From cf04965d21bf3d67eec29f1aa6ecea57eb6c595a Mon Sep 17 00:00:00 2001 From: Gregor Thalhammer Date: Sun, 13 May 2012 22:00:51 +0200 Subject: [PATCH] unwrap: removed use of global variables --- ...wrapper_with_mask_and_wrap_around_option.c | 42 ++++++++----------- unwrap2D/unwrap2D.pyx | 30 +++++++------ 2 files changed, 32 insertions(+), 40 deletions(-) diff --git a/unwrap2D/Miguel_2D_unwrapper_with_mask_and_wrap_around_option.c b/unwrap2D/Miguel_2D_unwrapper_with_mask_and_wrap_around_option.c index 14fe65de..131ec517 100644 --- a/unwrap2D/Miguel_2D_unwrapper_with_mask_and_wrap_around_option.c +++ b/unwrap2D/Miguel_2D_unwrapper_with_mask_and_wrap_around_option.c @@ -13,7 +13,6 @@ //When the mask is 0 this means that the pixel is invalid (noisy or corrupted pixel) //This program takes into consideration the image wrap around problem encountered in MRI imaging. -//TODO stdlib instead of malloc.h ? (calloc?) #include #include #include @@ -35,11 +34,6 @@ typedef struct int no_of_edges; } params_t; - -//int x_connectivity = 1; -//int y_connectivity = 1; -int No_of_edges = 0; - //PIXELM information struct pixelm { @@ -71,7 +65,6 @@ struct edge typedef struct edge EDGE; - //---------------start quicker_sort algorithm -------------------------------- #define swap(x,y) {EDGE t; t=x; x=y; y=t;} #define order(x,y) if (x.reliab > y.reliab) swap(x,y) @@ -424,6 +417,7 @@ void horizontalEDGEs(PIXELM *pixel, EDGE *edge, int i, j; EDGE *edge_pointer = edge; PIXELM *pixel_pointer = pixel; + int no_of_edges = params->no_of_edges; for (i = 0; i < image_height; i++) { @@ -436,7 +430,7 @@ void horizontalEDGEs(PIXELM *pixel, EDGE *edge, edge_pointer->reliab = pixel_pointer->reliability + (pixel_pointer + 1)->reliability; edge_pointer->increment = find_wrap(pixel_pointer->value, (pixel_pointer + 1)->value); edge_pointer++; - No_of_edges++; + no_of_edges++; } pixel_pointer++; } @@ -455,11 +449,12 @@ void horizontalEDGEs(PIXELM *pixel, EDGE *edge, edge_pointer->reliab = pixel_pointer->reliability + (pixel_pointer - image_width + 1)->reliability; edge_pointer->increment = find_wrap(pixel_pointer->value, (pixel_pointer - image_width + 1)->value); edge_pointer++; - No_of_edges++; + no_of_edges++; } pixel_pointer+=image_width; } } + params->no_of_edges = no_of_edges; } //calculate the reliability of the vertical edges of the image @@ -470,8 +465,9 @@ void verticalEDGEs(PIXELM *pixel, EDGE *edge, params_t *params) { int i, j; + int no_of_edges = params->no_of_edges; PIXELM *pixel_pointer = pixel; - EDGE *edge_pointer = edge + No_of_edges; + EDGE *edge_pointer = edge + no_of_edges; for (i=0; i < image_height - 1; i++) { @@ -484,7 +480,7 @@ void verticalEDGEs(PIXELM *pixel, EDGE *edge, edge_pointer->reliab = pixel_pointer->reliability + (pixel_pointer + image_width)->reliability; edge_pointer->increment = find_wrap(pixel_pointer->value, (pixel_pointer + image_width)->value); edge_pointer++; - No_of_edges++; + no_of_edges++; } pixel_pointer++; } //j loop @@ -503,15 +499,16 @@ void verticalEDGEs(PIXELM *pixel, EDGE *edge, edge_pointer->reliab = pixel_pointer->reliability + (pixel_pointer - image_width *(image_height - 1))->reliability; edge_pointer->increment = find_wrap(pixel_pointer->value, (pixel_pointer - image_width *(image_height - 1))->value); edge_pointer++; - No_of_edges++; + no_of_edges++; } pixel_pointer++; } } + params->no_of_edges = no_of_edges; } //gather the pixels of the image into groups -void gatherPIXELs(EDGE *edge, int image_width, int image_height) +void gatherPIXELs(EDGE *edge, params_t *params) { int k; PIXELM *PIXEL1; @@ -521,7 +518,7 @@ void gatherPIXELs(EDGE *edge, int image_width, int image_height) EDGE *pointer_edge = edge; int incremento; - for (k = 0; k < No_of_edges; k++) + for (k = 0; k < params->no_of_edges; k++) { PIXEL1 = pointer_edge->pointer_1; PIXEL2 = pointer_edge->pointer_2; @@ -683,14 +680,14 @@ void returnImage(PIXELM *pixel, float *unwrapped_image, int image_width, int im } //the main function of the unwrapper -int unwrap(float* wrapped_image, float* UnwrappedImage, unsigned char* input_mask, - int image_width, int image_height, - int wrap_around_x, int wrap_around_y) +unwrap2D(float* wrapped_image, float* UnwrappedImage, unsigned char* input_mask, + int image_width, int image_height, + int wrap_around_x, int wrap_around_y) { + params_t params = {TWOPI, 0, 0, 0}; unsigned char *extended_mask; int image_size = image_height * image_width; int No_of_Edges_initially = 2 * image_width * image_height; - params_t params = {TWOPI, 0, 0, 0}; extended_mask = (unsigned char *) calloc(image_size, sizeof(unsigned char)); PIXELM *pixel = (PIXELM *) calloc(image_size, sizeof(PIXELM)); @@ -704,23 +701,20 @@ int unwrap(float* wrapped_image, float* UnwrappedImage, unsigned char* input_mas //sort the EDGEs depending on their reiability. The PIXELs with higher //relibility (small value) first - quicker_sort(edge, edge + No_of_edges - 1); + quicker_sort(edge, edge + params.no_of_edges - 1); //gather PIXELs into groups - gatherPIXELs(edge, image_width, image_height); + gatherPIXELs(edge, ¶ms); unwrapImage(pixel, image_width, image_height); maskImage(pixel, input_mask, image_width, image_height); //copy the image from PIXELM structure to the unwrapped phase array //passed to this function + //TODO: replace by (cython?) function to directly write into numpy array ? returnImage(pixel, UnwrappedImage, image_width, image_height); free(edge); free(pixel); free(extended_mask); - - No_of_edges = 0; - return 1; - } diff --git a/unwrap2D/unwrap2D.pyx b/unwrap2D/unwrap2D.pyx index 4d2a7be0..2dd0b33c 100644 --- a/unwrap2D/unwrap2D.pyx +++ b/unwrap2D/unwrap2D.pyx @@ -1,22 +1,20 @@ -#import numpy as np -#cimport numpy as np -cdef extern int unwrap(float* wrapped_image, - float* unwrapped_image, - unsigned char* input_mask, - int image_width, int image_height, - int wrap_around_x, int wrap_around_y) +cdef extern unwrap2D(float* wrapped_image, + float* unwrapped_image, + unsigned char* input_mask, + int image_width, int image_height, + int wrap_around_x, int wrap_around_y) def _unwrap2D(float[:,::1] array, - unsigned char[:,::1] mask, - float[:,::1] unwrapped_array, - wrap_around_x, wrap_around_y): + unsigned char[:,::1] mask, + float[:,::1] unwrapped_array, + wrap_around_x, wrap_around_y): cdef int h = array.shape[0] cdef int w = array.shape[1] - unwrap(&array[0,0], - &unwrapped_array[0,0], - &mask[0,0], - array.shape[0], array.shape[1], - wrap_around_x, wrap_around_y, - ) + unwrap2D(&array[0,0], + &unwrapped_array[0,0], + &mask[0,0], + array.shape[0], array.shape[1], + wrap_around_x, wrap_around_y, + )