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

📄 dibimage.cpp

📁 人耳识别技术是20世纪90年代末开始兴起的一种生物特征识别技术
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	if(nMode == 0)
	{
		//使用水平方向的结构元素进行膨胀
		for(j = 0; j <lHeight; j++)
		{
			for(i = 1;i <lWidth-1; i++)
			{
				// 由于使用1×3的结构元素,为防止越界,所以不处理最左边和最右边
				// 的两列像素

				// 指向源图像倒数第j行,第i个象素的指针			
				lpSrc = (char *)lpDIBBits + lWidth * j + i;
				// 指向目标图像倒数第j行,第i个象素的指针			
				lpDst = (char *)lpNewDIBBits + lWidth * j + i;
				//取得当前指针处的像素值,注意要转换为unsigned char型
				pixel = (unsigned char)*lpSrc;

				//目标图像中含有0和255外的其它灰度值
				if(pixel != 255 && *lpSrc != 0)
				{
					return FALSE;
				}
				
				//目标图像中的当前点先赋成白色
				*lpDst = (unsigned char)255;

				//源图像中当前点自身或者左右只要有一个点是黑色,
				//则将目标图像中的当前点赋成黑色
				for (n = 0;n < 3;n++ )
				{
					pixel = *(lpSrc+n-1);
					if (pixel == 0 )
					{
						*lpDst = (unsigned char)0;
						break;
					}
				}			
			}
		}
	}
	else if(nMode == 1)
	{
		//使用垂直方向的结构元素进行膨胀
		for(j = 1; j <lHeight-1; j++)
		{
			for(i = 0;i <lWidth; i++)
			{
				// 由于使用1×3的结构元素,为防止越界,所以不处理最上边和最下边
				// 的两列像素

				// 指向源图像倒数第j行,第i个象素的指针			
				lpSrc = (char *)lpDIBBits + lWidth * j + i;
				// 指向目标图像倒数第j行,第i个象素的指针			
				lpDst = (char *)lpNewDIBBits + lWidth * j + i;
				//取得当前指针处的像素值,注意要转换为unsigned char型
				pixel = (unsigned char)*lpSrc;

				//目标图像中含有0和255外的其它灰度值
				if(pixel != 255 && *lpSrc != 0)
				{
					return FALSE;
				}

				//目标图像中的当前点先赋成白色
				*lpDst = (unsigned char)255;

				//源图像中当前点自身或者上下只要有一个点是黑色,
				//则将目标图像中的当前点赋成黑色
				for (n = 0;n < 3;n++ )
				{
					pixel = *(lpSrc+(n-1)*lWidth);
					if (pixel == 0 )
					{
						*lpDst = (unsigned char)0;
						break;
					}
				}				
			}
		}
	}
	else
	{
		//使用自定义的结构元素进行膨胀
		for(j = 1; j <lHeight-1; j++)
		{
			for(i = 0;i <lWidth; i++)
			{
				// 由于使用3×3的结构元素,为防止越界,所以不处理最左边和最右边
				// 的两列像素和最上边和最下边的两列像素

				// 指向源图像倒数第j行,第i个象素的指针			
				lpSrc = (char *)lpDIBBits + lWidth * j + i;
				// 指向目标图像倒数第j行,第i个象素的指针			
				lpDst = (char *)lpNewDIBBits + lWidth * j + i;
				//取得当前指针处的像素值,注意要转换为unsigned char型
				pixel = (unsigned char)*lpSrc;

				//目标图像中含有0和255外的其它灰度值
				if(pixel != 255 && *lpSrc != 0)
				{
					return FALSE;
				}

				//目标图像中的当前点先赋成白色
				*lpDst = (unsigned char)255;

				//原图像中对应结构元素中为黑色的那些点中只要有一个是黑色,
				//则将目标图像中的当前点赋成黑色
				//注意在DIB图像中内容是上下倒置的
				for (m = 0;m < 3;m++ )
				{
					for (n = 0;n < 3;n++)
					{
						if( structure[m][n] == -1)
							continue;
						pixel = *(lpSrc + ((2-m)-1)*lWidth + (n-1));
						if (pixel == 0 )
						{	
							*lpDst = (unsigned char)0;
							break;
						}
					}
				}			
			}
		}
	}
	// 复制膨胀后的图像
	memcpy(lpDIBBits, lpNewDIBBits, lWidth * lHeight);

	LocalUnlock(hNewDIBBits);
	LocalFree(hNewDIBBits);

	return TRUE;
}

/*************************************************************************
 * 函数名称:
 *   CloseDIB()
 * 参数:
 *   LPSTR lpDIBBits    - 指向源DIB图像指针
 *   LONG  lWidth       - 源图像宽度(象素数,必须是4的倍数)
 *   LONG  lHeight      - 源图像高度(象素数)
 *   int   nMode		- 闭运算方式:0表示水平方向,1垂直方向,2自定义结构元素。
 *	 int   structure[3][3]	- 自定义的3×3结构元素。
 * 返回值:
 *   BOOL               - 闭运算成功返回TRUE,否则返回FALSE。
 * 说明:
 * 该函数用于对图像进行开运算。结构元素为水平方向或垂直方向的三个点,
 * 中间点位于原点;或者由用户自己定义3×3的结构元素。
 * 要求目标图像为只有0和255两个灰度值的灰度图像。
 ************************************************************************/
BOOL CDibImage::CloseDIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight, int nMode , 
						 int structure[3][3])
{
	if (DilationDIB(lpDIBBits, lWidth, lHeight, nMode , structure))
	{
		if (ErosionDIB(lpDIBBits, lWidth, lHeight, nMode , structure))
		{
			return TRUE;
		}
	}
	return FALSE;
}

/*************************************************************************
 * 函数名称:
 *   SobelDIB()
 * 参数:
 *   LPSTR lpDIBBits    - 指向源DIB图像指针
 *   LONG  lWidth       - 源图像宽度(象素数,必须是4的倍数)
 *   LONG  lHeight      - 源图像高度(象素数)
 * 返回值:
 *   BOOL               - 边缘检测成功返回TRUE,否则返回FALSE。
 * 说明:
 * 该函数用Sobel边缘检测算子对图像进行边缘检测运算。
 * 要求目标图像为灰度图像。
 ************************************************************************/
BOOL CDibImage::SobelDIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight)
{	
	LPSTR	lpDst1;				// 指向缓存图像的指针
	LPSTR	lpDst2;		
	LPSTR	lpNewDIBBits1;		// 指向缓存DIB图像的指针
	HLOCAL	hNewDIBBits1;
	LPSTR	lpNewDIBBits2;
	HLOCAL	hNewDIBBits2;
	
	long	i,j;				// 循环变量	
	int		iTempH;				// 模板高度	
	int		iTempW;				// 模板宽度	
	FLOAT	fTempC;				// 模板系数	
	int		iTempMX;			// 模板中心元素X坐标	
	int		iTempMY;			// 模板中心元素Y坐标	
	FLOAT	aTemplate[9];		// 模板数组

	// 暂时分配内存,以保存新图像
	hNewDIBBits1 = LocalAlloc(LHND, lWidth * lHeight);
	if (hNewDIBBits1 == NULL)
	{
		return FALSE;
	}
	
	lpNewDIBBits1 = (char * )LocalLock(hNewDIBBits1);

	// 暂时分配内存,以保存新图像
	hNewDIBBits2 = LocalAlloc(LHND, lWidth * lHeight);
	if (hNewDIBBits2 == NULL)
	{
		return FALSE;
	}
	
	lpNewDIBBits2 = (char * )LocalLock(hNewDIBBits2);

	// 拷贝源图像到缓存图像中
	lpDst1 = (char *)lpNewDIBBits1;
	memcpy(lpNewDIBBits1, lpDIBBits, lWidth * lHeight);
	lpDst2 = (char *)lpNewDIBBits2;
	memcpy(lpNewDIBBits2, lpDIBBits, lWidth * lHeight);

	// 设置Sobel模板1参数
	iTempW = 3;
	iTempH = 3;
	fTempC = 1.0;
	iTempMX = 1;
	iTempMY = 1;
	aTemplate[0] = -1.0;
	aTemplate[1] = -2.0;
	aTemplate[2] = -1.0;
	aTemplate[3] = 0.0;
	aTemplate[4] = 0.0;
	aTemplate[5] = 0.0;
	aTemplate[6] = 1.0;
	aTemplate[7] = 2.0;
	aTemplate[8] = 1.0;

	if (!Template(lpNewDIBBits1, lWidth, lHeight, 
		iTempH, iTempW, iTempMX, iTempMY, aTemplate, fTempC))
	{
		return FALSE;
	}

	// 设置Sobel模板2参数
	aTemplate[0] = 0.0;
	aTemplate[1] = -1.0;
	aTemplate[2] = -2.0;
	aTemplate[3] = 1.0;
	aTemplate[4] = 0.0;
	aTemplate[5] = -1.0;
	aTemplate[6] = 2.0;
	aTemplate[7] = 1.0;
	aTemplate[8] = 0.0;

	if (!Template(lpNewDIBBits2, lWidth, lHeight, 
		iTempH, iTempW, iTempMX, iTempMY, aTemplate, fTempC))
	{
		return FALSE;
	}

	//求两幅缓存图像的最大值
	for(j = 0; j <lHeight; j++)
	{
		for(i = 0;i <lWidth-1; i++)
		{
			// 指向缓存图像1倒数第j行,第i个象素的指针			
			lpDst1 = (char *)lpNewDIBBits1 + lWidth * j + i;
			// 指向缓存图像2倒数第j行,第i个象素的指针			
			lpDst2 = (char *)lpNewDIBBits2 + lWidth * j + i;
			
			if(*lpDst2 > *lpDst1)
			{
				*lpDst1 = *lpDst2;
			}
		}
	}

	// 复制经过模板运算后的图像到源图像
	memcpy(lpDIBBits, lpNewDIBBits1, lWidth * lHeight);

	// 设置Sobel模板3参数
	aTemplate[0] = 1.0;
	aTemplate[1] = 0.0;
	aTemplate[2] = -1.0;
	aTemplate[3] = 2.0;
	aTemplate[4] = 0.0;
	aTemplate[5] = -2.0;
	aTemplate[6] = 1.0;
	aTemplate[7] = 0.0;
	aTemplate[8] = -1.0;

	if (!Template(lpNewDIBBits2, lWidth, lHeight, 
		iTempH, iTempW, iTempMX, iTempMY, aTemplate, fTempC))
	{
		return FALSE;
	}

	//求两幅缓存图像的最大值
	for(j = 0; j <lHeight; j++)
	{
		for(i = 0;i <lWidth-1; i++)
		{
			// 指向缓存图像1倒数第j行,第i个象素的指针			
			lpDst1 = (char *)lpNewDIBBits1 + lWidth * j + i;
			// 指向缓存图像2倒数第j行,第i个象素的指针			
			lpDst2 = (char *)lpNewDIBBits2 + lWidth * j + i;
			
			if(*lpDst2 > *lpDst1)
			{
				*lpDst1 = *lpDst2;
			}
		}
	}

	// 复制经过模板运算后的图像到源图像
	memcpy(lpDIBBits, lpNewDIBBits1, lWidth * lHeight);

	// 设置Sobel模板4参数
	aTemplate[0] = 2.0;
	aTemplate[1] = 1.0;
	aTemplate[2] = 0.0;
	aTemplate[3] = 1.0;
	aTemplate[4] = 0.0;
	aTemplate[5] = -1.0;
	aTemplate[6] = 0.0;
	aTemplate[7] = -1.0;
	aTemplate[8] = -2.0;

	if (!Template(lpNewDIBBits2, lWidth, lHeight, 
		iTempH, iTempW, iTempMX, iTempMY, aTemplate, fTempC))
	{
		return FALSE;
	}

	//求两幅缓存图像的最大值
	for(j = 0; j <lHeight; j++)
	{
		for(i = 0;i <lWidth-1; i++)
		{
			// 指向缓存图像1倒数第j行,第i个象素的指针			
			lpDst1 = (char *)lpNewDIBBits1 + lWidth * j + i;
			// 指向缓存图像2倒数第j行,第i个象素的指针			
			lpDst2 = (char *)lpNewDIBBits2 + lWidth * j + i;
			
			if(*lpDst2 > *lpDst1)
			{
				*lpDst1 = *lpDst2;
			}
		}
	}

	// 复制经过模板运算后的图像到源图像
	memcpy(lpDIBBits, lpNewDIBBits1, lWidth * lHeight);

	// 设置Sobel模板5参数
	aTemplate[0] = 1.0;
	aTemplate[1] = 2.0;
	aTemplate[2] = 1.0;
	aTemplate[3] = 0.0;
	aTemplate[4] = 0.0;
	aTemplate[5] = 0.0;
	aTemplate[6] = -1.0;
	aTemplate[7] = -2.0;
	aTemplate[8] = -1.0;

	if (!Template(lpNewDIBBits2, lWidth, lHeight, 
		iTempH, iTempW, iTempMX, iTempMY, aTemplate, fTempC))
	{
		return FALSE;
	}

	//求两幅缓存图像的最大值
	for(j = 0; j <lHeight; j++)
	{
		for(i = 0;i <lWidth-1; i++)
		{
			// 指向缓存图像1倒数第j行,第i个象素的指针			
			lpDst1 = (char *)lpNewDIBBits1 + lWidth * j + i;
			// 指向缓存图像2倒数第j行,第i个象素的指针			
			lpDst2 = (char *)lpNewDIBBits2 + lWidth * j + i;
			
			if(*lpDst2 > *lpDst1)
			{
				*lpDst1 = *lpDst2;
			}
		}
	}

	// 复制经过模板运算后的图像到源图像
	memcpy(lpDIBBits, lpNewDIBBits1, lWidth * lHeight);

	// 设置Sobel模板6参数
	aTemplate[0] = 0.0;
	aTemplate[1] = 1.0;
	aTemplate[2] = 2.0;
	aTemplate[3] = -1.0;
	aTemplate[4] = 0.0;
	aTemplate[5] = 1.0;
	aTemplate[6] = -2.0;
	aTemplate[7] = -1.0;
	aTemplate[8] = 0.0;

	if (!Template(lpNewDIBBits2, lWidth, lHeight, 
		iTempH, iTempW, iTempMX, iTempMY, aTemplate, fTempC))
	{
		return FALSE;
	}

	//求两幅缓存图像的最大值
	for(j = 0; j <lHeight; j++)
	{
		for(i = 0;i <lWidth-1; i++)
		{
			// 指向缓存图像1倒数第j行,第i个象素的指针			
			lpDst1 = (char *)lpNewDIBBits1 + lWidth * j + i;
			// 指向缓存图像2倒数第j行,第i个象素的指针			
			lpDst2 = (char *)lpNewDIBBits2 + lWidth * j + i;
			
			if(*lpDst2 > *lpDst1)
			{
				*lpDst1 = *lpDst2;
			}
		}
	}

	// 复制经过模板运算后的图像到源图像
	memcpy(lpDIBBits, lpNewDIBBits1, lWidth * l

⌨️ 快捷键说明

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