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

📄 myimagedbdoc.cpp

📁 这是一个分水岭程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	LONG pos = 0;

	for (x=0; x<imageWidth; x++)
	{
		for (INT y=0; y<imageHeight; y++)
		{
			pos = ( y*imageWidth + x ) * 3;//修改该位置的颜色分量值;
			
			INT nearwidth, nearheight;//邻域宽度;
			nearwidth = 0;
			nearheight = 0;

			BYTE* neararr = GetNearPixels(x, y, imageData,
				imageWidth, imageHeight, neiscale, 
				nearwidth, nearheight);
			//计算邻域均值;
			LONG total1, total2, total3;
			total1 = total2 = total3 = 0;//存放累加和;
			for (INT nearx=0; nearx<nearwidth; nearx++)
			{
				for (INT neary=0; neary<nearheight; neary++)
				{
					LONG nearpos = (neary*nearheight + nearx) * 3;
					total1 += neararr[nearpos];
					total2 += neararr[nearpos+1];
     				total3 += neararr[nearpos+2];					
				}
			}

			delete [] neararr;//删除邻域数组;
			neararr = NULL;

			//用均值更新临时数组中的元素;
			processarr[pos] = 255 - abs( processarr[pos] - total1 / (nearwidth*nearheight) ) * 10;
			processarr[pos+1] = 255 - abs( processarr[pos+1] - total2 / (nearwidth*nearheight) ) * 10;
			processarr[pos+2] = 255 - abs( processarr[pos+2] - total3 / (nearwidth*nearheight) ) * 10;

		}
	}

	//用临时数组替换原数组;
	for (x=0; x<imageWidth; x++)
	{
		for (INT y=0; y<imageHeight; y++)
		{
			LONG pos = (y*imageWidth + x) * 3;
			imageData[pos] = processarr[pos];
			imageData[pos+1] = processarr[pos+1];
			imageData[pos+2] = processarr[pos+2];
		}
	}

	RefreshImageObject();
	
	delete [] processarr;//删除临时数组;
	processarr = NULL;

	CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();
	EndWaitCursor();
	pFrame->pImageView->Invalidate(FALSE);	
}

void CMyImageDBDoc::OnMinmaxTex() 
{
	BeginWaitCursor();

	BYTE* processarr = new BYTE[imageWidth*imageHeight*3];
	for (INT x=0; x<imageWidth; x++)
	{
		for (INT y=0; y<imageHeight; y++)
		{
			LONG pos = (y*imageWidth + x) * 3;
			processarr[pos] = imageData[pos];
			processarr[pos+1] = imageData[pos+1];
			processarr[pos+2] = imageData[pos+2];
		}
	}

	//以下得到邻域内的最大最小值,用它们的差值代替原像素值;
	INT neiscale = 1;//邻域范围;
	LONG pos = 0;

	for (x=0; x<imageWidth; x++)
	{
		for (INT y=0; y<imageHeight; y++)
		{
			pos = ( y*imageWidth + x ) * 3;//修改该位置的颜色分量值;
			
			INT nearwidth, nearheight;//邻域宽度;
			nearwidth = 0;
			nearheight = 0;

			BYTE* neararr = GetNearPixels(x, y, imageData,
				imageWidth, imageHeight, neiscale, 
				nearwidth, nearheight);
			//计算邻域均值;
			INT tempmin1, tempmin2, tempmin3;
			INT tempmax1, tempmax2, tempmax3;
			tempmin1 = tempmin2 = tempmin3 = 255;
			tempmax1 = tempmax2 = tempmax3 = 0;

			for (INT nearx=0; nearx<nearwidth; nearx++)
			{
				for (INT neary=0; neary<nearheight; neary++)
				{
					LONG nearpos = (neary*nearheight + nearx) * 3;
					if ( neararr[nearpos]>tempmax1 )
					{
						tempmax1 = neararr[nearpos];
					}
					if ( neararr[nearpos]<tempmin1 )
					{
						tempmin1 = neararr[nearpos];
					}

					if ( neararr[nearpos+1]>tempmax2 )
					{
						tempmax2 = neararr[nearpos+1];
					}
					if ( neararr[nearpos+1]<tempmin2 )
					{
						tempmin2 = neararr[nearpos+1];
					}				

					if ( neararr[nearpos+2]>tempmax3 )
					{
						tempmax3 = neararr[nearpos+2];
					}
					if ( neararr[nearpos+2]<tempmin3 )
					{
						tempmin3 = neararr[nearpos+2];
					}
				}
			}
			delete [] neararr;//删除邻域数组;
			neararr = NULL;

			//用差值更新临时数组中的元素;
			processarr[pos] = 255 - ( tempmax1 - tempmin1 );
			processarr[pos+1] = 255 - ( tempmax2 - tempmin2 );
			processarr[pos+2] = 255 - ( tempmax3 - tempmin3 );

		}
	}

	//用临时数组替换原数组;
	for (x=0; x<imageWidth; x++)
	{
		for (INT y=0; y<imageHeight; y++)
		{
			LONG pos = (y*imageWidth + x) * 3;

			imageData[pos] = processarr[pos];
			imageData[pos+1] = processarr[pos+1];
			imageData[pos+2] = processarr[pos+2];
		}
	}

	RefreshImageObject();
	
	delete [] processarr;//删除临时数组;
	processarr = NULL;

	CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();
	EndWaitCursor();
	pFrame->pImageView->Invalidate(FALSE);		
}

void CMyImageDBDoc::OnInverseImage() 
{
	BeginWaitCursor();

	BYTE* processarr = new BYTE[imageWidth*imageHeight*3];

	//取反图像;
	for (INT x=0; x<imageWidth; x++)
	{
		for (INT y=0; y<imageHeight; y++)
		{
			LONG pos = (y*imageWidth + x) * 3;
			processarr[pos] = 255 - imageData[pos];
			processarr[pos+1] = 255 - imageData[pos+1];
			processarr[pos+2] = 255 - imageData[pos+2];
		}
	}

	//用临时数组替换原数组;
	for (x=0; x<imageWidth; x++)
	{
		for (INT y=0; y<imageHeight; y++)
		{
			LONG pos = (y*imageWidth + x) * 3;
			imageData[pos] = processarr[pos];
			imageData[pos+1] = processarr[pos+1];
			imageData[pos+2] = processarr[pos+2];
		}
	}

	RefreshImageObject();
	
	delete [] processarr;//删除临时数组;
	processarr = NULL;

	CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();
	EndWaitCursor();
	pFrame->pImageView->Invalidate(FALSE);		
}

void CMyImageDBDoc::SetClassVectors()
//设置用于分割的类中心向量;
{
	CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();

	//首先得到分割参数面板中选择的分割类;
    INT selcounts, cursel;
	selcounts = cursel = -1;
	CString* selclassnames = pFrame->mySegmentPara->
		GetSelectClass(selcounts, cursel);
	vtCount = selcounts;//所选用于分割的类数;
	if (selcounts<=0)
	{
		CString tempstr;
		tempstr.Format("请选择用于分割的点类!");
		AfxMessageBox(tempstr,NULL,MB_OK);
		return;
	}

	//其次从分割类表中读入各类信息并存入分割信息数组;
	if (myClassVector!=NULL)
	{
		delete [] myClassVector;
		myClassVector = NULL;
	}
	myClassVector = new MyFeatureVector[selcounts];//存放用于分割的各个类;
	//INT rdc = pFrame->seginfoRs->GetRecordCount();
	INT selpos = 0;
	pFrame->seginfoRs->MoveFirst();
	while(!pFrame->seginfoRs->IsEOF())
	{
		CString tempstr = "";
		BOOL tempb = pFrame->seginfoRs->GetFieldValue("regionclass", tempstr);
		if (isStrInArr(tempstr, selclassnames, selcounts))
		{
			myClassVector[selpos].classname = tempstr;
			pFrame->seginfoRs->GetFieldValue("colorl", myClassVector[selpos].colorl);
			pFrame->seginfoRs->GetFieldValue("colorlw", myClassVector[selpos].colorlw);
			pFrame->seginfoRs->GetFieldValue("coloru", myClassVector[selpos].coloru);
			pFrame->seginfoRs->GetFieldValue("coloruw", myClassVector[selpos].coloruw);
			pFrame->seginfoRs->GetFieldValue("colorv", myClassVector[selpos].colorv);
			pFrame->seginfoRs->GetFieldValue("colorvw", myClassVector[selpos].colorvw);		

			pFrame->seginfoRs->GetFieldValue("minmaxtexl", myClassVector[selpos].minmaxtexl);
			pFrame->seginfoRs->GetFieldValue("minmaxtexlw", myClassVector[selpos].minmaxtexlw);
			pFrame->seginfoRs->GetFieldValue("minmaxtexu", myClassVector[selpos].minmaxtexu);
			pFrame->seginfoRs->GetFieldValue("minmaxtexuw", myClassVector[selpos].minmaxtexuw);
			pFrame->seginfoRs->GetFieldValue("minmaxtexv", myClassVector[selpos].minmaxtexv);
			pFrame->seginfoRs->GetFieldValue("minmaxtexvw", myClassVector[selpos].minmaxtexvw);		

			selpos++;
		}
		pFrame->seginfoRs->MoveNext();
	}
}

BOOL CMyImageDBDoc::isStrInArr(CString str, CString* arr, LONG arrnumber)
//检查指定字符串是否存在于数组中;
{
	BOOL istrue = FALSE;
	for (INT i=0; i<arrnumber; i++)
	{
		if (str == arr[i])
		{
			istrue = TRUE;
			break;
		}
	}

	return istrue;
}

void CMyImageDBDoc::OnImageSegment() 
{
	SetClassVectors();//首先得到用于分割的类信息;
	SegmentByPt();//执行分割;
}

INT CMyImageDBDoc::DeterPtClass(MyLUV luv, MyLUV minmaxtex, MyFeatureVector* myClassVector, INT numberInArr)
//决定每个点所属的类;
{
    FLOAT mindis = -1;
	INT   outClass = -1;
	for (INT i=0; i<numberInArr; i++)
	{
		FLOAT tempf = GetDistance(luv, minmaxtex, myClassVector[i]);
		if ( tempf > mindis)
		{
			//距离值越大(越接近1)越好;
			mindis = tempf;
			outClass = i;
		}
	}
	return outClass;
}

FLOAT CMyImageDBDoc::GetDistance(MyLUV luv, MyLUV minmaxtex, MyFeatureVector vect)
//LUV和特征向量距离;
{
    DOUBLE d1 = myMath.Gaussian(luv.l, vect.colorl, vect.colorlw);
	DOUBLE d2 = myMath.Gaussian(luv.u, vect.coloru, vect.coloruw);
	DOUBLE d3 = myMath.Gaussian(luv.v, vect.colorv, vect.colorvw);

    DOUBLE d4 = myMath.Gaussian(minmaxtex.l, vect.minmaxtexl, vect.minmaxtexlw);
	DOUBLE d5 = myMath.Gaussian(minmaxtex.u, vect.minmaxtexu, vect.minmaxtexuw);
	DOUBLE d6 = myMath.Gaussian(minmaxtex.v, vect.minmaxtexv, vect.minmaxtexvw);

	FLOAT totalerr = (FLOAT) (vect.colorlw + vect.coloruw 
		+ vect.colorvw + vect.minmaxtexlw + vect.minmaxtexuw 
		+ vect.minmaxtexvw);

	FLOAT wl, wu, wv, wtexl, wtexu, wtexv;
	wl = (FLOAT) (1 - vect.colorlw/totalerr);
	wu = (FLOAT) (1 - vect.coloruw/totalerr);
	wv = (FLOAT) (1 - vect.colorvw/totalerr);
	wtexl = (FLOAT) (1 - vect.minmaxtexlw/totalerr);
	wtexu = (FLOAT) (1 - vect.minmaxtexuw/totalerr);
	wtexv = (FLOAT) (1 - vect.minmaxtexvw/totalerr);

	//返回方差加权的期望距离,此处也许可以使用学习算法学习权重;
	FLOAT toreturn = (FLOAT) ( d1*wl + d2*wu + d3*wv
		+ d4*wtexl + d5*wtexu + d6*wtexv);
	return toreturn;
}

void CMyImageDBDoc::OnGaborTransform() 
{
	BeginWaitCursor();

	FLOAT* gchannel = new FLOAT[imageWidth*imageHeight];
	FLOAT* resultarr = new FLOAT[imageWidth*imageHeight];
	//得到亮度通道;
	for (INT y=0; y<imageHeight; y++)
	{
		for (INT x=0; x<imageWidth; x++)
		{
			INT pos = y*imageWidth + x;
			gchannel[pos] = (FLOAT) imageData[pos*3+1];
		}
	}

	CMyGabor myGabor;
	myGabor.GetImageGaborCoeff(gchannel, imageWidth, imageHeight
		, 4, 3, &resultarr);

	for (INT x=0; x<imageWidth; x++)
	{
		for (INT y=0; y<imageHeight; y++)
		{
			LONG pos = (y*imageWidth + x);
			imageData[pos*3] = (BYTE) (resultarr[pos]);
			imageData[pos*3+1] = (BYTE) (resultarr[pos]);
			imageData[pos*3+2] = (BYTE) (resultarr[pos]);
		}
	}

	RefreshImageObject();

	delete [] resultarr;
	resultarr = NULL;
	delete [] gchannel;
	gchannel = NULL;
	
	CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();
	EndWaitCursor();
	pFrame->pImageView->Invalidate(FALSE);	
}

void CMyImageDBDoc::OnLightUp()
//增加当前图像亮度; 
{
	BeginWaitCursor();

	BYTE* processarr = new BYTE[imageWidth*imageHeight*3];
	for (INT y=0; y<imageHeight; y++)
	{
		for (INT x=0; x<imageWidth; x++)	
		{
			LONG pos = (y*imageWidth + x) * 3;
			processarr[pos] = imageData[pos] * 2;
			processarr[pos+1] = imageData[pos+1] * 2;
			processarr[pos+2] = imageData[pos+2] * 2;
		}
	}

	//用临时数组替换原数组;
	for (y=0; y<imageHeight; y++)
	{
		for (INT x=0; x<imageWidth; x++)	
		{
			LONG pos = (y*imageWidth + x) * 3;
			imageData[pos] = processarr[pos];
			imageData[pos+1] = processarr[pos+1];
			imageData[pos+2] = processarr[pos+2];
		}
	}

	RefreshImageObject();

	delete [] processarr;//删除临时数组;
	processarr = NULL;

	CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();
	EndWaitCursor();
	pFrame->pImageView->Invalidate(FALSE);	
}

void CMyImageDBDoc::OnDirectionHis() 
{
	BeginWaitCursor();

	//存放各点邻域信息;
	INT* tempdirect = new INT[imageWidth*imageHeight];
	FLOAT* tempitensity = new FLOAT[imageWidth*imageHeight];

	for (INT y=0; y<imageHeight; y++)
	{
		for (INT x=0; x<imageWidth; x++)
		{
			INT temppos = y*imageWidth + x;
			BYTE* neiarr=NULL;
			INT tempnr = NEIRADIUS;
			GetNearPixelsGreenExt(x, y, imageData
				, imageWidth, imageHeight, tempnr
				, &neiarr);//计算邻域
			INT tempi = 2*tempnr + 1;
			myTexture.GetDirection(neiarr, tempi
				, tempi, tempdirect[temppos]
				, tempitensity[temppos]);
			
			delete [] neiarr; neiarr = NULL;
		}
	}

	//用临时数组替换原数组;
	for (y=0; y<imageHeight; y++)
	{
		for (INT x=0; x<imageWidth; x++)
		{
			LONG pos = (y*imageWidth + x);
			if (tempitensity[pos]<0)
			{
				//无显著方向;
				imageData[pos*3] = 255;//方向0用蓝色表示;
				imageData[pos*3+1] = 255;
				imageData[pos*3+2] = 255;
				continue;
			}
			BYTE tempval = (BYTE)(tempitensity[pos]*255*6);
			switch(tempdirect[pos]) 
			{
			case 0:
				imageData[pos*3] = tempval;//方向0用蓝色表示;
				imageData[pos*3+1] = 0;
				imageData[pos*3+2] = 0;
				break;
			case 1:
				imageData[pos*3] = 0;//方向1用绿色表示;
				imageData[pos*3+1] = tempval;
				imageData[pos*3+2] = 0;
				break;
			case 2:
				imageData[pos*3] = 0;//方向2用红色表示;
				imageData[pos*3+1] = 0;
				imageData[pos*3+2] = tempval;
				break;
			case 3:
				imageData[pos*3] = tempval;//方向3用色表示;
				imageData[pos*3+1] = tempval;
				imageData[pos*3+2] = 0;
				break;
			default:
				{
					imageData[pos*3] = 0;//否则用黑色表示
					imageData[pos*3+1] = 0;

⌨️ 快捷键说明

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