📄 mysplitview.cpp
字号:
// MysplitView.cpp : implementation of the CMysplitView class
//
#include "stdafx.h"
#include "Mysplit.h"
#include "MysplitDoc.h"
#include "MysplitView.h"
#include "ErZhiHuaDlg.h"
#include "QuYuZhengZhangDlg.h"
#include "DibImage.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMysplitView
IMPLEMENT_DYNCREATE(CMysplitView, CView)
BEGIN_MESSAGE_MAP(CMysplitView, CView)
//{{AFX_MSG_MAP(CMysplitView)
ON_COMMAND(ID_Zhongzhilvbo, OnZhongzhilvbo)
ON_COMMAND(IDC_MuBiaoErZhiHua, OnMuBiaoErZhiHua)
ON_WM_LBUTTONDBLCLK()
ON_WM_LBUTTONDOWN()
ON_COMMAND(ID_ErZhiHua, OnErZhiHua)
ON_COMMAND(ID_QuYuZhengZhang, OnQuYuZengZhang)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMysplitView construction/destruction
CMysplitView::CMysplitView()
{
// TODO: add construction code here
}
CMysplitView::~CMysplitView()
{
}
BOOL CMysplitView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMysplitView drawing
void CMysplitView::OnDraw(CDC* pDC)
{
BeginWaitCursor();
CMysplitDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
HDIB hDIB = pDoc->GetHDIB();
if (hDIB != NULL)
{
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
int cxDIB = (int) pDoc->GetDibImage()->DIBWidth(lpDIB);
int cyDIB = (int) pDoc->GetDibImage()->DIBHeight(lpDIB);
::GlobalUnlock((HGLOBAL) hDIB);
CRect rcDIB;
rcDIB.top = rcDIB.left = 0;
rcDIB.right = cxDIB;
rcDIB.bottom = cyDIB;
CRect rcDest;
if (pDC->IsPrinting())
{
// 是打印,计算输出图像的位置和大小,以便符合页面
// 获取打印页面的水平宽度(象素)
int cxPage = pDC->GetDeviceCaps(HORZRES);
// 获取打印页面的垂直高度(象素)
int cyPage = pDC->GetDeviceCaps(VERTRES);
// 获取打印机每英寸象素数
int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
// 计算打印图像大小(缩放,根据页面宽度调整图像大小)
rcDest.top = rcDest.left = 0;
rcDest.bottom = (int)(((double)cyDIB * cxPage * cyInch)
/ ((double)cxDIB * cxInch));
rcDest.right = cxPage;
// 计算打印图像位置(垂直居中)
int temp = cyPage - (rcDest.bottom - rcDest.top);
rcDest.bottom += temp/2;
rcDest.top += temp/2;
}
else
{
// 不必缩放图像
rcDest = rcDIB;
}
pDoc->GetDibImage()->PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(),
&rcDIB, pDoc->GetDocPalette());
}
EndWaitCursor();
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CMysplitView printing
BOOL CMysplitView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMysplitView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMysplitView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMysplitView diagnostics
#ifdef _DEBUG
void CMysplitView::AssertValid() const
{
CView::AssertValid();
}
void CMysplitView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMysplitDoc* CMysplitView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMysplitDoc)));
return (CMysplitDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMysplitView message handlers
void CMysplitView::OnZhongzhilvbo()
{
// TODO: Add your command handler code here
CMysplitDoc* pDoc = GetDocument();
//进行中值滤波
pDoc->m_hDIB=pDoc->GetDibImage()->MidFilter(pDoc->m_hDIB,3,3);
// pDoc->GetDibImage()->MidFilter(HDIB hDIB,int tem_w,int tem_h);//调用DibImage中的MidFilter(),对图像中值滤波
Invalidate(TRUE);
}
void CMysplitView::OnMuBiaoErZhiHua()
{
// TODO: Add your command handler code here
flag=2;
k=1; //k=1用于标记执行过该函数
CMysplitDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc); // 判断是否是8-bpp位图(这里只处理8-bpp位图的阈值变换,其它的可以类推)
LPSTR lpDIB; // 指向DIB的指针
LPSTR lpDIBBits; // 指向DIB象素指针
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
if (pDoc->GetDibImage()->DIBNumColors(lpDIB) != 256)
{
MessageBox("目前只支持256色位图的阈值变换!", "系统提示" ,
MB_ICONINFORMATION | MB_OK);
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
return;
}
MessageBox("请用鼠标双击来选择提取目标");
Invalidate();
}
void CMysplitView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMysplitDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
LPSTR lpDIB; // 指向DIB的指针
LPSTR lpDIBBits; // 指向DIB象素指针
DWORD Wide; // DIB中图像的宽度
DWORD Height; // DIB中图像的高度
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
Wide=pDoc->GetDibImage()->DIBWidth(lpDIB);
Height=pDoc->GetDibImage()->DIBHeight(lpDIB);
lpDIBBits = pDoc->GetDibImage()->FindDIBBits(lpDIB); // 找到DIB图像象素起始位置
int xx=point.x; //记录左键双击的横坐标
int yy=point.y; //记录左键双击的纵坐标
if(flag==1) pDoc->GetDibImage()->QuYuZhengZhang(lpDIBBits,Wide,Height,xx,yy,yuzhi); //调用区域增长法的函数
else if(flag==2)pDoc->GetDibImage()->MuBiaoErZhiHua(lpDIBBits,Wide,Height,xx,yy);
else ;
pDoc->UpdateAllViews(NULL);
CView::OnLButtonDblClk(nFlags, point);
}
void CMysplitView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(k==1) //是否执行过阈值分割的函数(k==1时表示执行过)
{
t=1;
Lx=point.x; //记录左键的横坐标
Ly=point.y; //记录左键的纵坐标
}
CView::OnLButtonDown(nFlags, point);
}
void CMysplitView::OnErZhiHua()
{
// TODO: Add your command handler code here
k=1; // k=1用于标记执行过该函数
float midu[256]; // 记录图象的灰度密度
CMysplitDoc* pDoc = GetDocument();
BYTE YuZhi; // 进行分割的阈值
LPSTR lpDIB; // 指向DIB的指针
LPSTR lpDIBBits; // 指向DIB象素指针
CErZhiHuaDlg dlgPara; // 参数对话框,用于输入阈值
DWORD Wide; // DIB中图像的宽度
DWORD Height; // DIB中图像的高度
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
lpDIBBits = pDoc->GetDibImage()->FindDIBBits(lpDIB); // 找到DIB图像象素起始位置
Wide=pDoc->GetDibImage()->DIBWidth(lpDIB);
Height=pDoc->GetDibImage()->DIBHeight(lpDIB);
// 判断是否是8-bpp位图(这里只处理8-bpp位图的阈值变换,其它的可以类推)
if (pDoc->GetDibImage()->DIBNumColors(lpDIB) != 256)
{
MessageBox("目前只支持256色位图的阈值变换!", "系统提示" ,
MB_ICONINFORMATION | MB_OK);
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
return;
}
pDoc->GetDibImage()->TongJi(lpDIB,midu); //调用DibImage中的TongJi(),对数组midu[]赋值
for (int i = 0; i <256; i++)
dlgPara.m_fIntensity[i] = midu[i]; //将midu[]转移到对话框中,用于画直方图
dlgPara.m_YuZhi = 0; //对话框中的阈值初始为0
// 显示对话框,提示用户设定阈值
if (dlgPara.DoModal() != IDOK)
{
return;
}
YuZhi = dlgPara.m_YuZhi; //将对话框中的阈值转移到变量YuZhi中
delete dlgPara;
BeginWaitCursor();
// 调用ErZhiHua()函数进行阈值变换
pDoc->GetDibImage()->ErZhiHua(lpDIBBits, Wide, Height, YuZhi);
pDoc->UpdateAllViews(NULL); // 更新视图
::GlobalUnlock((HGLOBAL)pDoc->GetHDIB());
EndWaitCursor();
}
void CMysplitView::OnQuYuZengZhang()
{
// TODO: Add your command handler code here
flag=1;
k=1; //k=1用于标记执行过该函数
CMysplitDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc); // 判断是否是8-bpp位图(这里只处理8-bpp位图的阈值变换,其它的可以类推)
LPSTR lpDIB; // 指向DIB的指针
//LPSTR lpDIBBits; // 指向DIB象素指针
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
CQuYuZhengZhangDlg dlg3;
//int yuzhi;
if (dlg3.DoModal()==IDOK)
{
yuzhi=dlg3.m_yuzhi;
}
if (pDoc->GetDibImage()->DIBNumColors(lpDIB) != 256)
{
MessageBox("目前只支持256色位图的阈值变换!", "系统提示" ,
MB_ICONINFORMATION | MB_OK);
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -