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

📄 myimagedbdoc.cpp

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

	//得到邻域最大最小值;
	FLOAT tempmin1, tempmin2, tempmin3;
	FLOAT tempmax1, tempmax2, tempmax3;
	tempmin1 = tempmin2 = tempmin3 = 1000;
	tempmax1 = tempmax2 = tempmax3 = -1000;
	
	for (INT nearx=0; nearx<nearwidth; nearx++)
	{
		for (INT neary=0; neary<nearheight; neary++)
		{
			LONG nearpos = (neary*nearheight + nearx);
			if ( neararr[nearpos].l>tempmax1 )
			{
				tempmax1 = neararr[nearpos].l;
			}
			if ( neararr[nearpos].l<tempmin1 )
			{
				tempmin1 = neararr[nearpos].l;
			}
			
			if ( neararr[nearpos].u>tempmax2 )
			{
				tempmax2 = neararr[nearpos].u;
			}
			if ( neararr[nearpos].u<tempmin2 )
			{
				tempmin2 = neararr[nearpos].u;
			}				
			
			if ( neararr[nearpos].v>tempmax3 )
			{
				tempmax3 = neararr[nearpos].v;
			}
			if ( neararr[nearpos].v<tempmin3 )
			{
				tempmin3 = neararr[nearpos].v;
			}
		}
	}
	delete [] neararr;//删除邻域数组;
	neararr = NULL;
	
	//用差值更新临时数组中的元素;
	outluv.l = ( tempmax1 - tempmin1 );
	outluv.u = ( tempmax2 - tempmin2 );
	outluv.v = ( tempmax3 - tempmin3 );

	return outluv;
}

MyLUV* CMyImageDBDoc::GetNearPixelsLUV(INT xPos, INT yPos
	  , MyLUV* inLUVs, INT picWidth, INT picHeight
	  , INT inScale, INT& outWidth, INT& outHeight)
//得到LUV邻域;
{
	//首先计算可能的邻域点数;
	INT templeft = xPos - inScale;
	INT tempright = xPos + inScale;
	INT left, right, up, down;//邻域的左右上下边界;
	if (templeft>0 && tempright<picWidth)
	{
		left = inScale;
		right = inScale;
	}else
	{
		if (templeft<=0)
		{
			left = xPos;
		}else
		{
			left = inScale;
		}
		if (tempright>=picWidth)
		{
			right = picWidth - xPos - 1;
		}else
		{
			right = inScale;
		}
	}

	INT temptop = yPos - inScale;
	INT tempbottom = yPos + inScale;
	if ( temptop>0 && tempbottom<picHeight )
	{
		up = inScale;
		down = inScale;
	}else
	{
		if (temptop<=0)
		{
			up = yPos;
		}else
		{
			up = inScale;
		}
		if (tempbottom>=picHeight)
		{
			down = picHeight - yPos - 1;
		}else
		{
			down = inScale;
		}
	}

	outWidth = left + right + 1;
	outHeight = up + down + 1;

	LONG outlen = outWidth * outHeight;
	MyLUV* outarr = new MyLUV[outlen];

	LONG pos = 0;
	for (INT x=0; x<outWidth; x++)
	{
		for (INT y=0; y<outHeight; y++)
		{
			pos = ( y*outWidth + x );
			//以下求输出的第x,y个元素在原数组中的X和Y位置;
			INT originx = xPos - left + x;
			INT originy = yPos - up + y;
			//在输入的图像数据中的位置;
			LONG inpos = (originy * picWidth + originx); 
			outarr[pos].l = inLUVs[inpos].l;
			outarr[pos].u = inLUVs[inpos].u;
			outarr[pos].v = inLUVs[inpos].v;
		}
	}
	
	return outarr;
}

void CMyImageDBDoc::GetNearPixelsGreenExt(INT xPos, INT yPos
	    , BYTE* inPixels, INT picWidth, INT picHeight
	    , INT radius, BYTE** outArr)
//得到邻域像素值(正方形,G通道),输入位置从0开始计数, 边缘处对称延拓;
{
	INT matrixwidth = (radius*2+1);
	BYTE* temparr = new BYTE[matrixwidth*matrixwidth];//包括指定点自身;

	LONG pos = 0;
	INT rposx, rposy;
	rposx = rposy = 0;//在图像中的位置;

	for (INT y=-radius; y<=radius; y++)
	{
		rposy = yPos+y;
		if (rposy<0)
		{
			rposy = -rposy;
		}else if (rposy>=picHeight)
		{
			rposy = picHeight - (rposy-picHeight);
		}
		
		for (INT x=-radius; x<=radius; x++)
		{
			rposx = xPos+x;
			if (rposx<0)
			{
				rposx = -rposx;
			}else if (rposx>=picWidth)
			{
				rposx = picWidth - (rposx-picWidth);
			}

			//在输入的图像数据中的位置;
			LONG inpos = ( rposy*imageWidth + rposx ) * 3 + 1;//RGB三色值;
			//在输出数组中的位置;
			LONG pos = ( (y+radius)*matrixwidth + (x+radius) );
			temparr[pos] = ( inPixels[inpos] );
		}
	}

	*outArr = temparr;
}


void CMyImageDBDoc::GetNearPixelsExt(INT xPos, INT yPos
	, BYTE* inPixels, INT picWidth, INT picHeight
	, INT radius, BYTE** outArr)
//得到邻域像素值(正方形),输入位置从0开始计数, 边缘处延拓;
{
	INT matrixwidth = (radius*2+1);
	BYTE* temparr = new BYTE[matrixwidth*matrixwidth*3];//包括指定点自身;

	LONG pos = 0;
	INT rposx, rposy;
	rposx = rposy = 0;//在图像中的位置;

	for (INT y=-radius; y<=radius; y++)
	{
		rposy = yPos+y;
		if (rposy<0)
		{
			rposy = -rposy;
		}else if (rposy>=picHeight)
		{
			rposy = picHeight - (rposy-picHeight);
		}
		
		for (INT x=-radius; x<=radius; x++)
		{
			rposx = xPos+x;
			if (rposx<0)
			{
				rposx = -rposx;
			}else if (rposx>=picWidth)
			{
				rposx = picWidth - (rposx-picWidth);
			}

			//在输入的图像数据中的位置;
			LONG inpos = ( rposy*imageWidth + rposx ) * 3;//RGB三色值;
			//在输出数组中的位置;
			LONG pos = ( (y+radius)*matrixwidth + (x+radius) ) * 3;
			temparr[pos] = inPixels[inpos];
			temparr[pos+1] = inPixels[inpos+1];
			temparr[pos+2] = inPixels[inpos+2];
		}
	}

	*outArr = temparr;
}


BYTE* CMyImageDBDoc::GetNearPixels(INT xPos, INT yPos, 
      BYTE* inPixels, INT picWidth, INT picHeight, INT inScale, 
	  INT& outWidth, INT& outHeight)
//得到邻域像素值, 输入位置从0开始计数;
{
	//首先计算可能的邻域点数;
	INT templeft = xPos - inScale;
	INT tempright = xPos + inScale;
	INT left, right, up, down;//邻域的左右上下边界;
	if (templeft>0 && tempright<picWidth)
	{
		//outWidth = inScale * 2 + 1;//加1则包含了像素本身;
		left = inScale;
		right = inScale;
	}else
	{
		if (templeft<=0)
		{
			//outWidth += xPos;//以左全在邻域内;
			left = xPos;
		}else
		{
			left = inScale;
		}
		if (tempright>=picWidth)
		{
			//outWidth += picWidth - xPos - 1;//以右全在邻域内;
			right = picWidth - xPos - 1;
		}else
		{
			right = inScale;
		}

/*
		if (outWidth>picWidth)
		{
			outWidth = picWidth;//邻域宽度与图像宽相等;
		}
*/
	}

	INT temptop = yPos - inScale;
	INT tempbottom = yPos + inScale;
	if ( temptop>0 && tempbottom<picHeight )
	{
		//outWidth = inScale * 2 + 1;//加1则包含了像素本身;
		up = inScale;
		down = inScale;
	}else
	{
		if (temptop<=0)
		{
			up = yPos;
		}else
		{
			up = inScale;
		}
		if (tempbottom>=picHeight)
		{
			down = picHeight - yPos - 1;
		}else
		{
			down = inScale;
		}
/*
		if (outWidth>picWidth)
		{
			outWidth = picWidth;//邻域宽度与图像宽相等;
		}
*/
	}

	outWidth = left + right + 1;
	outHeight = up + down + 1;

	LONG outlen = outWidth * outHeight;
	BYTE* outarr = new BYTE[outlen*3];

	LONG pos = 0;
	for (INT x=0; x<outWidth; x++)
	{
		for (INT y=0; y<outHeight; y++)
		{
			pos = ( y*outWidth + x ) * 3;//RGB三色值;
			//以下求输出的第x,y个元素在原数组中的X和Y位置;
			INT originx = xPos - left + x;
			INT originy = yPos - up + y;
			//在输入的图像数据中的位置;
			LONG inpos = (originy * picWidth + originx) * 3; 
			outarr[pos] = inPixels[inpos];
			outarr[pos+1] = inPixels[inpos+1];
			outarr[pos+2] = inPixels[inpos+2];
		}
	}
	
	return outarr;
}

void CMyImageDBDoc::OnSquareFuzzy() 
{
	SquareFuzzy();	
}

void CMyImageDBDoc::OnFileSaveAs() 
{
	char szFileFilter[] = "位图文件 Files(*.BMP)|*.BMP|";
	
	CMDIFrameWnd *pFrame = 
		(CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
	
    CFileDialog FileDialogBox(
		FALSE,
		NULL,
		"*.BMP",
		OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
		szFileFilter,
		pFrame);
	
	FileDialogBox.m_ofn.lpstrTitle = "另存为";
	
	if( FileDialogBox.DoModal()==IDOK )
	{
        CString bmpname = FileDialogBox.GetPathName();
		myImageObject->SaveToFile(bmpname);
	}
}

void CMyImageDBDoc::OnHis1d2Db() 
{
	//首先得到数据库及图像的相关信息;
	CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();
	if (!pFrame->isDbOK)
	{
		CString tempstr = "先连接数据库!!,:)";
		AfxMessageBox(tempstr);
		return;
	}
	CString tablename = pFrame->tableName;

	CString imagename;
	if ( imageName != "" )
	{
		imagename = imageName;
	}else
	{
		imagename = pFrame->selName;
	}

	LONG width = myImageObject->GetWidth();
	LONG height = myImageObject->GetHeight();
	dataLen = width*height*3;	

	//计算1维直方图特征;
	D1ColorBin* myhisd1 = NULL;
	INT mybinnumber = -1;
	myhisd1 = myColorFeature.CalcuHisD1(imageData, imageWidth, 
		imageHeight);

	//将计算得到的直方图入库;
	CString sqlquery;
	sqlquery.Format("select * from %s where name=\'%s\'",
		tablename, imagename);
	myImagedataRs = new CADORecordset( &(pFrame->myAdoDb) );
	if (!myImagedataRs->Open(sqlquery, CADORecordset::openUnknown))
	{
		//未正常连接指定表;
		CString tempstr;
		tempstr.Format("打不开图像数据表!!!");
		AfxMessageBox(tempstr,NULL,MB_OK);			
		return;
	}else
	{
		if (myImagedataRs->GetRecordCount() <= 0)
		{
			CString tempstr = "表中找不到对应的图像记录!";
			AfxMessageBox(tempstr);
			return;
		}else
		{
			myImagedataRs->Edit();
			myImagedataRs->MoveFirst();
			BOOL tempbool = FALSE;
			//直方图结构的大小;
			LONG hislen = sizeof(D1ColorBinUnit) * myhisd1->bincount;
			tempbool = myImagedataRs->SetFieldValue("his1dlen", myhisd1->bincount);
			tempbool = myImagedataRs->AppendChunk("his1d", myhisd1->binunits, hislen);
			tempbool = myImagedataRs->Update();
		}
		myImagedataRs->Close();		
	}

	//删除直方图;
	delete [] myhisd1->binunits;
	delete myhisd1;
}

D1ColorBin* CMyImageDBDoc::GetHisd1FromDB(CString imageid)
{
	//首先得到数据库及图像的相关信息;
	CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();
	if (!pFrame->isDbOK)
	{
		CString tempstr = "先连接数据库!!,:)";
		AfxMessageBox(tempstr);
		return NULL;
	}
	CString tablename = pFrame->tableName;
	//1维直方图特征;
	D1ColorBin* myhisd1 = NULL;

	//读入库内直方图;
	CString sqlquery;
	sqlquery.Format("select * from %s where id=%s",
		tablename, imageid);
	myImagedataRs = new CADORecordset( &(pFrame->myAdoDb) );
	if (!myImagedataRs->Open(sqlquery, CADORecordset::openUnknown))
	{
		//未正常连接指定表;
		CString tempstr;
		tempstr.Format("打不开图像数据表!!!");
		AfxMessageBox(tempstr,NULL,MB_OK);			
		return NULL;
	}else
	{
		if (myImagedataRs->GetRecordCount() <= 0)
		{
			CString tempstr = "表中找不到对应的图像记录!";
			AfxMessageBox(tempstr);
			return NULL;
		}else
		{			
			myImagedataRs->MoveFirst();
			LONG len = -1;
			myhisd1 = new D1ColorBin;
			BOOL tempbool = myImagedataRs->GetFieldValue("his1dlen", myhisd1->bincount);
			myhisd1->binunits = new D1ColorBinUnit[myhisd1->bincount];
			tempbool = myImagedataRs->GetChunk("his1d", myhisd1->binunits);
		}
		myImagedataRs->Close();		
	}	
	return myhisd1;
}


void CMyImageDBDoc::GetHisd1FromDB(INT minid, INT maxid, D1ColorBin** outhisarr, INT& outnumber)
//读入此id范围内图像的直方图
{
	//首先得到数据库及图像的相关信息;
	CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();
	if (!pFrame->isDbOK)
	{
		CString tempstr = "先连接数据库!!,:)";
		AfxMessageBox(tempstr);
		return;
	}
	CString tablename = pFrame->tableName;
	

⌨️ 快捷键说明

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