⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hough.c

📁 图像置乱代码
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>#include <sys/param.h>#include <string.h>#include <tina/all_tina.h>//#include "hough.h"//#include "IM1.h"/*------hough.h--------*/#define R_TO_D 0.017453/*---------------------*/////////////////////////////////////////////////////static Tv *tv = NULL;static Tv *tv1=NULL;static Tv *tv2=NULL;static Tv *tv3=NULL;static int rho_shift_step=1;static int theta_shift_step=3;static int row_shift_step=4;static int col_shift_step=1;static int hough_thresh=100;//static int M_m=255;//static int N_n=255; Imrect *im_orig1=NULL;static Imrect *im_new1=NULL;extern void sorting();extern int  **int_matrix();void free_int_matrix();static void drawline();static void fulldrawo();static void fulldrawn();static void *detect_line(); /*xxxxxxxxxxxxxxxxxxxxxxx*/struct my_line{  Ipos ending1;  Ipos ending2;  struct my_line *next;};typedef struct my_line line_stack; line_stack *first_line_node;line_stack *pp, *q;/*xxxxxxxxxxxxxxxxxxxxxxxxx*//*----------------------------------*/struct lnode{	int ximage;	int yimage;	struct lnode *next;	};struct link_param{	double theta;	double rho;	struct link_param *down;	struct lnode *next;};/*-------------------------------*/struct  line_node{	int xxx;	int yyy;	int count;	struct line_node *next;};//xlinetypedef struct line_node linkmatrix;//linkematrix *ppp[10][10];/*xline** list_Alloc( int width, int height){	int i, j;	xline **p;	p = (xline **)malloc( height * sizeof(xline *) );	for ( i = 0; i< height; i++ )		*(p+i) = ( xline *)malloc( width*sizeof(xline));	for ( i = 0; i < height; i++ )		for( j= 0; j< width; j++ )		{			p[i][j] = (struct link_node *)malloc(sizeof(struct link_node));			p[i][j]->xxx = 0 ;			p[i][j]->yyy = 0;			p[i][j]->count = 0;			p[i][j]->next = NULL ;		}	return p ;}/*-----------------------------*/void   hough_tool(int x, int y){        	static void *tool = NULL;    	static void help_proc(), tv_choice_proc(), image_handler();    	static void output_tv_proc(), draw_proc();    	static void save_param_proc(),save_result_proc(),open_param_proc(),open_result_proc();    	static Imrect *show_image();    	static Imrect *negate_image();    	if (tool)    	{		tw_show_tool(tool);		return;    	}       tv = tv_create("Hough (Input)");
    tv1 = tv_create("Hough (Parameter space)");
    tv2 = tv_create("Hough (Output)");    tv_set_fulldraw(tv, fulldrawo);    //tv_set_fulldraw(tv1, fulldraw);    tv_set_fulldraw(tv2, fulldrawo);    	tool = (void *)tw_tool("Hough Tool", x, y);    	tw_choice("Tv choice", tv_choice_proc, 0, "Input","Parameter space","Output", NULL);
        tw_button("Help", help_proc,NULL);
	tw_newrow();
        tw_iglobal("rho", &rho_shift_step, 3);        tw_iglobal("theta", &theta_shift_step, 3);        tw_newrow();
        tw_iglobal("param_sp_row", &row_shift_step, 3);        tw_iglobal("col", &col_shift_step, 3);
	tw_newrow();
        tw_iglobal("hough_threshold", &hough_thresh, 3);
	tw_newrow();        //tw_iglobal("M_m", &M_m, 3);	//tw_iglobal("N_n", &N_n, 3);	tw_newrow();        tw_button("Show", image_handler, show_image);	tw_button("Negate", image_handler, negate_image);	tw_newrow();        tw_button("Detect_line", detect_line);        tw_button("Draw",draw_proc);	tw_newrow();         tw_button("Save param_space",save_param_proc);         tw_button("Save hough result",save_result_proc);         tw_newrow();         tw_button("Open param-space",open_param_proc);         tw_button("Open hough result",open_result_proc);    	tw_end_tool();}/* Pop, process, push & display image. */static void image_handler(processing_fn)
Imrect *(processing_fn) ();
{
    Imrect *im_orig;
    Imrect *im_new;
    int     type;

    /* Check image on stack is right type */
    if ((Bool) stack_check_types(IMRECT, NULL) == true)
    	{
	/* Pop the image off the stack (this is NOT a copy) */
	im_orig = (Imrect *) stack_pop(&type);

	/* Process (eg negate) image */
	im_new = processing_fn(im_orig);

	/* Push new image onto the stack */
	stack_push(im_new, IMRECT, im_free);

	/* Free the memory used by the old image */
	im_free(im_orig);

	/* Scale the new image (adjust brightness & contrast,so all
	 * pixels are between 0 & 255) */
	im_new = imf_scale(im_new, 0.0, 255.0);

	/* Display the new image */
	tv_imrect2(tv, im_new);        
    	}
    else error("image_handler: wrong type on stack", non_fatal);
}static void fulldrawo(Tv *tv){     tv_imrect2(tv, im_orig1);}static void fulldrawn(Tv *tv){     tv_imrect2(tv, im_new1);}/* Show image */
static Imrect *show_image(im_orig)
Imrect *im_orig;
{	first_line_node=(line_stack*)malloc(sizeof (line_stack));    	first_line_node->next=NULL;	im_new1 = im_orig;	im_orig1 = im_orig;
    return im_copy(im_orig);
}static Imrect *negate_image(im_orig)
Imrect *im_orig;
{
    Imrect *im_new = NULL;	/* New image */
    Imregion *region;		/* Image region (ie where the pixels
				 * are) */

    /* If image (and its region) is NOT NULL then process it */
    if (im_orig && (region = im_orig->region))
    	{
	int     x1 = region->lx;/* Top left x co-ord */
	int     y1 = region->ly;/* Top left y co-ord */
	int     x2 = region->ux;/* Bottom right x co-ord */
	int     y2 = region->uy;/* Bottom right y co-ord */
	int     row, col;

	/* Create a new (empty) image to work on, with the same width,
	 * height, region and pixel type as the original */
	im_new = im_alloc(im_orig->height, im_orig->width, region, im_orig->vtype);

	for (row = y1; row < y2; ++row)		/* For each row in the image. */
	    for (col = x1; col < x2; ++col)	/* For each pixel in the row */
	    	{
		/* Apply function to each pixel in each row */
		float   gl_orig = im_get_pixf(im_orig, row, col);
		float   gl_new  = -gl_orig;	/* FLIP PIXEL */
		im_put_pixf(gl_new, im_new, row, col);
	    	}
    	}
     return im_new;
} 
static void *detect_line()

{
    Imrect *im_new = NULL;	/* New image */
    Imregion *region;		/* Image region (ie where the pixels
				 * are) */	   /* If image (and its region) is NOT NULL then process it */
    if (im_new1 && (region = im_new1->region))
    	{
	int     x1 = region->lx;/* Top left x co-ord */
	int     y1 = region->ly;/* Top left y co-ord */
	int     x2 = region->ux;/* Bottom right x co-ord */
	int     y2 = region->uy;/* Bottom right y co-ord */
	int     drow, dcol;        float gl_orig,gl_new; 
	/* Create a new (empty) image to work on, with the same width,
	 * height, region and pixel type as the original */
	im_new = im_alloc(im_new1->height, im_new1->width, region, im_new1->vtype);    	int N1 = im_new1->width;//number of rows of image	int N2 = im_new1->height;//column	float SQRTD = sqrt ((float)N1*(float)N1 + (float)N2 * (float)N2);	//int N_n = floor( N1 / theta_shift_step);//number of rows of matrix p	//int M_m = floor( N2 / rho_shift_step);//cloumn	//int N_n = 180;	//int M_m = SQRTD;	int N_n = N1;	int M_m = N2;	printf("N1=%d,N2=%d    N_n=%d,M_m=%d\n",N1,N2,N_n,M_m);	//int  **xin = (int **)int_matrix(N1, N2);	//int  **xout = (int **)int_matrix(N1, N2);	//int  **p = (int **)int_matrix(N_n, M_m);	int xin[N1][N2];	int xout[N1][N2];	int p[N_n][M_m];		struct link_param *pp;	float COS[N_n],SIN[N_n];	int k,l,i,j,kk,ll,y;	float r,b;	float th;		for ( i = 0; i < N_n; i++)		{			th=(float)  i * 180.0 / (N_n-1)-90;			th= th*R_TO_D;			COS[i] = ( double )cos((double)th);			SIN[i] = ( double )sin((double)th);			//printf("i = %d,COS=%3.2f,SIN=%3.2f\n",i,COS[i],SIN[i]);		}/*-90<=theta<=90*/	for (drow = y1; drow < y2; ++drow)		/* For each row in the image. */
	    for (dcol = x1; dcol < x2; ++dcol)	/* For each pixel in the row */
	    	{			if(im_get_pixf(im_new1, drow, dcol) == 255)				xin[drow][dcol]=0;//white			else 				{xin[drow][dcol]=1;//black				//printf("xin[%d][%d]\n",drow,dcol);				}			xout[drow][dcol]=0;					}	printf("y1=%d,y2=%d,x1=%d,x2=%d  w=%d h=%d\n",y1,y2,x1,x2,N1,N2);//struct link_param *ptr1;//ptr1 = (struct link_param *)malloc(sizeof(struct link_param));		/*Initialize matrix p*/		for (kk = 0; kk < N_n;kk++)			for( ll = 0; ll < M_m; ll ++)			{				p[kk][ll] = 0;				//ptr1->theta = kk;				//ptr1->rho = ll;				//ptr1->next = NULL;				//ptr1->down = NULL;							}	int rd;  	Ipos pos1;	for(k = 0;k <N1;k++)		for( l = 0;l < N2 ; l ++)		{			if ( xin[k][l] ==1)			{				for(i = 0;i < N_n;i+=theta_shift_step)				{					r=k*COS[i] + l*SIN[i];/*---------------------------------------------------------------------*  					if((fmod(k,row_shift_step) == 0)&&(fmod(l,col_shift_step) == 0) )						{						rd = floor(r);                                  		pos1=ipos(i,rd);						tv_set_color(tv1,red);                        			tv_point(tv1,pos1);					}/*--------------------------------------------------------------------*/					b = SQRTD;					r += b;					r /= (SQRTD*2.0);					r *= ( M_m -1 );					r += 0.5 ;   					j = floor (r);/*--------------------------------------------------------------------*/  					if((fmod(k,row_shift_step) == 0)&&(fmod(l,col_shift_step) == 0) )						{						rd = floor(r);                                  		pos1=ipos(i,rd);						tv_set_color(tv1,blue);                        			tv_point(tv1,pos1);					}/*--------------------------------------------------------------------*/					p[i][j]++;/*					ptr1->theta = i;					ptr1->rho = j;					ptr1->next = NULL;					ptr1->down = NULL;					//struct lnode ptr/*--------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -