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

📄 draw.cpp

📁 人头跟踪算法
💻 CPP
字号:
#include "stdafx.h"	// TRACE(), AfxMessageBox(), max(), min()#include <assert.h>#include "draw.h"static char msg[80];extern CDC *my_cdc;  // for debugging, to allow TextOut()extern HDC my_hdc;  // for debugging, to allow displaying of imagesextern OUTLINE outlines[MAX_OUTLINE_SZ + 1];void DrawHistogramBackprojection(ImageBGR24 *img_in,								 ColorHistogram *ch_model, 								 ColorHistogram *ch_background, 								 Image8 *img_out) {	unsigned char *ptri, *ptro;	unsigned char b, g, r;	int color1, color2, color3;	float ratio;	int indx;	int i;	assert(img_in->m == ISIZEX && img_in->n == ISIZEY);	assert(img_out->m == ISIZEX && img_out->n == ISIZEY);	ptri = img_in->GetImagePtr();	ptro = img_out->GetImagePtr();	for (i=0 ; i<ISIZEX*ISIZEY ; i++) {		b = *ptri++;		g = *ptri++;		r = *ptri++;		BGRToColorBins(b, g, r, &color1, &color2, &color3);		indx = color3*N_BINS_COLOR1*N_BINS_COLOR2+color2*N_BINS_COLOR1 + color1;		assert(indx >= 0 && indx < N_BINS_TOT);		ratio = ((float) ch_model->val[indx]) / ch_background->val[indx];		ratio = min(1.0f, ratio);		*ptro++ = (unsigned char) max(0, min(255, 255*ratio));	}}void DrawColorLikelihoodMap(ImageBGR24 *img_in,							ColorHistogram *ch, 							Image8 *img_out) {	unsigned char *ptri, *ptro;	unsigned char b, g, r;	int color1, color2, color3;	int n_pixels;	int indx;	int i;	assert(img_in->m == ISIZEX && img_in->n == ISIZEY);	assert(img_out->m == ISIZEX && img_out->n == ISIZEY);	ptri = img_in->GetImagePtr();	ptro = img_out->GetImagePtr();	for (i=0 ; i<ISIZEX*ISIZEY ; i++) {		b = *ptri++;		g = *ptri++;		r = *ptri++;		BGRToColorBins(b, g, r, &color1, &color2, &color3);		indx = color3*N_BINS_COLOR1*N_BINS_COLOR2+color2*N_BINS_COLOR1 + color1;		assert(indx >= 0 && indx < N_BINS_TOT);		n_pixels = ch->val[indx];		*ptro++ = max(0, min(255, 2*n_pixels));	}}void DrawPixelsInCertainColorBin(ImageBGR24 *img_in,								 ColorHistogram *ch, 								 int bin,								 Image8 *img_out) {	unsigned char *ptri, *ptro;	unsigned char b, g, r;	int color1, color2, color3;	int indx;	int i;	assert(bin >= 0 && bin < N_BINS_TOT);	assert(img_in->m == ISIZEX && img_in->n == ISIZEY);	assert(img_out->m == ISIZEX && img_out->n == ISIZEY);	ptri = img_in->GetImagePtr();	ptro = img_out->GetImagePtr();	for (i=0 ; i<ISIZEX*ISIZEY ; i++) {		b = *ptri++;		g = *ptri++;		r = *ptri++;		BGRToColorBins(b, g, r, &color1, &color2, &color3);		indx = color3*N_BINS_COLOR1*N_BINS_COLOR2+color2*N_BINS_COLOR1 + color1;		assert(indx >= 0 && indx < N_BINS_TOT);		*ptro++ = (indx == bin ? 255 : 0);	}}void DrawMask(int xcen, int ycen, OUTLINE *outline, Image8 *img_mask){	unsigned char *ptri = outline->mask;	unsigned char *ptro = img_mask->GetImagePtr() + 							(ycen-outline->halfsize_y)*ISIZEX + 							(xcen-outline->halfsize_x);	int sizex = 2*(outline->halfsize_x)+1;	int sizey = 2*(outline->halfsize_y)+1;	int x, y;	img_mask->Clear();	for (y=0 ; y<sizey ; y++) {		for (x=0 ; x<sizex ; x++) {			*ptro++ = *ptri++;		}		ptro += ISIZEX - sizex;	}}void DrawPixelListsAndOutline(PixelList *addlist,							  PixelList *sublist,							  int xcen, int ycen,							  EllipseState *state,							  ImageBGR24 *img){	Image8 img_mask(ISIZEX, ISIZEY);	unsigned char *ptri = img->GetImagePtr();	pixelListType *ptra, *ptrs;	int x, y;	int i;	img->Clear();	if (state != NULL) {		// Color ellipse red		DrawMask(state->x, state->y, &outlines[state->sz], &img_mask);		img->FillWithRGB(img_mask.GetImagePtr(), NULL, NULL); 	}	// Color pixels in addlist green	if (addlist != NULL) {		ptra= addlist->pixels;		for (i=addlist->n_pixels ; i>0 ; i--) {			x = xcen + *ptra++;			y = ycen + *ptra++;			*(ptri + 3*(y*ISIZEX + x) + 1) = 255;  // Set green pixel		}	}	// Color pixels in sublist blue	if (sublist != NULL) {		ptrs= sublist->pixels;		for (i=sublist->n_pixels ; i>0 ; i--) {			x = xcen + *ptrs++;			y = ycen + *ptrs++;			*(ptri + 3*(y*ISIZEX + x) + 0) = 255;  // Set blue pixel		}	}}void ShowPixelListsAndOutlines2(SearchStrategy *ss,							   PixelList **add_lists,							   PixelList **sub_lists){#define SLEEP_TIME 0	ImageBGR24 img(ISIZEX,ISIZEY);	EllipseState state;	signed char *ptrss;	int xcen = ISIZEX/2, ycen = ISIZEY/2;	int sz, i;	int n_pix_add, n_pix_sub;	sz = MIN_OUTLINE_SZ + 1;//	for (sz=MIN_OUTLINE_SZ ; sz<=MAX_OUTLINE_SZ ; sz++)  {		ptrss = ss->ds;		for (i=0 ; i<ss->n_states_to_check ; i++) {			state.x  = xcen + *ptrss++;			state.y  = ycen + *ptrss++;			state.sz = sz   + *ptrss++;			if (state.sz>=MIN_OUTLINE_SZ && state.sz<=MAX_OUTLINE_SZ) {				sprintf(msg, "(%3d, %3d, %3d)", state.x, state.y, state.sz);				my_cdc->TextOut(200, 125, msg);				n_pix_add = add_lists[sz][i].n_pixels;				n_pix_sub = sub_lists[sz][i].n_pixels;				assert(n_pix_add * n_pix_add >= 0);				if (n_pix_add>=0) {					DrawPixelListsAndOutline(NULL, NULL,						xcen, ycen, &state, &img);					img.Display(my_hdc, 200, 5);					Sleep(SLEEP_TIME);					DrawPixelListsAndOutline(&add_lists[sz][i], NULL,						xcen, ycen, &state, &img);					img.Display(my_hdc, 200, 5);					Sleep(SLEEP_TIME);					DrawPixelListsAndOutline(NULL, NULL,						xcen, ycen, &state, &img);					img.Display(my_hdc, 200, 5);					Sleep(SLEEP_TIME);					DrawPixelListsAndOutline(NULL, &sub_lists[sz][i], 						xcen, ycen, &state, &img);					img.Display(my_hdc, 200, 5);					Sleep(SLEEP_TIME);				} else {					DrawPixelListsAndOutline(NULL, NULL,						xcen, ycen, &state, &img);				}				img.Display(my_hdc, 200, 5);				Sleep(SLEEP_TIME);			}		}//	}#undef SLEEP_TIME}void OverlayOutlineBGR24(Image8* img_in, 						 EllipseState *state,						 unsigned char r,						 unsigned char g,						 unsigned char b,						 ImageBGR24 *img_out){   unsigned char *ptrin, *ptrout;   short *ptrs;   int sz;   int x, y;   int i;   // Copy image   ptrin = img_in->GetImagePtr();   ptrout = img_out->GetImagePtr();   for (i=0 ; i<ISIZEX*ISIZEY ; i++) {	   *ptrout++ = *ptrin;	   *ptrout++ = *ptrin;	   *ptrout++ = *ptrin++;   }//   tmp1 = curr_state.x;//   tmp2 = curr_state.y;//   tmp3 = curr_state.sz;   	// Overlay ellipse   for (sz = max(state->sz-1, MIN_OUTLINE_SZ) ; sz <= state->sz ; sz++)  {	   x = state->x;	   y = state->y;	   ptrs = outlines[sz].perim;	   ptrout = img_out->GetImagePtr() + 3*(y*ISIZEX + x);       for (i = outlines[sz].perim_length ; i>0; i--)  {           x = *ptrs++;           y = *ptrs++;		   ptrout += 3*(y*ISIZEX + x);		   assert(ptrout>=img_out->GetImagePtr() && ptrout<img_out->GetImagePtr()+3*ISIZEX*ISIZEY);		   *ptrout++ = b; 		   *ptrout++ = g; 		   *ptrout++ = r; 		   ptrout -= 3;       }   }	/*	// Draw confidence	{	   int scaled_conf;	   if (conf < min_conf)  scaled_conf = 0;	   else if (conf > max_conf)  scaled_conf = 100;	   else scaled_conf = (conf - min_conf) / (max_conf - min_conf);	   cpack(0xFF00FF00);	   sboxf((Coord) (ncols-100)/2,					(Coord) nrows - 3,		    (Coord) (ncols-100)/2 + scaled_conf,		(Coord) nrows - 1);	   cpack(0xFF0000FF);	   sboxf((Coord) (ncols-100)/2 + scaled_conf + 1,	(Coord) nrows - 3,		 (Coord) ncols - (ncols-100)/2,			(Coord) nrows - 1);	}	*/}void OverlayOutline8(Image8* img_in, 					 EllipseState *state,					 unsigned char val,					 Image8* img_out){   unsigned char *ptrin, *ptrout;   short *ptrs;   int sz;   int x, y;   int newval;   int i;   // Copy image   *img_out = *img_in;   if (val==255) newval=254;   else if (val==0) newval=1;   else ;   ptrout = img_out->GetImagePtr();   for (i=0 ; i<ISIZEX*ISIZEY ; i++) {	   if (*ptrout==val) *ptrout=newval;	   ptrout++;   }	// Overlay ellipse   ptrin = img_in->GetImagePtr();   ptrout = img_out->GetImagePtr();   for (sz = state->sz ; sz <= state->sz ; sz++)  {//   for (sz = max(state->sz-1, MIN_OUTLINE_SZ) ; sz <= state->sz ; sz++)  {	   x = state->x;	   y = state->y;	   ptrs = outlines[sz].perim;	   ptrout = img_out->GetImagePtr() + x + y*ISIZEX;       for (i = outlines[sz].perim_length ; i>0; i--)  {           x = *ptrs++;           y = *ptrs++;		   ptrout += x + y*ISIZEX;		   assert(ptrout>=img_out->GetImagePtr() && ptrout<img_out->GetImagePtr()+ISIZEX*ISIZEY);		   *ptrout = val;        }   }}

⌨️ 快捷键说明

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