CImg CImg::operator | (CImg& gray)
{
	CImg grayRet = *this;
	
	int nHeight = GetHeight();
	int nWidth = GetWidthPixel();
	
	if(nHeight != gray.GetHeight())
	{
		AfxMessageBox("计算或运算的两幅图像必须具有相同的大小!运算失败,返回原图像。");
		return grayRet;
	}

	if(nWidth != gray.GetWidthPixel())
	{
		AfxMessageBox("计算或运算的两幅图像必须具有相同的大小!运算失败,返回原图像。");
		return grayRet;
	}


	// 两幅图像的或
	for(int i=0; i<nHeight; i++)
	{
		for(int j=0; j<nWidth; j++)
		{
			if(gray.GetGray(j, i) == 0)
				grayRet.SetPixel(j, i, RGB(0, 0, 0));
		}
	}

	return grayRet;
}
BOOL CImg::operator == (CImg& gray)
{
	int nHeight = GetHeight();
	int nWidth = GetWidthPixel();

	if(nHeight != gray.GetHeight())
		return false;

	if(nWidth != gray.GetWidthPixel())
		return false;


	for(int i=0; i<nHeight; i++)
	{
		for(int j=0; j<nWidth; j++)
		{
			if( GetGray(j, i) != gray.GetGray(j, i) )
				return false;
		}
	}

	return true;
}
/*******************
CImg CImg::operator - (CImg gray)

功能:图像按位减

参数:
	CImg 对象

返回值:
	CImg: 相减后的 CImg 类图像对象
*******************/
CImg CImg::operator - (CImg &gray)
{
	CImg grayRet = *this; //返回图像

	//取得图像的高和宽
	int nHeight = GetHeight();
	int nWidth = GetWidthPixel();

	int i, j;//循环变量
	
	//不能在CImg类对象中直接进行像素相减,因为相减的结果可能小于0
	vector< vector<int> > GrayMat;//相减后暂存图像的灰度点阵
	vector<int> vecRow(nWidth, 0); //GrayMat中的一行(初始化为0)
	for(i=0; i<nHeight; i++)
	{
		GrayMat.push_back(vecRow);
	}

	//最大、最小灰度和值
	int nMax = -255;
	int nMin = 255; 

	//逐行扫描图像
	for(i=0; i<nHeight; i++)
	{
		for(j=0; j<nWidth; j++)
		{
			//按位相加
			GrayMat[i][j] = GetGray(j, i) - gray.GetGray(j, i);
			
			//统计最大、最小值
			if( GrayMat[i][j] > nMax)
				nMax = GrayMat[i][j];
			if( GrayMat[i][j] < nMin)
				nMin = GrayMat[i][j];
		}// j
	}// i
	
	
	//将GrayMat的取值范围重新归一化到[0, 255]
	int nSpan = nMax - nMin;
	
	for(i=0; i<nHeight; i++)
	{
		for(j=0; j<nWidth; j++)
		{
			BYTE bt;
			if(nSpan > 0)
				bt = (GrayMat[i][j] - nMin)*255/nSpan;
			else if(GrayMat[i][j] <= 255)
				bt = GrayMat[i][j] ;
			else
				bt = 255;
				
			grayRet.SetPixel(j, i, RGB(bt, bt, bt));			
			

		}// for j
	}// for i
		
	
	
	return grayRet;
}
Esempio n. 4
0
CImg* CDimageView::Canny(CImg* pImg)
{
	// 各方向梯度值

	// 使用Prewitt模板计算各个方向上的梯度值
	CImg* imgGH=PrewittEdge(pImg,1);
	CImg* imgGV=PrewittEdge(pImg,2);
	CImg* imgGCW=PrewittEdge(pImg,3);
	CImg* imgGCCW=PrewittEdge(pImg,4);
	CImg* imgGratitude = new CImg(*pImg);
	imgGratitude->InitPixels(0);

	int width=pImg->GetWidthPixel();
	int height=pImg->GetHeight();
	// 最大梯度方向
	BYTE * pbDirection = new BYTE [height * width];

	memset(pbDirection, 0,height * width * sizeof(BYTE));

	// 寻找每点的最大梯度方向并写入对应的最大梯度值

	for (int i=0; i<height; i++)
	{
		for (int j=0; j<width; j++)
		{
			BYTE gray = 0;

			if (imgGH->GetGray(j, i) > gray)
			{
				gray = imgGH->GetGray(j, i);
				pbDirection[i * width + j] = 1;
				imgGratitude->SetPixel(j, i, RGB(gray, gray, gray));
			}

			if (imgGV->GetGray(j, i) > gray)
			{
				gray = imgGV->GetGray(j, i);
				pbDirection[i * width + j] = 2;
				imgGratitude->SetPixel(j, i, RGB(gray, gray, gray));
			}

			if (imgGCW->GetGray(j, i) > gray)
			{
				gray = imgGCW->GetGray(j, i);
				pbDirection[i * width + j] = 3;
				imgGratitude->SetPixel(j, i, RGB(gray, gray, gray));
			}

			if (imgGCCW->GetGray(j, i) > gray)
			{
				gray = imgGCCW->GetGray(j, i);
				pbDirection[i * width + j] = 4;
				imgGratitude->SetPixel(j, i, RGB(gray, gray, gray));
			}
		}
	}

	// 阈值化时重用前面的对象
	CImg *pImgThreL = imgGH, *pImgThreH = imgGV;

	// 检查阈值参数,如未给出阈值则计算以取得最佳阈值
	int bThreH;
	int bThreL;

	bThreH = 1.2 * imgGratitude->DetectThreshold(100);
	bThreL = 0.4 * bThreH;
		

	// 将最大梯度图像按高低值分别进行阈值化
	imgGratitude->Threshold(pImgThreL, bThreL);
	imgGratitude->Threshold(pImgThreH, bThreH);

	// 初始化目标图像
	CImg* mImg = new CImg(*pImg);
	mImg->InitPixels(0);
	
	// 根据低阈值图像在高阈值图像上进行边界修补
	for (int i=1; i<height-1; i++)
	{
		for (int j=1; j<width-1; j++)
		{
			if (pImgThreH->GetGray(j, i))
			{
				// 高阈值图像上发现点直接确定
				mImg->SetPixel(j, i, RGB(255, 255, 255));
				
				// 搜索梯度最大方向上的邻域
				switch ( pbDirection[i * width + j] ) {
					case 1:	// 水平方向
						if (pImgThreL->GetGray(j+1, i))
						{
							pImgThreH->SetPixel(j+1, i, RGB(255, 255, 255));
						}
						if (pImgThreL->GetGray(j-1, i))
						{
							pImgThreH->SetPixel(j-1, i, RGB(255, 255, 255));
						}
						break;

					case 2:	// 垂直方向
						if (pImgThreL->GetGray(j, i+1))
						{
							pImgThreH->SetPixel(j, i+1, RGB(255, 255, 255));
						}
						if (pImgThreL->GetGray(j, i-1))
						{
							pImgThreH->SetPixel(j, i-1, RGB(255, 255, 255));
						}
						break;

					case 3:	// 45度方向
						if (pImgThreL->GetGray(j+1, i-1))
						{
							pImgThreH->SetPixel(j+1, i-1, RGB(255, 255, 255));
						}
						if (pImgThreL->GetGray(j-1, i+1))
						{
							pImgThreH->SetPixel(j-1, i+1, RGB(255, 255, 255));
						}
						break;

					case 4:	// 135度方向
						if (pImgThreL->GetGray(j+1, i+1))
						{
							pImgThreH->SetPixel(j+1, i+1, RGB(255, 255, 255));
						}
						if (pImgThreL->GetGray(j-1, i-1))
						{
							pImgThreH->SetPixel(j-1, i-1, RGB(255, 255, 255));
						}
						break;
				}
			}//if
		}//for j
	}//for i



	delete pbDirection;

	return mImg;
}