#include "header.h" /* Written by Waqas Akram, Justin Romberg, & Charles Gamiz, May 1997 */ /* This file contains the essential code for the */ /* region growing technique implemented */ /* The criterion checking function */ /* It basically checks to see if the input pixel */ /* is part of our region, based on the criterion */ /* defined by HIST_MIN & HIST_MAX in the header file */ /* check_criterion() checks the new pixel values and sees if it is to be * added to the region */ int check_criterion(struct region * reg, struct pixel * point) { /* assumes IMAGE is globally declared */ int v = IMAGE[point->m][point->n]; double g = GRADIENT[point->m][point->n]; /*if (v >= intThresh) return 1; else return 0; */ if ((g < 0.95)&&(v >= intThresh)) return 1; else if ((g >= 0.95)&&(v >= (reg->mean - reg->std))) return 1; else return 0; } struct pixel * merge_check(struct region * temp_region, int y, int x, struct pixel * tail) { struct pixel * current; struct pixel * temp_pixel; /*printf("Called merge_check...");*/ /* make sure pixel is not out-of- bounds */ if ((y < 0)||(y >= sizeI[0])||(x < 0)||(x >= sizeI[1])) { /*printf("...returning.\n");*/ return tail; } /* make sure pixel has not already been visited */ if (temp_region->visit[y][x] == 1) { shitcount++; /*printf("...returning.\n");*/ return tail; } /* printf("Checking %d, %d \n", y, x);*/ current = create_pixel(y, x); /* mark pixel visited */ temp_region->visit[y][x] = 1; /* check if pixel fits criterion */ if (check_criterion(temp_region, current) == 1) { /* donot de-allocate this! (add pixel uses it) */ temp_pixel = create_pixel(y, x); /* add pixel to struct region */ AddPixel(temp_region, temp_pixel); /*add pixel neighbors to check_list, if not visited*/ /* top, center */ if ((y+1 < sizeI[0])&& (temp_region->visit[y+1][x] == 0)) { tail->next = create_pixel(y+1, x); tail = tail->next; } /* top, right */ if (((y+1 < sizeI[0])&&(x+1 < sizeI[1]))&& (temp_region->visit[y+1][x+1] == 0)) { tail->next = create_pixel(y+1, x+1); tail = tail->next; } /* right */ if ((x+1 < sizeI[1])&& (temp_region->visit[y][x+1] == 0)) { tail->next = create_pixel(y, x+1); tail = tail->next; } /* bottom, right */ if (((y-1 >= 0)&&(x+1 < sizeI[1]))&& (temp_region->visit[y-1][x+1] == 0)) { tail->next = create_pixel(y-1, x+1); tail = tail->next; } /* bottom, center */ if ((y-1 >= 0)&& (temp_region->visit[y-1][x] == 0)) { tail->next = create_pixel(y-1, x); tail = tail->next; } /* bottom, left */ if (((y-1 >= 0)&&(x-1 >= 0))&& (temp_region->visit[y-1][x-1] == 0)) { tail->next = create_pixel(y-1, x-1); tail = tail->next; } /* left */ if ((x-1 >= 0)&& (temp_region->visit[y][x-1] == 0)) { tail->next = create_pixel(y, x-1); tail = tail->next; } /* top, left */ if (((y+1 < sizeI[0])&&(x-1 >= 0))&& (temp_region->visit[y+1][x-1] == 0)) { tail->next = create_pixel(y+1, x-1); tail = tail->next; } } /*printf("...returning.\n");*/ return tail; } /* debug */ void DumpList(struct pixel * head) { struct pixel *temp; temp = head; while (temp != NULL) { printf("%d, %d\n", temp->m, temp->n); temp = temp->next; } } /* The main region growing function */ struct region * grow_region(struct pixel * point) { struct region * temp_region; struct pixel * temp_pixel; int x,y, cnt; /*int region_complete;*/ struct pixel * head; struct pixel * check_list; struct pixel * tail; printf("%d %d\n",point->m, point->n); /* check the starting point with criterion test */ if (IMAGE[point->m][point->n] < intThresh) { printf("ERROR: starting point is not clear.\n"); return NULL; } /* create a region */ temp_region = create_region(); temp_pixel = create_pixel(point->m, point->n); AddPixel(temp_region, temp_pixel); temp_region->visit[point->m][point->n] = 1; /* initialize check_list with starting point */ check_list = create_pixel(point->m, point->n); tail = check_list; cnt = 0; shitcount = 0; while (check_list != NULL) { cnt++; head = check_list; y = head->m; x = head->n; /* printf("Current pixel %d, %d\n",y, x); */ /* check yourself */ tail = merge_check(temp_region, y, x, tail); /* top, center */ tail = merge_check(temp_region, y+1, x, tail); /* top, right */ tail = merge_check(temp_region, y+1, x+1, tail); /* right */ tail = merge_check(temp_region, y, x+1, tail); /* bottom, right */ tail = merge_check(temp_region, y-1, x+1, tail); /* bottom, center */ tail = merge_check(temp_region, y-1, x, tail); /* bottom, left */ tail = merge_check(temp_region, y-1, x-1, tail); /* left */ tail = merge_check(temp_region, y, x-1, tail); /* top, left */ tail = merge_check(temp_region, y+1, x-1, tail); /* de-allocate head */ check_list = check_list->next; free(head); /* printf("Current List : \n"); */ /* DumpList(check_list); */ /*printf("%d ", cnt);*/ } /*printf("check_list size = %d\n", cnt);*/ /*printf("re-visited = %d\n", shitcount);*/ return temp_region; } int ** create_mask(int ** M, struct pixel * pixlist, int mask_level) { struct pixel * tmp; int i,j; if (M == NULL) { /* allocate space and copy IMAGE into M */ M = (int **)calloc(sizeI[0], sizeof(int *)); for (i=0; i < sizeI[0]; i++) { M[i] = (int *)calloc(sizeI[1], sizeof(int)); } for (i=0; i < sizeI[0]; i++) for (j=0; j < sizeI[1]; j++) M[i][j] = IMAGE[i][j]; } /* mask OUT with pix_list */ tmp = pixlist; while (tmp != NULL) { M[tmp->m][tmp->n] = mask_level; tmp = tmp->next; } return M; } int ** create_visit(char ** V) { int ** M; int i,j; int cnt; /* allocate space and copy visit graph into M */ M = (int **)calloc(sizeI[0], sizeof(int *)); for (i=0; i < sizeI[0]; i++) { M[i] = (int *)calloc(sizeI[1], sizeof(int)); } cnt = 0; for (i=0; i < sizeI[0]; i++) for (j=0; j < sizeI[1]; j++) { if (V[i][j] == NULL) M[i][j] = 0; else { M[i][j] = 1; cnt++; } } printf("visited = %d\n", cnt); return M; }