📄 edge_detectionview.cpp
字号:
// Edge_detectionView.cpp : implementation of the CEdge_detectionView class
//
#include "stdafx.h"
#include "Edge_detection.h"
#include "Edge_detectionDoc.h"
#include "Edge_detectionView.h"
#include "math.h"
#include "GlobalFunc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEdge_detectionView
IMPLEMENT_DYNCREATE(CEdge_detectionView, CView)
BEGIN_MESSAGE_MAP(CEdge_detectionView, CView)
//{{AFX_MSG_MAP(CEdge_detectionView)
ON_COMMAND(ID_EDGE_CANNY, OnEdgeCanny)
ON_COMMAND(ID_SOBEL_OPERATOR, OnSobelOperator)
ON_COMMAND(ID_SPATIAL_MOMENT, OnSpatialMoment)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEdge_detectionView construction/destruction
CEdge_detectionView::CEdge_detectionView()
{
// TODO: add construction code here
}
CEdge_detectionView::~CEdge_detectionView()
{
}
BOOL CEdge_detectionView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CEdge_detectionView drawing
void CEdge_detectionView::OnDraw(CDC* pDC)
{
CEdge_detectionDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CSize sizeDibDisplay;
if(!pDoc->m_pDibInit->IsEmpty()){
sizeDibDisplay = pDoc->m_pDibInit->GetDimensions();
pDoc->m_pDibInit->Draw(pDC,CPoint(0,0),sizeDibDisplay);
}
}
BOOL CEdge_detectionView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CEdge_detectionView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CEdge_detectionView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CEdge_detectionView diagnostics
#ifdef _DEBUG
void CEdge_detectionView::AssertValid() const
{
CView::AssertValid();
}
void CEdge_detectionView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CEdge_detectionDoc* CEdge_detectionView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEdge_detectionDoc)));
return (CEdge_detectionDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEdge_detectionView message handlers
/*************************************************************************
*
* \函数名称:
* OnEdgeCanny()
*
* \输入参数:
* 无
*
* \返回值:
* 无
*
* \说明:
* 实现并行边界分割-Canny算子
*
************************************************************************
*/
void CEdge_detectionView::OnEdgeCanny()
{
// TODO: Add your command handler code here
//更改光标形状
BeginWaitCursor();
CEdge_detectionDoc * pDoc = GetDocument();
ASSERT_VALID(pDoc);
CDib* pDib = pDoc->m_pDibInit;
LPBITMAPINFOHEADER lpBMIH=pDib->m_lpBMIH;
// 判断是否是8-bpp位图
if (lpBMIH->biBitCount != 8)
{
// 提示用户
MessageBox("目前只支持256色位图的图象分割!", "系统提示" ,
MB_ICONINFORMATION | MB_OK);
// 返回
return;
}
// 循环控制变量
int y;
int x;
CSize sizeImage = pDib->GetDimensions();
int nWidth = sizeImage.cx ;
int nHeight= sizeImage.cy ;
int nSaveWidth = pDib->GetDibSaveDim().cx;
// 开辟内存,存储图象数据
unsigned char * pUnchImage = new unsigned char[nWidth*nHeight];
for(y=0; y<nHeight; y++)
{
for(x=0; x<nWidth; x++)
{
pUnchImage[y*nWidth+x] = pDib->m_lpImage[y*nSaveWidth+x];
}
}
// canny算子计算后的结果
unsigned char * pUnchEdge = new unsigned char[nWidth*nHeight];
// 调用canny函数进行边界提取
Canny(pUnchImage, nWidth, nHeight, 0.4, 0.4, 0.79, pUnchEdge);
for(y=0; y<nHeight; y++)
{
for(x=0; x<nWidth; x++)
{
//255代表边界,但是显示的时候是用黑线表示边界
pDib->m_lpImage[y*nWidth+x]=(unsigned char)(255-pUnchEdge[y*nWidth+x]);
}
}
delete []pUnchImage;
pUnchImage = NULL ;
delete []pUnchEdge ;
pUnchEdge = NULL ;
// 恢复光标形状
EndWaitCursor();
// 设置脏标记
pDoc->SetModifiedFlag(TRUE);
// 更新视图
pDoc->UpdateAllViews(NULL);
}
/*************************************************************************
*
* \函数名称:
* OnEdgeSobel()
*
* \输入参数:
* 无
*
* \返回值:
* 无
*
* \说明:
* 实现并行边界分割-Sobel算子
*
*************************************************************************/
void CEdge_detectionView::OnSobelOperator()
{
// TODO: Add your command handler code here
//更改光标形状
BeginWaitCursor();
// 循环控制变量
int y;
int x;
CEdge_detectionDoc * pDoc = (CEdge_detectionDoc *)this->GetDocument();
CDib * pDib = pDoc->m_pDibInit;
LPBITMAPINFOHEADER lpBMIH=pDib->m_lpBMIH;
// 判断是否是8-bpp位图
if (lpBMIH->biBitCount != 8)
{
// 提示用户
MessageBox("目前只支持256色位图的图象分割!", "系统提示" ,
MB_ICONINFORMATION | MB_OK);
// 返回
return;
}
// 图象的长宽大小
CSize sizeImage = pDib->GetDimensions();
int nWidth = sizeImage.cx ;
int nHeight = sizeImage.cy ;
// 指向梯度数据的指针
double * pdGrad;
// 按照图像的大小开辟内存空间,存储梯度计算的结果
pdGrad=new double[nHeight*nWidth];
//图像数据的指针
LPBYTE lpImage = pDib->m_lpImage;
// 图像在计算机在存储中的实际大小
CSize sizeImageSave = pDib->GetDibSaveDim();
// 图像在内存中每一行象素占用的实际空间
int nSaveWidth = sizeImageSave.cx;
// 应用Sobel算子求梯度
SobelOperator(pDib, pdGrad);
for(y=0; y<nHeight ; y++ )
for(x=0 ; x<nWidth ; x++ )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -