示例#1
0
文件: frame.cpp 项目: oakfr/navguide
void
frame_t::convert_grayscale()
{
    int i;

    for (i=0; i<n_img(); i++) {
        convert_grayscale (i);
    }
}
void
detect_edges(Image *img, float sigma, float threshold, \
             unsigned char *edge_pix, PList *edge_pts)
{
	int x, y;
	int w = img->w;
	int h = img->h;

	// convert image to grayscale
	float **gray = array_create(w, h);
	convert_grayscale(img, gray);

	// blur grayscale image
	float **gray2 = array_create(w, h);
	gaussian_blur(gray, gray2, w, h, sigma);

	// compute gradient of blurred image
	float **g_mag = array_create(w, h);
	float **g_ang = array_create(w, h);
	compute_gradient(gray2, w, h, g_mag, g_ang);

	// mark edge pixels
	#define PIX(y,x) edge_pix[(y)*w+(x)]
#pragma omp parallel 
{
#pragma omp for private(x) nowait
	for (y = 0; y < h; y++)
	for (x = 0; x < w; x++)
	{
		PIX(y,x) = is_edge(g_mag,g_ang,threshold,x,y,w,h) ? 255 : 0;
	}

	// connect horizontal edges
#pragma omp for private(x) nowait
	for (y = 0; y < h  ; y++)
	for (x = 1; x < w-1; x++)
	{
		if (!PIX(y,x) && PIX(y,x+1) && PIX(y,x-1))
			PIX(y,x) = 255;
	}

	// connect vertical edges
#pragma omp for private(y) nowait
	for (x = 0; x < w  ; x++)
	for (y = 1; y < h-1; y++)
	{
		if (!PIX(y,x) && PIX(y+1,x) && PIX(y-1,x))
			PIX(y,x) = 255;
	}
#pragma omp for private(x) 
	// connect diagonal edges
	for (y = 1; y < h-1; y++)
	for (x = 1; x < w-1; x++)
	{
		if (!PIX(y,x) && PIX(y-1,x-1) && PIX(y+1,x+1))
			PIX(y,x) = 255;
		if (!PIX(y,x) && PIX(y-1,x+1) && PIX(y+1,x-1))
			PIX(y,x) = 255;
	}
}
	// add edge points to list
	if (edge_pts)
	{
		for (y = 0; y < h; y++)
		for (x = 0; x < w; x++)
		{
			if (PIX(y,x)) PList_push(edge_pts, x, y);
		}
	}

	// cleanup
	array_free(g_mag, h);
	array_free(g_ang, h);
	array_free(gray2, h);
	array_free(gray, h);
}