Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
	FILE *fp;
	int w, h, i;
	if ((fp = fopen(argv[1], "r")) == NULL && argc == 1) {
		printf("ERROR: %s can't open %s!", argv[0], argv[1]);
	} else {
		if (read_pgm_hdr(fp, &w, &h) != -1) {
			struct image img, img_gauss, img_out; //img_scratch, img_scratch2, 
			printf("*** PGM file recognized, reading data into image struct ***\n");
			img.width = w;
			img.height = h;
			unsigned char *img_data = malloc(w * h * sizeof(char));
			for (i = 0; i < w * h; i++) {
				img_data[i] = fgetc(fp);
			}
			img.pixel_data = img_data;
			img_out.width = img_gauss.width = w;
			img_out.height = img_gauss.height = h;
			unsigned char *img_gauss_data = malloc(w * h * sizeof(char));
			img_gauss.pixel_data = img_gauss_data;
			unsigned char *img_out_data = malloc(w * h * sizeof(char));
			img_out.pixel_data = img_out_data;
			printf("*** image struct initialized ***\n");
			printf("*** performing gaussian noise reduction ***\n");
			gaussian_noise_reduce(&img, &img_gauss);
			//printf("*** performing morphological closing ***\n");
			//morph_close(&img, &img_scratch, &img_scratch2, &img_gauss);
			canny_edge_detect(&img_gauss, &img_out);
			write_pgm_image(&img_out);
			free(img_data);
			free(img_gauss_data);
			free(img_out_data);
		} else {
			printf("ERROR: %s is not a PGM file!", argv[1]);
		}
	}
	return(1);
}
Ejemplo n.º 2
0
int canny(int j)
{

	int w, h, i,u;
    unsigned char yo[522240];

	u = 0;

	if(u==1)
	{
        readpicture(yo,j);
        img.pixel_data = yo;
	}
	else
	{
       //img.pixel_data =  CurrentFrame.framebits;
	}

    w = 960;
    h = 544;
    img.width = w;
    img.height = h;

    img_out.width = 960;
    img_out.height = 544;

    unsigned char *img_gauss_data = malloc(w * h * sizeof(char));
    img_gauss.pixel_data = img_gauss_data;
    gaussian_noise_reduce(&img, &img_gauss);
    //printf("*** performing morphological closing ***\n");
    //morph_close(&img, &img_scratch, &img_scratch2, &img_gauss);
    canny_edge_detect(&img_gauss, &img);
    write_pgm_image(&img,j);
    free(img_gauss_data);
	return(1);
}
Ejemplo n.º 3
0
int main( int argc, char* args[] )
{
	img_t in, gauss, out;

	hough_t HT = {0};
	lines_t lv = {0};
	roi_t ROI = {0,0, 640, 480};
	HT.l = &lv;
	in.w = out.w = gauss.w = IMG_W;
	in.h = out.h = gauss.h = IMG_H;
	in.r = out.r = gauss.r = &ROI;

	#if STATIC_ALLOC
		unsigned char in_img_data[IMG_W * IMG_H] = {0};
		unsigned char gauss_img_data[IMG_W * IMG_H] = {0};
		unsigned char out_img_data[IMG_W * IMG_H] = {0};
		unsigned int hough_accumulator[(int)(IMG_W*1.4142)*180];
		in.d = in_img_data;
		gauss.d = gauss_img_data;
		out.d = out_img_data;
		HT.accu = hough_accumulator;
	#else
		in.d = malloc(in.w * in.h * sizeof(unsigned char));
		gauss.d = malloc(gauss.w * gauss.h * sizeof(unsigned char));
		out.d = malloc(out.w * out.h * sizeof(unsigned char));
		HT.accu = malloc((int)(IMG_W*1.4142)*180 * sizeof(unsigned int));
	#endif

	#if SANITY_CHECKS
		if(in.d && gauss.d && out.d){
			#if DEBUG
				printf("memory OK\n");
			#endif
		}
	#endif

	#if SDL_USED
		SDL_Surface* screen = NULL;
		SDL_Init(SDL_INIT_EVERYTHING);
	#endif
	#if LOAD_BMP_SDL
		SDL_Surface* input_img = NULL;
		screen = SDL_SetVideoMode(IMG_W, IMG_H, 32, SDL_HWSURFACE);
		input_img = SDL_LoadBMP("input.bmp");
		SDL_BlitSurface(input_img, NULL, screen, NULL);
		{
			unsigned int i, j;
			Uint32 p;
			for(i=0; i<IMG_H; i++){
				for(j=0; j<IMG_W; j++){
					in.d[i*in.w + j] = (unsigned char)get_pixel32(screen, j, i);
				}
			}
		}
	#else

	#endif


		init_hough(&HT);

	#if PRINT_TIME
	clock_t start = clock();
	#endif

	unsigned int i;
	for(i=0; i<ITERATIONS; i++){
		gaussian_noise_reduce(&in, &gauss);	/* Pre-edge detection: some blurring */
		canny_edge_detect(&gauss, &out);	/* Actual edge detection */
		init_hough(&HT);
		hough_transform(&out, &HT); /* Transform */
		hough_getlines(HT.max/3, &HT); /* Analyze the accumulator, fill the detected lines */
	}

	#if PRINT_TIME
	printf("Hough transform - time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
	#endif

	#if DEBUG
		printf("Accumulator info:\n \timage \tw: %d, h: %d\n", HT.img_w, HT.img_h);
		printf("\taccum \tw: %d, h: %d, max: %d\n", HT.w, HT.h, HT.max);
	#endif

	#if DRAW_ACCUM /* Draw the accumulator */
		screen = SDL_SetVideoMode(HT.w, HT.h, 32, SDL_HWSURFACE); /* Set the window to accumulator's size */
		{
			unsigned int i, j;
			Uint32 p;
			for(i=0; i<HT.h; i++){ /* Loop through every pixel */
				for(j=0; j<HT.w; j++){
					p = 0xff*(1-(float)HT.accu[i*HT.w + j]/(float)HT.max); /* Maximize contrast */
					put_pixel32(screen, j, i, (0xff000000 | (p << 16) | (p << 8) | p)); /* Draw each pixel */
				}
			}
		}
		SDL_Flip(screen); /* Flush to window */

		#if WAIT_KEY
			wait();
		#endif
	#endif


	#if DRAW_OUTPUT
		screen = SDL_SetVideoMode(IMG_W, IMG_H, 32, SDL_HWSURFACE);
		{
			unsigned int i, j;
			Uint32 p;
			for(i=0; i<IMG_H; i++){
				for(j=0; j<IMG_W; j++){
					p = out.d[i*out.w + j];
					put_pixel32(screen, j, i, (0xff000000 | (p << 16) | (p << 8) | p));
				}
			}

			for(i=0; i<HT.l->count; i++){ /* Draw all the detected lines */
				lineRGBA(screen, HT.l->l[i].x1, HT.l->l[i].y1, HT.l->l[i].x2, HT.l->l[i].y2, 0xff, 0, 0, 0xff);
			}
		}
		SDL_Flip(screen);

		#if WAIT_KEY
			wait();
		#endif
	#endif

	#if LOAD_BMP_SDL
		SDL_FreeSurface(input_img);
	#endif

	#if SDL_USED
		SDL_FreeSurface(screen);
		SDL_Quit();
	#endif

	#if !STATIC_ALLOC
		free(in.d);
		free(gauss.d);
		free(out.d);
		free(HT.accu);
	#endif

 return 0;
}