grey8image.cc

来自「VC视频对象的跟踪提取原代码(vc视频监控源码)」· CC 代码 · 共 1,031 行 · 第 1/2 页

CC
1,031
字号
    if (res == NULL)	res = new Grey8Image(new_width, new_height, frame_id, frame_time_in_ms);        unsigned char *odat = res->get_data();    for (int y = 0; y < new_height; y++)    {	unsigned char *in_even = get_pixel(0,y * 2);	unsigned char *in_odd = get_pixel(0,y * 2 + 1);	for (int x = 0; x < new_width; x++)	{	    int curr_pix = *in_even++ + *in_odd++ + 		*in_even++ + *in_odd++;	    *odat++ = (curr_pix >> 2);	}    }    return res;}Image *Grey8Image::blur(Image *res){          unsigned char *upnt, *pnt, *dpnt;    unsigned char *out;    int temp_total;    if (res  == NULL)	res = new Grey8Image(width, height, frame_id, frame_time_in_ms);        for (int y = 1; y < height-1; y++)    {	upnt = get_pixel(0,y-1);	pnt = get_pixel(0,y);	dpnt = get_pixel(0,y+1);	out = res->get_pixel(1,y);	for (int x = 1; x < width; x++)	{	    temp_total  = upnt[0] + upnt[1] + upnt[2] 		+ pnt[0] + pnt[2]		+ dpnt[0] + dpnt[1] + dpnt[2];	    *(out++) = (unsigned char) (temp_total >> 3);	    upnt++; pnt++; dpnt++;	}    }    return res;}Image *Grey8Image::mask(Image *mask, Image *res){    if (mask == NULL)	return NULL;        if (res == NULL) 	res = new Grey8Image(width, height, frame_id, frame_time_in_ms);        if ((mask->get_width() != width) || (mask->get_height() != height))    {	int x;	int y;	register unsigned char *mask_pix;	for (y = 0; y < height; y++)	    for (x = 0; x < width; x++)	    {		if (mask->check_coords(x,y))		{		    mask_pix = mask->get_pixel(x,y);		    if (*mask_pix != (unsigned char) 0x00)			*res->get_pixel(x,y) = *get_pixel(x,y);		}	    }    }    else     {	unsigned char *rdat = res->get_data();	unsigned char *dat1 = data;	unsigned char *dat2 = mask->get_data();	unsigned char *edat = get_end_data();	while (dat1 < edat)	    *rdat++ = *dat1++ & *dat2++;    }        return res;}Image *Grey8Image::fix_holes(int gapsize, Image *res){    int i;    if (res == NULL)	res = new Grey8Image(width, height, frame_id, frame_time_in_ms);    Grey8Image *temp_img1;    Grey8Image *temp_img2;    Grey8Image *dummy;        temp_img1 = new Grey8Image(width, height, frame_id, frame_time_in_ms);    temp_img2 = new Grey8Image(width, height, frame_id, frame_time_in_ms);    temp_img1->clear(CLEAR_MARK);    temp_img2->clear(CLEAR_MARK);    this->maximum(temp_img1);        for (i = 1; i < gapsize; i++)    {	temp_img1->maximum(temp_img2);	dummy = temp_img1;	temp_img1 = temp_img2;	temp_img2 = dummy;    }            for (i = 0; i < gapsize; i++)    {	temp_img1->minimum(temp_img2);	dummy = temp_img1;	temp_img1 = temp_img2;	temp_img2 = dummy;    }        temp_img1->copy(res);    delete temp_img2;    delete temp_img1;    return res;}#ifdef USE_GL /////////////////////////   GL interface    ////////////////////////////long Grey8Image::display(long awindow){#ifdef NO_DISPLAY    return NULLWIN;#else    int width4 = width;        if ((width % 4) != 0)	width4 = 4 * ((width / 4) + 1);        if (awindow != NULLWIN)	glwin = awindow;        if (glwin == NULLWIN)    {	int zoom = Image::display_zoom->value;	prefsize(width4 * zoom, height * zoom);// nts: this gives an error in Ygl version 4.x//	foreground();	if (window_title[0] == 0)  // no title given?  default to this:	    sprintf(window_title," GREY8 image %d by %d ", width, height);		glwin = winopen(window_title);		if (glwin < 0) // error	{	    cerror << " Warning: cannot open window for Grey8Image display "		   << endl << flush;	}		winset(glwin);	reshapeviewport();	prefsize(width4 * zoom, height * zoom);	if (zoom  != 1)	{	    // nts: this is included in Ygl version 4.x !	    rectzoom((float) zoom, (float) zoom);	    viewport(0, width4 * zoom - 1, 0, height * zoom - 1);	    ortho2(-0.5, width4 + 0.5, -0.5, height + 0.5);	}	winconstraints();	cmode();	gflush();	//#ifndef LINUX	for (int k = 0; k < 256; k++)	    mapcolor(k,k,k,k);//#endif		gflush();	gconfig();    }    if (width != width4)    {	Grey8Image tmp_image(width4,height);	tmp_image.clear(CLEAR_MARK);	tmp_image.paste_subimage(0,width-1,0,height-1, this);	winset(glwin);		crectwrite(0,0,width4-1,height-1, (Uint8*)		   tmp_image.get_data());    }    else    {	winset(glwin);	crectwrite(0,0,width-1,height-1,(Uint8 *) data);    }    return glwin;#endif   // #ifdef NO_DISPLAY  #else}/////////////////////////   GL interface ends   ////////////////////////////#else/////////////////////////   X interface   ////////////////////////////#ifndef NO_DISPLAYvoid Grey8Image::setup_greymap(Display *mydisplay, int screen, Window mywindow){    cmap = DefaultColormap(mydisplay, screen);    no_grey_cells = 256;    unsigned int min_cells = 256;        Status res = 0;    unsigned long pix_vals[256];    unsigned int no_cells = no_grey_cells;        while ((res == 0) && (no_cells >= min_cells))    {		res = XAllocColorCells(mydisplay, cmap, False, NULL,			       0, pix_vals, no_cells);	no_cells /= 2;    }        if (res == 0)    {	cmap = XCreateColormap(mydisplay, mywindow,			       DefaultVisual(mydisplay, screen),			       AllocNone);	no_cells = no_grey_cells;	while ((res == 0) && (no_cells >= 8))	{	    res = XAllocColorCells(mydisplay, cmap, True, NULL,				   0, pix_vals, no_cells);	    	    no_cells /= 2;	}		if (res == 0) exit(1);    }        no_cells *= 2;        XColor *grey = (XColor *) new XColor [no_cells];    for (unsigned int cell = 0; cell < no_cells; cell++)    {	unsigned int intensity = (unsigned int) ((cell  * (long) 65536 ) / (no_cells));	grey[cell].red = grey[cell].green = grey[cell].blue =	    intensity;	grey[cell].pixel = pix_vals[cell];	grey[cell].flags = DoRed | DoGreen | DoBlue;    }        for (unsigned int i = 0; i < 256; i++)	reverse_cmap[i] = pix_vals[(i * no_cells) / 256];        XStoreColors(mydisplay, cmap, grey, no_cells);    delete grey;    pixel = (unsigned char) pix_vals[0];}#endif   // #ifndef NO_DISPLAYvoid Grey8Image::virtual_display(){#ifndef NO_DISPLAY    if (mydisplay == NULL)    {	mydisplay = XOpenDisplay("");	int screen = DefaultScreen(mydisplay);	mypixmap = XCreatePixmap(mydisplay,				 DefaultRootWindow(mydisplay), width,				 height, 8);	myimage = XCreateImage( mydisplay, DefaultVisual(mydisplay, screen),				8, ZPixmap, 0, None, width, height,				8, 0);	mygc = XCreateGC (mydisplay, mypixmap, 0, 0);		// X11 works with images stored IA_TOP_TO_BOTTOM and 8-bit grey values.	// Check whether we do, too:	if ((no_grey_cells == 256) && (Image::image_storage_mode == IA_TOP_TO_BOTTOM))	    myimage->data = (unsigned char *) data;	else	    myimage->data = new unsigned char[width * height];    }    if ((unsigned char*) data != myimage->data)    {	Grey8Image tmp_img(width, height, (unsigned char*) myimage->data);	flip_vertically(&tmp_img);    }    XPutImage(mydisplay, mypixmap, mygc, myimage, 0,0,0,0,width,	      height);#endif   // #ifndef NO_DISPLAY}long Grey8Image::display(long dummy){ #ifndef NO_DISPLAY    if (mydisplay == NULL)    {	if (window_title[0] == 0)	{	    if (window_title[0] == 0)  // no title given?  default to this:		sprintf(window_title," GREY8 image %d by %d ", width, height);	}		XSizeHints myhints;	myhints.width = width;	myhints.height = height;	myhints.flags = PSize;	mydisplay = XOpenDisplay("");	int screen = DefaultScreen(mydisplay);	unsigned long white = WhitePixel(mydisplay, screen);	unsigned long black = BlackPixel(mydisplay, screen);	mywindow = XCreateSimpleWindow (mydisplay, DefaultRootWindow(mydisplay)					,100,100,width,height,5,black, white);	mypixmap = XCreatePixmap(mydisplay,				 DefaultRootWindow(mydisplay), width,				 height, 8);	XSetStandardProperties(mydisplay, mywindow, window_title,			       "image", None,			       NULL, 0, &myhints);	XSetWindowBackgroundPixmap(mydisplay, mywindow, mypixmap);	XMapRaised(mydisplay, mywindow);	myimage = XCreateImage( mydisplay, DefaultVisual(mydisplay, screen),				8, ZPixmap, 0, None, width, height,				8, 0);	if (no_using_cmap == 0) setup_greymap(mydisplay, screen, mywindow);	no_using_cmap++;	XSetWindowColormap(mydisplay, mywindow, cmap);//       XSetWindowAttributes xswa;//       xswa.backing_pixel = (unsigned long) pixel;//       xswa.backing_planes = plane_masks[0] | plane_masks[1] | plane_masks[2]// 	| plane_masks[3] | plane_masks[4] | plane_masks[5] | plane_masks[6]// 	  | plane_masks[7];//       XChangeWindowAttributes(mydisplay, mywindow, CWBackingPlanes |// 			      CWBackingPixel, &xswa); 	//mygc = XCreateGC (mydisplay, mywindow, 0, 0);	mygc = XCreateGC (mydisplay, mypixmap, 0, 0);	/*if ((shift == 0) && (offset == 0))	  myimage->data = (char *) data;	  else*/		// X11 works with images stored IA_TOP_TO_BOTTOM and 8-bit grey values.	// Check whether we do, too:	if ((no_grey_cells == 256) && (Image::image_storage_mode == IA_TOP_TO_BOTTOM))	    myimage->data = (unsigned char*) data;	else	    myimage->data = new unsigned char[width * height];    }        if ((unsigned char*) data != myimage->data)    {	unsigned int x,y;	unsigned char *idat, *odat = (unsigned char*) myimage->data;	for (y = height; y > 0; y--)	{	    idat = get_pixel(0, (y-1));	    for (x = 0; x < width; x++)		*odat++ = reverse_cmap[*idat++];	}    }            //XPutImage(mydisplay, mywindow, mygc, myimage, 0,0,0,0,width,        //height);    XPutImage(mydisplay, mypixmap, mygc, myimage, 0,0,0,0,width,	      height);    XClearArea(mydisplay, mywindow, 0,0,0,0, True);    #endif   // #ifndef NO_DISPLAY        return 0;}#endif   // ifdef USE_GL else   ie use X11/////////////////////////   X interface ends   ////////////////////////////Image *Grey8Image::diff_stats(Image *image2, realno &mean,			      realno &variance, Image *res){    if (image2->get_width() != width || image2->get_height() != height)    {	cerror << " can't difference different sized images !";	exit(1);    }    if (res == NULL)	res = new Grey8Image(width, height, frame_id, frame_time_in_ms);    unsigned char *idat1 = data,  *idat2 = image2->get_data()	, *odat = res->get_data(), *enddat = get_end_data();    int diff, no_pixels = enddat - data;    realno total_diff = 0 , total_square = 0;        for ( ;idat1 < enddat; idat1++)    {	diff = (*idat1 - *idat2);	*odat++ =  abs (diff);	total_diff += diff;	total_square += (diff * diff);	idat2++;    }    mean = (total_diff / no_pixels);    variance = (total_square / no_pixels) - (mean * mean);    return res;}Image *Grey8Image::image_blend(Image *i2, int a, int b, Image *res){    // if trying to blend a Greyscale and colour image    if ((i2 != NULL) && (i2->get_image_type() == RGB32))	return i2->image_blend(this,b,a,res);        if (res == NULL)	res = new Grey8Image(width, height, frame_id, frame_time_in_ms);        unsigned char *idat1 = data, *idat2 = i2->get_data(),	*odat = res->get_data(),  *enddat = get_end_data();    int pix;    int aplusb = a + b;        for ( ;idat1 < enddat; idat1++)    {	pix = (a * *idat1) + (b * *idat2);	*odat++ = (pix / aplusb);	idat2++;    }    return res;}Image *Grey8Image::map_intensities(PntGreyMap gmap, Image *res){    if (res == NULL)	res = new Grey8Image(width, height, frame_id, frame_time_in_ms);        unsigned char *idat = data, *odat = res->get_data(),	*enddat = get_end_data();    for ( ;idat < enddat; idat++)	*odat++ = gmap[*idat];        return res;}Image *Grey8Image::b_w_compress(Image *res){    if (res == NULL)	res = new Grey8Image(width, height, frame_id, frame_time_in_ms);    unsigned char *idat = data, *odat = res->get_data();    unsigned char flag;    int counter;    while (idat < end_data)    {	flag = *idat;	counter = -1;	while ((idat < end_data) && (*idat == flag) && counter < 127)	{	    counter++;	    idat++;	}	if (flag == MARK) counter = counter | 128;	*odat++ = counter;    }    res->set_end_data(odat);    return res;}Image *Grey8Image::b_w_uncompress(Image *res){    if (res == NULL)	res = new Grey8Image(width, height, frame_id, frame_time_in_ms);    unsigned char *idat = data, *odat = res->get_data();    unsigned char *edat = res->get_end_data();    unsigned char flag;    int counter;    while (odat < edat)    {	counter = (*idat & 127) + 1;	if ((*idat & 128) != 0)	    flag = MARK;	else	    flag = CLEAR_MARK;	while (counter > 0  && odat < edat)	{	    counter--;	    *odat++ = flag;	}	idat++;    }    return res;}realno Grey8Image::get_marked_fraction(){    int total_marked  = 0;    for (unsigned char *idat = data; idat < end_data; idat ++)    {	if (*idat == MARK)	    total_marked++;    }    return (realno) total_marked / (end_data - data);}void Grey8Image::save_pnm(char *filename)  // save PGM file{    // save as portable grey map (binary mode)    FILE *outfile = NULL;    if (filename == NULL)	outfile = stdout;    else    {	outfile = fopen(filename,"wb");  // `b' may be important on non-UNIX	if (outfile == NULL)	{	    cerror << " Grey8Image::save_pnm: could not open output file 

⌨️ 快捷键说明

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