?? 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);
// 獲取打印機(jī)每英寸象素數(shù)
int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
// 計算打印圖像大小(縮放,根據(jù)頁面寬度調(diào)整圖像大小)
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();
//進(jìn)行中值濾波
pDoc->m_hDIB=pDoc->GetDibImage()->MidFilter(pDoc->m_hDIB,3,3);
// pDoc->GetDibImage()->MidFilter(HDIB hDIB,int tem_w,int tem_h);//調(diào)用DibImage中的MidFilter(),對圖像中值濾波
Invalidate(TRUE);
}
void CMysplitView::OnMuBiaoErZhiHua()
{
// TODO: Add your command handler code here
flag=2;
k=1; //k=1用于標(biāo)記執(zhí)行過該函數(shù)
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色位圖的閾值變換!", "系統(tǒng)提示" ,
MB_ICONINFORMATION | MB_OK);
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
return;
}
MessageBox("請用鼠標(biāo)雙擊來選擇提取目標(biāo)");
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; //記錄左鍵雙擊的橫坐標(biāo)
int yy=point.y; //記錄左鍵雙擊的縱坐標(biāo)
if(flag==1) pDoc->GetDibImage()->QuYuZhengZhang(lpDIBBits,Wide,Height,xx,yy,yuzhi); //調(diào)用區(qū)域增長法的函數(shù)
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) //是否執(zhí)行過閾值分割的函數(shù)(k==1時表示執(zhí)行過)
{
t=1;
Lx=point.x; //記錄左鍵的橫坐標(biāo)
Ly=point.y; //記錄左鍵的縱坐標(biāo)
}
CView::OnLButtonDown(nFlags, point);
}
void CMysplitView::OnErZhiHua()
{
// TODO: Add your command handler code here
k=1; // k=1用于標(biāo)記執(zhí)行過該函數(shù)
float midu[256]; // 記錄圖象的灰度密度
CMysplitDoc* pDoc = GetDocument();
BYTE YuZhi; // 進(jìn)行分割的閾值
LPSTR lpDIB; // 指向DIB的指針
CErZhiHuaDlg dlgPara; // 參數(shù)對話框,用于輸入閾值
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色位圖的閾值變換!", "系統(tǒng)提示" ,
MB_ICONINFORMATION | MB_OK);
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
return;
}
pDoc->GetDibImage()->TongJi(lpDIB,midu); //調(diào)用DibImage中的TongJi(),對數(shù)組midu[]賦值
for (int i = 0; i <256; i++)
dlgPara.m_fIntensity[i] = midu[i]; //將midu[]轉(zhuǎn)移到對話框中,用于畫直方圖
dlgPara.m_YuZhi = 0; //對話框中的閾值初始為0
// 顯示對話框,提示用戶設(shè)定閾值
if (dlgPara.DoModal() != IDOK)
{
return;
}
YuZhi = dlgPara.m_YuZhi; //將對話框中的閾值轉(zhuǎn)移到變量YuZhi中
delete dlgPara;
BeginWaitCursor();
// 調(diào)用ErZhiHua()函數(shù)進(jìn)行閾值變換
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用于標(biāo)記執(zhí)行過該函數(shù)
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色位圖的閾值變換!", "系統(tǒng)提示" ,
MB_ICONINFORMATION | MB_OK);
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
return;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -