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

📄 imageprocessingview.cpp

📁 matlab版图像数字处理这本书的配套代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// ImageProcessingView.cpp : implementation of the CImageProcessingView class
//

#include "stdafx.h"
#include "ImageProcessing.h"

#include "ImageProcessingDoc.h"
#include "ImageProcessingView.h"
#include "mainfrm.h"
#include "ZoomShrinkDlg.h"
#include "interpolation.h"
#include "CombiningSpatialEnhancement.h"
#include "Enhancement.h"
#include<math.h>
#include <complex>
using namespace std;
#include "FFT.h"
#include "Radius.h"
#include "ZFTDialog.h"
#include "MorphologicalProcessing.h"


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

/////////////////////////////////////////////////////////////////////////////
// CImageProcessingView

IMPLEMENT_DYNCREATE(CImageProcessingView, CScrollView)

BEGIN_MESSAGE_MAP(CImageProcessingView, CScrollView)
//{{AFX_MSG_MAP(CImageProcessingView)
ON_COMMAND(ID_IMAGE_INFO, OnImageInfo)
ON_COMMAND(ID_BILINEARZOOMSHRINK, OnBiLinearZoomshrink)
ON_COMMAND(ID_NEIGHBOUR_ZOOMSHRINK, OnNeighbourZoomshrink)
ON_COMMAND(ID_COMBINING_SPATIAL_ENHANCEMENT, OnCombiningSpatialEnhancement)
	ON_COMMAND(ID_PADDING, OnPadding)
	ON_COMMAND(ID_ZFTJH, OnZftjh)
	ON_COMMAND(ID_MOTIONBLUR, OnMotionblur)
	ON_COMMAND(ID_ZFT, OnZft)
	ON_COMMAND(ID_GAUSSIAN_NOISE, OnGaussianNoise)
	ON_COMMAND(ID_RGB_FAN_SE, OnRGBFanSe)
	ON_COMMAND(ID_HSI_FAN_SE, OnHSIFanSe)
	ON_COMMAND(ID_TEXTURE_SEG, OnTextureSeg)
	//}}AFX_MSG_MAP
// Standard printing commands
//ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
//ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
//ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImageProcessingView construction/destruction

CImageProcessingView::CImageProcessingView()
{
	// TODO: add construction code here
	
}

CImageProcessingView::~CImageProcessingView()
{
}

BOOL CImageProcessingView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CImageProcessingView drawing

void CImageProcessingView::OnDraw(CDC* pDC)
{
	
	CImageProcessingDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	

	HDIB hDIB=pDoc->GetHDIB();
	if(hDIB!=NULL)
	{
		LPSTR lpDIB=(LPSTR)::GlobalLock((HGLOBAL)hDIB);
		int cxDIB=(int)::DIBWidth(lpDIB);
		int cyDIB=(int)::DIBHeight(lpDIB);
		::GlobalUnlock((HGLOBAL)hDIB);
		CRect rcDIB;
		rcDIB.top=rcDIB.left=0;
		rcDIB.right=cxDIB;
		rcDIB.bottom=cyDIB;
		CRect rcDest;
		rcDest=rcDIB;
		::PaintDIB(pDC->m_hDC,&rcDest,pDoc->GetHDIB(),&rcDIB,pDoc->GetDocPalette());
	}
		
}




/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// CImageProcessingView diagnostics

#ifdef _DEBUG
void CImageProcessingView::AssertValid() const
{
	CView::AssertValid();
}

void CImageProcessingView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CImageProcessingDoc* CImageProcessingView::GetDocument() 
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CImageProcessingDoc)));
	return (CImageProcessingDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CImageProcessingView message handlers



LRESULT CImageProcessingView::OnDoRealize(WPARAM wParam, LPARAM)
{
	ASSERT(wParam != NULL);
	
	
	CImageProcessingDoc* pDoc = GetDocument();
	
	
	if (pDoc->GetHDIB() == NULL)
	{
		
		return 0L;
	}
	
	
	CPalette* pPal = pDoc->GetDocPalette();
	if (pPal != NULL)
	{
		// 获取MainFrame
		CMainFrame* pAppFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
		ASSERT_KINDOF(CMainFrame, pAppFrame);
		
		CClientDC appDC(pAppFrame);
		
		
		CPalette* oldPalette = appDC.SelectPalette(pPal, ((HWND)wParam) != m_hWnd);
		
		if (oldPalette != NULL)
		{
			UINT nColorsChanged = appDC.RealizePalette();
			if (nColorsChanged > 0)
				pDoc->UpdateAllViews(NULL);
			appDC.SelectPalette(oldPalette, TRUE);
		}
		else
		{
			TRACE0("\tCImageProcessingView::OnPaletteChanged中调用SelectPalette()失败!\n");
		}
	}
	
	return 0L;
}

void CImageProcessingView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	
}

void CImageProcessingView::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType) 
{
	CView::OnInitialUpdate();
	ASSERT(GetDocument() != NULL);
	
    SetScrollSizes(MM_TEXT, GetDocument()->GetDocSize());
}

void CImageProcessingView::OnActivateView(BOOL bActivate, CView* pActivateView,CView* pDeactiveView)
{
	CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
	
	if (bActivate)
	{
		ASSERT(pActivateView == this);
		OnDoRealize((WPARAM)m_hWnd, 0);   
	}
}







void CImageProcessingView::OnImageInfo() 
{
	// TODO: Add your command handler code here
    CImageProcessingDoc *pDoc=GetDocument();
    if(pDoc->m_hDIB!=0)
	{
    pDoc->ImageInfo.Format("当前图像宽:%d,当前图像高:%d",pDoc->m_sizeDoc.cx,pDoc->m_sizeDoc.cy);
	::MessageBox(NULL,pDoc->ImageInfo,"Visual C++图像增强处理",MB_ICONINFORMATION|MB_OK);
	
	}
	else 
	{
		::MessageBox(NULL,"当前没有可供处理的图像!","Visual C++图像增强处理",MB_ICONINFORMATION|MB_OK);
		return;
	}
	 
}



void CImageProcessingView::OnNeighbourZoomshrink() 
{
	// TODO: Add your command handler code here
	LONG width,height;
	LONG newWidth,newHeight,newLineBytes;
	LPSTR lpDIB,lpDIBBits,lpNewDIB,lpNewDIBBits;
	LPBITMAPINFOHEADER lpbmi,lpNewbmi;

	CImageProcessingDoc *pDoc=GetDocument();
	lpDIB=(LPSTR)::GlobalLock((HGLOBAL)pDoc->m_hDIB);
	lpDIBBits=lpDIB+*(LPDWORD)lpDIB+256*sizeof(RGBQUAD);
	lpbmi=(LPBITMAPINFOHEADER)lpDIB;
	
	width=lpbmi->biWidth;
	height=lpbmi->biHeight;

	float ratioX,ratioY;

	CZoomShrinkDlg dlg;
	dlg.m_origiony=width;
	dlg.m_origionx=height;
	if(dlg.DoModal()!=IDOK)
		return;
	newHeight=dlg.m_setx;
	newWidth=dlg.m_sety;
	newLineBytes=(newWidth*8+31)/32*4;

	ratioX=(double)newHeight/height;
	ratioY=(double)newWidth/width;

	HDIB hDIB=(HDIB)::GlobalAlloc(GHND,newHeight*newLineBytes+*(LPDWORD)lpDIB+256*sizeof(RGBQUAD));
	lpNewDIB=(LPSTR)::GlobalLock((HGLOBAL)hDIB);
	memcpy(lpNewDIB,lpDIB,*(LPDWORD)lpDIB+sizeof(RGBQUAD)*256);

	lpNewDIBBits=lpNewDIB+*(LPDWORD)lpDIB+256*sizeof(RGBQUAD);
	lpNewbmi=(LPBITMAPINFOHEADER)lpNewDIB;
	lpNewbmi->biWidth=newWidth;
	lpNewbmi->biHeight=newHeight;

	LONG i,j;
	float i0,j0;
	LPSTR lpDst;
	for(i=0;i<newHeight;i++)
		for(j=0;j<newWidth;j++)
		{
			lpDst=lpNewDIBBits+newLineBytes*(newHeight-1-i)+j;
			i0=i/ratioX;
			j0=j/ratioY;
			*((unsigned char *)lpDst)=NeighborInterpolation(lpDIBBits,width,height,i0,j0);
		}
	GlobalFree((HGLOBAL)pDoc->m_hDIB);
	pDoc->m_hDIB=hDIB;
	pDoc->m_sizeDoc=CSize((int)newWidth,(int)newHeight);
	SetScrollSizes(MM_TEXT,pDoc->m_sizeDoc);
	pDoc->UpdateAllViews(NULL);
	::GlobalUnlock((HGLOBAL)pDoc->m_hDIB);
	
}


void CImageProcessingView::OnBiLinearZoomshrink() 
{
	// TODO: Add your command handler code here
	LONG width,height,LineBytes;
	LONG newWidth,newHeight,newLineBytes;
	LPSTR lpDIB,lpDIBBits,lpNewDIB,lpNewDIBBits;
	LPBITMAPINFOHEADER lpbmi,lpNewbmi;

	CImageProcessingDoc *pDoc=GetDocument();
	lpDIB=(LPSTR)::GlobalLock((HGLOBAL)pDoc->m_hDIB);
	lpDIBBits=lpDIB+*(LPDWORD)lpDIB+256*sizeof(RGBQUAD);
	lpbmi=(LPBITMAPINFOHEADER)lpDIB;
	
	width=lpbmi->biWidth;
	LineBytes=(width*8+31)/32*4;
	height=lpbmi->biHeight;

	float ratioX,ratioY;

	CZoomShrinkDlg dlg;
	dlg.m_origiony=width;
	dlg.m_origionx=height;
	if(dlg.DoModal()!=IDOK)
		return;
	newHeight=dlg.m_setx;
	newWidth=dlg.m_sety;
	newLineBytes=(newWidth*8+31)/32*4;

	ratioX=(double)newHeight/height;
	ratioY=(double)newWidth/width;

	HDIB hDIB=(HDIB)::GlobalAlloc(GHND,newHeight*newLineBytes+*(LPDWORD)lpDIB+256*sizeof(RGBQUAD));
	lpNewDIB=(LPSTR)::GlobalLock((HGLOBAL)hDIB);
	memcpy(lpNewDIB,lpDIB,*(LPDWORD)lpDIB+sizeof(RGBQUAD)*256);

	lpNewDIBBits=lpNewDIB+*(LPDWORD)lpDIB+256*sizeof(RGBQUAD);
	lpNewbmi=(LPBITMAPINFOHEADER)lpNewDIB;
	lpNewbmi->biWidth=newWidth;
	lpNewbmi->biHeight=newHeight;

	LONG i,j;
	float i0,j0;
	LPSTR lpDst;
	for(i=0;i<newHeight;i++)
		for(j=0;j<newWidth;j++)
		{
			lpDst=lpNewDIBBits+newLineBytes*(newHeight-1-i)+j;
			i0=i/ratioX;
			j0=j/ratioY;
			*((unsigned char *)lpDst)=BiLinearInterpolation(lpDIBBits,width,height,i0,j0);
		}
	GlobalFree((HGLOBAL)pDoc->m_hDIB);
	pDoc->m_hDIB=hDIB;
	pDoc->m_sizeDoc=CSize((int)newWidth,(int)newHeight);
	SetScrollSizes(MM_TEXT,pDoc->m_sizeDoc);
	pDoc->UpdateAllViews(NULL);
	::GlobalUnlock((HGLOBAL)pDoc->m_hDIB);
}


void CImageProcessingView::OnCombiningSpatialEnhancement() 
{
	// TODO: Add your command handler code here
	CImageProcessingDoc *pDoc=GetDocument();
	LPSTR lpDIB,lpDIBBits,pTemp;
	LONG width,height,LineBytes;
	lpDIB=(LPSTR)::GlobalLock(pDoc->m_hDIB);
	lpDIBBits=lpDIB+*(LPDWORD)lpDIB+256*sizeof(RGBQUAD);
	LPBITMAPINFOHEADER lpbmi=(LPBITMAPINFOHEADER)lpDIB;
	width=lpbmi->biWidth;
	LineBytes=(width*8+31)/32*4;
	height=lpbmi->biHeight;

	CEnhancement dlg;
	if(dlg.DoModal()!=IDOK)
		return;
	BYTE s=dlg.m_select;
	switch(s)
	{
	case 2:
		{
			LPSTR pLaplacian=(LPSTR)new char[LineBytes*height];
			Laplacian(lpDIBBits,pLaplacian,width,height);
			pTemp=pLaplacian;
			break;
		}
	case 3:
	case 4:
	case 6:
		{
			LPSTR pLaplacianEnhanced=(LPSTR)new char[LineBytes*height];
			LPSTR pSobel=(LPSTR)new char[LineBytes*height];
			LPSTR pSmoothSobel=(LPSTR)new char[LineBytes*height];
			MulSharpen(lpDIBBits,pLaplacianEnhanced,pSobel,pSmoothSobel,width,height);
			if(s==3)
			{
				delete []pSobel;
				pTemp=pLaplacianEnhanced;
			}
			else if(s==4)
			{
				delete []pLaplacianEnhanced;
				pTemp=pSobel;
			}
			else
			{
				delete []pLaplacianEnhanced;
				delete []pSobel;
				pTemp=pSmoothSobel;
			}
			break;
		}
	case 5:
		{
			LPSTR pSobel=(LPSTR)new char[LineBytes*height];
			LPSTR pSmoothSobel=(LPSTR)new char[LineBytes*height];
			SmoothSobel(lpDIBBits,pSobel,pSmoothSobel,width,height);
			delete []pSobel;
			pTemp=pSmoothSobel;
			break;
		}
	case 7:
		{
			LPSTR pLaplacianEnhanced=(LPSTR)new char[LineBytes*height];
			LPSTR pSobel=(LPSTR)new char[LineBytes*height];
			LPSTR pSmoothSobel=(LPSTR)new char[LineBytes*height];
			SharpenEnhanced(lpDIBBits,pLaplacianEnhanced,pSobel,pSmoothSobel,width,height);
			delete []pLaplacianEnhanced;
			delete []pSobel;
			pTemp=pSmoothSobel;
			break;
		}
	case 8:
		{
			LPSTR pLaplacianEnhanced=(LPSTR)new char[LineBytes*height];
			LPSTR pSobel=(LPSTR)new char[LineBytes*height];
			LPSTR pSmoothSobel=(LPSTR)new char[LineBytes*height];
			PowerLaw(lpDIBBits,pLaplacianEnhanced,pSobel,pSmoothSobel,width,height);
			delete []pLaplacianEnhanced;
			delete []pSobel;
			pTemp=pSmoothSobel;
			break;
		}
	default:return;
	}
	LONG i,j;
	for(i=0;i<height;i++)
	for(j=0;j<width;j++)
		{
		*(lpDIBBits+LineBytes*(height-1-i)+j)=*(pTemp+LineBytes*(height-1-i)+j);
		}
	Invalidate();
	::GlobalUnlock((HGLOBAL)pDoc->m_hDIB);
}


void CImageProcessingView::OnPadding() 
{
	// TODO: Add your command handler code here
	LONG width,height,LineBytes;
	LONG newWidth,newHeight,newLineBytes;
	LPSTR lpDIB,lpDIBBits,lpNewDIB,lpNewDIBBits;
	LPBITMAPINFOHEADER lpbmi,lpNewbmi;
	
	CImageProcessingDoc *pDoc=GetDocument();
	lpDIB=(LPSTR)::GlobalLock((HGLOBAL)pDoc->m_hDIB);
	lpDIBBits=lpDIB+*(LPDWORD)lpDIB+256*sizeof(RGBQUAD);
	lpbmi=(LPBITMAPINFOHEADER)lpDIB;
	
	width=lpbmi->biWidth;
	LineBytes=(width*8+31)/32*4;
	height=lpbmi->biHeight;
	
	
	double temp;
	LONG i,j;
	for(i=0;;i++)

⌨️ 快捷键说明

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