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

📄 gravitycenter.cpp

📁 数字图像处理
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	for(i=EDGE_SPACE;i<nHeight-EDGE_SPACE;i++)
	{
		for(j=EDGE_SPACE;j<nWidth-EDGE_SPACE;j++)
		{
			tmp = sGray[i*nWidth+j];
			his[tmp]++;
			if(tmp>grayMax)
				grayMax = tmp;
		}
	}
	//calculate the contrast of image
	calHistNum = (nWidth-2*EDGE_SPACE)*(nHeight-2*EDGE_SPACE);
	for(i= 0;i<256;i++)
	{
		mean+=his[i]*i;
	}
	mean = mean/calHistNum;
	whiteContrast = 100*(grayMax-mean)/grayMax;

	if(whiteContrast>30) threshold = grayMax-50;
	else if (whiteContrast>27) threshold = grayMax-30;
	else if (whiteContrast>24) threshold = grayMax-20;
	else if (whiteContrast>20) threshold = grayMax-15;
	else if (whiteContrast>9) threshold = grayMax-11;
	else if (whiteContrast>4) threshold = grayMax-6;
	else threshold = grayMax;

	return threshold;

}

//直方图法求窗口内阈值
int CGravityCenter::GetWThByHistogram(unsigned char *sGray, WININFO sWin, int nWidth, int nHeight)
{
	int threshold;
	int mean=0;
	int grayMax=0;
	int calHistNum=0;
	int whiteContrast=0;
	int i,j,tmp;
	int his[256];
	for(i= 0;i<256;i++)
		his[i] = 0;
	//calculate the histogram of image
	for(j=sWin.pt.y-sWin.h;j<sWin.pt.y+sWin.h;j++)
	{
		for(i=sWin.pt.x-sWin.w;i<sWin.pt.x+sWin.w;i++)
		{
			tmp = sGray[i*nWidth+j];
			his[tmp]++;
			if(tmp>grayMax)
				grayMax = tmp;
			calHistNum++;
		}
	}
	//calculate the contrast of image
	for(i= 0;i<256;i++)
	{
		mean+=his[i]*i;
	}
	mean = mean/calHistNum;
	whiteContrast = 100*(grayMax-mean)/grayMax;

	if(whiteContrast>30) threshold = grayMax-50;
	else if (whiteContrast>27) threshold = grayMax-30;
	else if (whiteContrast>24) threshold = grayMax-20;
	else if (whiteContrast>20) threshold = grayMax-15;
	else if (whiteContrast>9) threshold = grayMax-11;
	else if (whiteContrast>4) threshold = grayMax-6;
	else threshold = grayMax;


	return threshold;

}

//利用目标面积求阈值
int CGravityCenter::GetWThByAera(unsigned char *sGray, WININFO sWin, int nWidth, int nHeight)
{
	int threshold;
	int grayMax=0;
	int i,j,tmp;
	int his[256];
	for(i= 0;i<256;i++)
		his[i] = 0;
	//calculate the histogram of image
	for(j=sWin.pt.y-sWin.h;j<sWin.pt.y+sWin.h;j++)
	{
		for(i=sWin.pt.x-sWin.w;i<sWin.pt.x+sWin.w;i++)
		{
			tmp = sGray[i*nWidth+j];
			his[tmp]++;
			if(tmp>grayMax)
				grayMax = tmp;
		}
	}

	float rate=(sWin.w-EDGE_DRAW)*(sWin.h-EDGE_DRAW)+0.0f/(sWin.w*sWin.h);
    threshold = int(grayMax-grayMax*rate);

	return threshold;

}

//调整边界窗口
WININFO CGravityCenter::AdjustWindow(WININFO sWin, int sWidth, int sHeight)
{
	WININFO win;
	CRect rt;
	rt.left = sWin.pt.x-sWin.w;
	rt.right = sWin.pt.x+sWin.w;
	rt.top = sWin.pt.y-sWin.h;
	rt.bottom = sWin.pt.y+sWin.h;
	
	if(rt.left<2)
	{
		rt.left = 2;
		rt.right = (sWin.pt.x+sWin.w)<42 ? 42:(sWin.pt.x+sWin.w);
	}
	if(rt.right>sWidth-2)
	{
		rt.right = sWidth-2;
		rt.left = (sWin.pt.x-sWin.w)>=(sWidth-42) ? (sWidth-42):(sWin.pt.x-sWin.w);
	}
	if(rt.top<2)
	{
		rt.top = 2;
		rt.bottom = (sWin.pt.y+sWin.h)<42 ? 42:(sWin.pt.y+sWin.h);
	}
	if(rt.bottom>sHeight-2)
	{
		rt.bottom = sHeight-2;
		rt.top = (sWin.pt.y-sWin.h)>=(sHeight-42) ? (sHeight-42):(sWin.pt.y-sWin.h);
	}

	win.w =  (rt.right-rt.left)/2;
	win.h =  (rt.bottom-rt.top)/2;
	win.pt.x = rt.left+win.w;
	win.pt.y = rt.top+win.h;
	win.flag = sWin.flag;
	
	return win;

}

//多目标跟踪中擦除目标
void CGravityCenter::erasure(unsigned char* sGray, WININFO sWin, int sWidth, int sHeight)
{
	int i,j;
	for(j=sWin.pt.y-sWin.h;j<sWin.pt.y+sWin.h;j++)
	{
		for(i=sWin.pt.x-sWin.w;i<sWin.pt.x+sWin.w;i++)
		{
			int k = sWidth*j+i;
			sGray[k] = (unsigned char)0;
		}
	}
}

//重心跟踪
WININFO CGravityCenter::gvtrack(unsigned char* sGray,int sWidth, int sHeight, WININFO sWin, int sThreshold)
{
	WININFO win;
	win.w = 0;
	win.h = 0;
	win.pt.x = 0;
	win.pt.y = 0;
	win.flag = FALSE;
	
	int M00bh = 0;
	int M01bh = 0;
	int M10bh = 0;
	int i,j,iLine;//iCol;
	for(j=sWin.pt.y-sWin.h;j<sWin.pt.y+sWin.h;j++)
	{
		iLine = 0;
		for(i=sWin.pt.x-sWin.w;i<sWin.pt.x+sWin.w;i++)
		{
			int k = sWidth*j+i;
			if(sGray[k]>sThreshold)
			{
				iLine++;
				M01bh+=i;
				M10bh+=j;
			}
		}
		M00bh+=iLine;
		if(win.w<iLine) win.w = iLine;
		if(iLine>3) win.h++;
	}

	if(M00bh>10)
	{
		win.pt.x = int(M01bh/M00bh+0.5);
		win.pt.y = int(M10bh/M00bh+0.5);
		win.w = win.w/2+EDGE_DRAW;
		win.h = win.h/2+EDGE_DRAW;
		win.flag = TRUE;
	}
	
	return win;

}
//投影找最大值法
WININFO CGravityCenter::project(unsigned char* sGray, int sWidth, int sHeight, int sThreshold)
{
	WININFO win;
	win.w = 0;
	win.h = 0;
	win.pt.x = 0;
	win.pt.y = 0;
	win.flag = FALSE;
	
	int X_MaxLocation=0, X_MaxLength=0;
	int Y_MaxLocation=0, Y_MaxLength=0;

	int *row, *column;
	row = new int[sWidth];
	column = new int[sHeight];
	memset(row,0,sWidth*sizeof(int));
	memset(column,0,sHeight*sizeof(int));

	int i,j,t;
	for(j=EDGE_SPACE; j<sWidth-EDGE_SPACE; j++)
	{
		for (i=EDGE_SPACE; i<sHeight-EDGE_SPACE; i++)
		{
			int k = i*sWidth+j;
			if(sGray[k]>sThreshold)
				row[j] = 1;
		}
		
	}
	for(i=EDGE_SPACE; i<sHeight-EDGE_SPACE; i++)
	{
		for (j=EDGE_SPACE; j<sWidth-EDGE_SPACE; j++)
		{
			int k = i*sWidth+j;
			if(sGray[k]>sThreshold)
				column[i] = 1;
		}
		
	}
	
	int location=0;
	int count=0;
	int length=0;
	for(t=0; t<sWidth; t++)
	{
		if(row[t] == 1)
		{
			count++;
			length = count;
			location = t-length+1;
		}
		else
		{
			count = 0;
			if(length>X_MaxLength)
			{
				X_MaxLocation = location;
				X_MaxLength = length;
			}
		}

	}

	location=0;
	count=0;
	length=0;
	for(t=0; t<sHeight; t++)
	{
		if(column[t] == 1)
		{
			count++;
			length = count;
			location = t-length+1;
		}
		else
		{
			count = 0;
			if(length>Y_MaxLength)
			{
				Y_MaxLocation = location;
				Y_MaxLength = length;
			}
		}
	}
	
	delete []row;
	delete []column;

	win.pt.x = int(X_MaxLocation+X_MaxLength/2+0.5);
	win.pt.y = int(Y_MaxLocation+Y_MaxLength/2+0.5);
	win.w = X_MaxLength/2+EDGE_DRAW;
	win.h = Y_MaxLength/2+EDGE_DRAW;
	if((X_MaxLength+Y_MaxLength)>5)
		win.flag = TRUE;

	return win;

}

//重心法
WININFO CGravityCenter::gravity(unsigned char* sGray, int sWidth, int sHeight, int sThreshold)
{
	WININFO win;
	win.w = 0;
	win.h = 0;
	win.pt.x = 0;
	win.pt.y = 0;
	win.flag = FALSE;
	
	int M00bh = 0;
	int M01bh = 0;
	int M10bh = 0;
	int iLine,i,j;
	for(j=EDGE_SPACE;j<sHeight-EDGE_SPACE;j++)
	{
		iLine = 0;
		for(i=EDGE_SPACE;i<sWidth-EDGE_SPACE;i++)
		{
			int k = sWidth*j+i;
			if(sGray[k]>sThreshold)
			{
				iLine++;
				M01bh+=i;
				M10bh+=j;
			}
		}
		M00bh+=iLine;
		if(win.w<iLine) win.w = iLine;
		if(iLine>0) win.h++;
	}

	if(M00bh>10)
	{
		win.pt.x = int(M01bh/M00bh+0.5);
		win.pt.y = int(M10bh/M00bh+0.5);
		win.w = win.w/2+EDGE_DRAW;
		win.h = win.h/2+EDGE_DRAW;
		win.flag = TRUE;
	}

	return win;

}

//找边法
WININFO CGravityCenter::findEdge(unsigned char* sGray, int sWidth, int sHeight, int sThreshold, int sBlockNum)
{
	WININFO win;
	win.w = 0;
	win.h = 0;
	win.pt.x = 0;
	win.pt.y = 0;
	win.flag = FALSE;

	int blockH = sHeight/sBlockNum;
	int x=0,w=0,n=0;
	int i,j,t=0;
	int tStart, tEnd;
	int *row = new int[sWidth];

	while(n<sBlockNum)
	{
		memset(row,0,sWidth*sizeof(int));

		for(j=EDGE_SPACE; j<sWidth-EDGE_SPACE; j++)
		{
			for (i=n*blockH; i<(n+1)*blockH; i++)
			{
				int k = i*sWidth+j;
				if(sGray[k]>sThreshold)
					row[j] = 1;
			}
			
		}

		for(t=0;t<sWidth;t++)
		{
			if(row[t]==1)
			{
				tStart=t;
				while(row[t]!=0) t++;
				tEnd = t;
				w = (tEnd-tStart)/2;
				if(w>1)
				{
					x = tStart+(tEnd-tStart)/2;
					break;
				}
			}

		}

		if(x>2) 
		{
			win.pt.x = x;
			win.pt.y = n*blockH+blockH/2;
			win.w = w+10;
			win.h = blockH/2;
			win.flag = TRUE;
			break;
		}
		else n++;
	}
	delete []row;
	row = NULL;

	return win;

}

⌨️ 快捷键说明

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