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

📄 imageoperation.cpp

📁 一些自己做的关于图象处理的程序(如傅立叶变换
💻 CPP
字号:
// ImageOperation.cpp: implementation of the CImageOperation class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "photostar.h"
#include "ImageOperation.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


CImageOperation::CImageOperation(int iKind, int imKind)
{
	m_iKind=iKind;
 	m_imKind=imKind;
}


CImageOperation::~CImageOperation()
{

}

// HistogramBlance.cpp: implementation of the CHistogramBlance class.
//
//////////////////////////////////////////////////////////////////////
/**********************************************************************
* Copyright (c) 2003, Medical Image Processing Lab, Sichuan University 
* All rights reserved.
* 
* 文件名称:Equalize.cpp
* 摘    要:图像代数运算CAlgebra源文件
* 
* 当前版本:1.2 
* 作    者:jian wei zhang	
* 完成日期:2004年10月30日
************************************************************************/


BOOL CImageOperation::OnAlgebraOperation()
{
if (m_pImageObject1 == NULL||m_pImageObject2==NULL)
	{
		return FALSE;
	}
	
	int iWidth1 = m_pImageObject1->GetWidth();
	int iHeight1 = m_pImageObject1->GetHeight();
	int iNumBits1=m_pImageObject1->GetNumBits();
	int iWidth2 = m_pImageObject2->GetWidth();
	int iHeight2 = m_pImageObject2->GetHeight();
	int iNumBits2=m_pImageObject2->GetNumBits();
	
	unsigned char *pOldBuffer1,*pOldBuffer2, *pNewBuffer, *pOldBits1, *pOldBits2,*pNewBits;
	BITMAPFILEHEADER *pOldBFH1,*pOldBFH2, *pNewBFH;
	BITMAPINFOHEADER *pOldBIH1,*pOldBIH2, *pNewBIH;
	RGBQUAD *pOldPalette1,*pOldPalette2, *pNewPalette;
	int nWidthBytes1,nNumColors1;
	int nWidthBytes2,nNumColors2;
    int midcolors,midbytes;
	CImageObject* midpImage;
	int temp;

	/******************************the old image1*****************************/
	pOldBuffer1 = (unsigned char *) m_pImageObject1->GetDIBPointer(&nWidthBytes1, m_pImageObject1->GetNumBits());
	if (pOldBuffer1== NULL) 
	{
		return FALSE;
	}
	
	pOldBFH1 = (BITMAPFILEHEADER *) pOldBuffer1;
	pOldBIH1 = (BITMAPINFOHEADER *) &pOldBuffer1[sizeof(BITMAPFILEHEADER)];
	nNumColors1 = m_pImageObject1->GetNumColors();
	pOldPalette1 = (RGBQUAD *)  &pOldBuffer1[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
	pOldBits1 = (unsigned char *) &pOldBuffer1[sizeof(BITMAPFILEHEADER)
		+sizeof(BITMAPINFOHEADER)+nNumColors1*sizeof(RGBQUAD)];
	/******************************the old image2*****************************/
	pOldBuffer2 = (unsigned char *) m_pImageObject2->GetDIBPointer(&nWidthBytes2, m_pImageObject2->GetNumBits());
	if (pOldBuffer2== NULL) 
	{
		return FALSE;
	}
	
	pOldBFH2 = (BITMAPFILEHEADER *) pOldBuffer2;
	pOldBIH2 = (BITMAPINFOHEADER *) &pOldBuffer2[sizeof(BITMAPFILEHEADER)];
	nNumColors2 = m_pImageObject2->GetNumColors();
	pOldPalette2 = (RGBQUAD *)  &pOldBuffer2[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
	pOldBits2 = (unsigned char *) &pOldBuffer2[sizeof(BITMAPFILEHEADER)
		+sizeof(BITMAPINFOHEADER)+nNumColors2*sizeof(RGBQUAD)];
	
	/*****************************the new image*****************************/
	DWORD dwNewSize;
	HGLOBAL hNewDib;
	
	//Allocate a global memory block for the new image

	if((nWidthBytes2 * (m_pImageObject2->GetHeight()))>=(nWidthBytes1 * (m_pImageObject1->GetHeight())))
	{
		midpImage=m_pImageObject2;
		midcolors=nNumColors2;
		midbytes=nWidthBytes2;
	}
	else
	{
		midpImage=m_pImageObject1;
		midcolors=nNumColors1;
		midbytes=nWidthBytes1;
	}
	dwNewSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) 
			+ midcolors * sizeof(RGBQUAD)
			+ midbytes * (midpImage->GetHeight());
		hNewDib = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwNewSize);
		if (hNewDib == NULL)
		{
			midpImage->m_nLastError = IMAGELIB_MEMORY_ALLOCATION_ERROR;
			::GlobalUnlock(midpImage->GetDib());
			return FALSE;
		}
		
		//Get the pointer to the new memory block
		pNewBuffer = (unsigned char *) ::GlobalLock(hNewDib);
		if (pNewBuffer == NULL)
		{
			::GlobalFree( hNewDib );
			midpImage->m_nLastError = IMAGELIB_MEMORY_LOCK_ERROR;
			::GlobalUnlock(midpImage->GetDib());
			return FALSE;
		}
		
		pNewBFH = (BITMAPFILEHEADER *) pNewBuffer;
		pNewBIH = (BITMAPINFOHEADER *) &pNewBuffer[sizeof(BITMAPFILEHEADER)];
		pNewPalette = (RGBQUAD *) &pNewBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
		pNewBits = (unsigned char *) &pNewBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+midcolors*sizeof(RGBQUAD)];
		
		//Make a copy of the old image
		 memcpy(pNewBuffer,((unsigned char *) midpImage->GetDIBPointer(&midbytes, midpImage->GetNumBits())),dwNewSize);	
	
	
	/*******************************algebra Operation****************************/
    int i,j;
	switch(iNumBits1) 
	{
	case 8:
		switch(iNumBits2)
		{
		case 8:
			int iXcount,iYcount,nWidthBytes;
			if(iHeight1>=iHeight2)
			{
				iYcount=iHeight2;
			}
			else
			{ 
				iYcount=iHeight1;
			}
			if(iWidth1>=iWidth2)
			{
				iXcount=iWidth2;
			}
			else
			{
				iXcount=iWidth1;
			}
			if(nWidthBytes1>=nWidthBytes2)
			{
				nWidthBytes=nWidthBytes1;
			}
			else
			{
				nWidthBytes=nWidthBytes2;
			}
            switch(m_iKind)
			{                                         
            case 1:
                for(i=0;i<iYcount;i++)
				{
					for(j=0;j<iXcount;j++)
					{
 						pNewBits[(iYcount-1-i)*nWidthBytes+j]=(int)(0.5*pOldBits1[(iYcount-1-i)*nWidthBytes1+j]+0.5*pOldBits2[(iYcount-1-i)*nWidthBytes2+j]);
						if(pNewBits[(iYcount-1-i)*nWidthBytes+j]>255)
						{
												
							pNewBits[(iYcount-1-i)*nWidthBytes+j]=255;
						}
			         }
				}
            	break;
            case 2:
				for(i=0;i<iYcount;i++)
				{
					for(j=0;j<iXcount;j++)
					{
						pNewBits[((iYcount)-1-i)*nWidthBytes+j]=(int)(pOldBits1[(iYcount-1-i)*nWidthBytes1+j]-pOldBits2[(iYcount-1-i)*nWidthBytes2+j]);
            	        if(pNewBits[(iYcount-1-i)*nWidthBytes+j]<0)
						{ 
							pNewBits[(iYcount-1-i)*nWidthBytes+j]=0;
						}
					}
				}
				break;
            case 3:
				switch(m_imKind) 
				{
				case 1:
					for(i=0;i<iYcount;i++)
					{
						for(j=0;j<iXcount;j++)
						{
						    temp=(int)(pOldBits1[(iYcount-1-i)*nWidthBytes1+j]*pOldBits2[(iYcount-1-i)*nWidthBytes2+j]);
							if(temp>255)
							{ 
								temp=255;
							}	
							pNewBits[((iYcount)-1-i)*nWidthBytes+j]=temp;
						}
					}
					break;
				case 2:
					for(i=0;i<iYcount;i++)
					{
						for(j=0;j<iXcount;j++)
						{   
							temp=255-pOldBits2[(iYcount-1-i)*nWidthBytes2+j];
						    if(temp==0)
							{ 
							   pNewBits[((iYcount)-1-i)*nWidthBytes+j]=255;
							}
						    temp=(int)(pOldBits1[(iYcount-1-i)*nWidthBytes1+j]*temp);
							if(temp>255)
							{
								temp=255;
							}
							pNewBits[((iYcount)-1-i)*nWidthBytes+j]=temp;
							
							
						}
					}
					break;
				
				}
				break;
            }

			break;
		case 16:
			break;
		case 24:
			break;
		case 32:
			break;
		}

		break;
	case 16 :
		break;
	case 24:
		break;
	case 32:
		break;
	}
	//Free the memory for the old image
	::GlobalUnlock(m_pImageObject1->GetDib());
	::GlobalFree(m_pImageObject1->GetDib());
	::GlobalUnlock(m_pImageObject2->GetDib());
	::GlobalFree(m_pImageObject2->GetDib());
	//Update the DIB in the object pointed by m_pImaeObject
	::GlobalUnlock(hNewDib); 
	midpImage->SetDib(hNewDib);
	return TRUE;


}
// HistogramBlance.cpp: implementation of the CHistogramBlance class.
//
//////////////////////////////////////////////////////////////////////
/**********************************************************************
* Copyright (c) 2003, Medical Image Processing Lab, Sichuan University 
* All rights reserved.
* 
* 文件名称:Equalize.cpp
* 摘    要:图像逻辑运算CLogic源文件
* 
* 当前版本:1.2
* 作    者:jian wei zhang	
* 完成日期:2004年12月1日
************************************************************************/
BOOL CImageOperation::onLogicOperation()
{
if (m_pImageObject1 == NULL||m_pImageObject2==NULL)
	{
		return FALSE;
	}
	
	int iWidth1 = m_pImageObject1->GetWidth();
	int iHeight1 = m_pImageObject1->GetHeight();
	int iNumBits1=m_pImageObject1->GetNumBits();
	int iWidth2 = m_pImageObject2->GetWidth();
	int iHeight2 = m_pImageObject2->GetHeight();
	int iNumBits2=m_pImageObject2->GetNumBits();
	
	unsigned char *pOldBuffer1,*pOldBuffer2, *pNewBuffer, *pOldBits1, *pOldBits2,*pNewBits;
	BITMAPFILEHEADER *pOldBFH1,*pOldBFH2, *pNewBFH;
	BITMAPINFOHEADER *pOldBIH1,*pOldBIH2, *pNewBIH;
	RGBQUAD *pOldPalette1,*pOldPalette2, *pNewPalette;
	int nWidthBytes1,nNumColors1;
	int nWidthBytes2,nNumColors2;
 

	

	/******************************the old image1*****************************/
	pOldBuffer1 = (unsigned char *) m_pImageObject1->GetDIBPointer(&nWidthBytes1, m_pImageObject1->GetNumBits());
	if (pOldBuffer1== NULL) 
	{
		return FALSE;
	}
	
	pOldBFH1 = (BITMAPFILEHEADER *) pOldBuffer1;
	pOldBIH1 = (BITMAPINFOHEADER *) &pOldBuffer1[sizeof(BITMAPFILEHEADER)];
	nNumColors1 = m_pImageObject1->GetNumColors();
	pOldPalette1 = (RGBQUAD *)  &pOldBuffer1[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
	pOldBits1 = (unsigned char *) &pOldBuffer1[sizeof(BITMAPFILEHEADER)
		+sizeof(BITMAPINFOHEADER)+nNumColors1*sizeof(RGBQUAD)];
	/******************************the old image2*****************************/
	pOldBuffer2 = (unsigned char *) m_pImageObject2->GetDIBPointer(&nWidthBytes2, m_pImageObject2->GetNumBits());
	if (pOldBuffer2== NULL) 
	{
		return FALSE;
	}
	
	pOldBFH2 = (BITMAPFILEHEADER *) pOldBuffer2;
	pOldBIH2 = (BITMAPINFOHEADER *) &pOldBuffer2[sizeof(BITMAPFILEHEADER)];
	nNumColors2 = m_pImageObject2->GetNumColors();
	pOldPalette2 = (RGBQUAD *)  &pOldBuffer2[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
	pOldBits2 = (unsigned char *) &pOldBuffer2[sizeof(BITMAPFILEHEADER)
		+sizeof(BITMAPINFOHEADER)+nNumColors2*sizeof(RGBQUAD)];
	
	/*****************************the new image*****************************/
	DWORD dwNewSize;
	HGLOBAL hNewDib;
	
	//Allocate a global memory block for the new image
	dwNewSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) 
			+ nNumColors2* sizeof(RGBQUAD)
			+ nWidthBytes2* (m_pImageObject2->GetHeight());
		hNewDib = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwNewSize);
		if (hNewDib == NULL)
		{
			m_pImageObject2->m_nLastError = IMAGELIB_MEMORY_ALLOCATION_ERROR;
			::GlobalUnlock(m_pImageObject2->GetDib());
			return FALSE;
		}
		
		//Get the pointer to the new memory block
		pNewBuffer = (unsigned char *) ::GlobalLock(hNewDib);
		if (pNewBuffer == NULL)
		{
			::GlobalFree( hNewDib );
			m_pImageObject2->m_nLastError = IMAGELIB_MEMORY_LOCK_ERROR;
			::GlobalUnlock(m_pImageObject2->GetDib());
			return FALSE;
		}
		
		pNewBFH = (BITMAPFILEHEADER *) pNewBuffer;
		pNewBIH = (BITMAPINFOHEADER *) &pNewBuffer[sizeof(BITMAPFILEHEADER)];
		pNewPalette = (RGBQUAD *) &pNewBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
		pNewBits = (unsigned char *) &pNewBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors2*sizeof(RGBQUAD)];
		
		//Make a copy of the old image
		 memcpy(pNewBuffer,pOldBuffer2,dwNewSize);	
	
	
	/*******************************logic Operation****************************/
    int i,j;
	switch(iNumBits1) 
	{
	case 8:
		switch(iNumBits2)
		{
		case 8:
			switch(m_imKind) 
			{
			  case 1:
				  for(i=0;i<iHeight1;i++)
				  {
					  for(j=0;j<iWidth1;j++)
					  {
						  pNewBits[(iHeight1-1-i)*nWidthBytes1+j]=(int)(255-pOldBits2[(iHeight1-1-i)*nWidthBytes1+j]);
						  
					  }
				  }
				 break;
			  case 2:
				  for(i=0;i<iHeight1;i++)
				  {
					  for(j=0;j<iWidth1;j++)
					  {   
						  if(pOldBits2[(iHeight1-1-i)*nWidthBytes1+j]>=pOldBits1[(iHeight1-1-i)*nWidthBytes1+j])
						  pNewBits[(iHeight1-1-i)*nWidthBytes1+j]=pOldBits1[(iHeight1-1-i)*nWidthBytes1+j];
						  else
                          pNewBits[(iHeight1-1-i)*nWidthBytes1+j]=pOldBits2[(iHeight1-1-i)*nWidthBytes1+j];
					  }
				  }
				 break;
			  case 3:
				  for(i=0;i<iHeight1;i++)
				  {
					  for(j=0;j<iWidth1;j++)
					  {   if(pOldBits2[(iHeight1-1-i)*nWidthBytes1+j]>=pOldBits1[(iHeight1-1-i)*nWidthBytes1+j])
					  pNewBits[(iHeight1-1-i)*nWidthBytes1+j]=pOldBits2[(iHeight1-1-i)*nWidthBytes1+j];
					  else
                          pNewBits[(iHeight1-1-i)*nWidthBytes1+j]=pOldBits1[(iHeight1-1-i)*nWidthBytes1+j];
					  }
				  }
				  break;
			  case 4:
				  for(i=0;i<iHeight1;i++)
				  {
					  for(j=0;j<iWidth1;j++)
					  {   /*if(pOldBits2[(iHeight1-1-i)*nWidthBytes1+j]==255)
					  pNewBits[(iHeight1-1-i)*nWidthBytes1+j]=pOldBits1[(iHeight1-1-i)*nWidthBytes1+j];
					  else
                          pNewBits[(iHeight1-1-i)*nWidthBytes1+j]=0;
					  */
					  if(pOldBits2[(iHeight1-1-i)*nWidthBytes1+j]>=pOldBits1[(iHeight1-1-i)*nWidthBytes1+j])
					  {
						  pNewBits[(iHeight1-1-i)*nWidthBytes1+j]=pOldBits2[(iHeight1-1-i)*nWidthBytes1+j];
					  }
					  else
					  {
					  
                          pNewBits[(iHeight1-1-i)*nWidthBytes1+j]=pOldBits1[(iHeight1-1-i)*nWidthBytes1+j];
					  }
						  pNewBits[(iHeight1-1-i)*nWidthBytes1+j]=255-pNewBits[(iHeight1-1-i)*nWidthBytes1+j];
					  }
				  }
				  break;
			}
			break;
		case 16:
			break;
		case 24:
			break;
		case 32:
			break;
		}

		break;
	case 16 :
		break;
	case 24:
		break;
	case 32:
		break;
	}
	//Free the memory for the old image
	::GlobalUnlock(m_pImageObject1->GetDib());
	::GlobalFree(m_pImageObject1->GetDib());
	::GlobalUnlock(m_pImageObject2->GetDib());
	::GlobalFree(m_pImageObject2->GetDib());
	//Update the DIB in the object pointed by m_pImaeObject
	::GlobalUnlock(hNewDib); 
	m_pImageObject2->SetDib(hNewDib);
	return TRUE;
}

⌨️ 快捷键说明

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