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

📄 mrf_imgsegdlg.cpp

📁 有指导的马尔可夫随机场(MRF)的图像分割代码
💻 CPP
字号:
// MRF_imgSegDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MRF_imgSeg.h"
#include "MRF_imgSegDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMRF_imgSegDlg dialog

CMRF_imgSegDlg::CMRF_imgSegDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMRF_imgSegDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMRF_imgSegDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMRF_imgSegDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMRF_imgSegDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMRF_imgSegDlg, CDialog)
	//{{AFX_MSG_MAP(CMRF_imgSegDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOPEN, OnOpen)
	ON_BN_CLICKED(IDRESET, OnReset)
	ON_WM_LBUTTONUP()
	ON_BN_CLICKED(IDSEGMENT, OnSegment)
	ON_MESSAGE(WM_REDRAW,OnRedraw)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMRF_imgSegDlg message handlers

BOOL CMRF_imgSegDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	//m_classN=0;
	//	m_mouseEnable=0;
	//	for(int i=0;i<30;i++)
	//	{
	//		m_area[i].x=0; m_area[i].y=0;
	//		m_area[i].width=0; m_area[i].height=0;
	//	}
	//	m_selection.x=0; m_selection.y=0;
	//	m_selection.width=0; m_selection.height=0;
	
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMRF_imgSegDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

//DEL void CMRF_imgSegDlg::OnPaint() 
//DEL {
//DEL 	if (IsIconic())
//DEL 	{
//DEL 		CPaintDC dc(this); // device context for painting
//DEL 
//DEL 		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
//DEL 
//DEL 		// Center icon in client rectangle
//DEL 		int cxIcon = GetSystemMetrics(SM_CXICON);
//DEL 		int cyIcon = GetSystemMetrics(SM_CYICON);
//DEL 		CRect rect;
//DEL 		GetClientRect(&rect);
//DEL 		int x = (rect.Width() - cxIcon + 1) / 2;
//DEL 		int y = (rect.Height() - cyIcon + 1) / 2;
//DEL 
//DEL 		// Draw the icon
//DEL 		dc.DrawIcon(x, y, m_hIcon);
//DEL 	
//DEL 		
//DEL 	}
//DEL 	else
//DEL 	{
//DEL 		CDialog::OnPaint();
//DEL 	}
//DEL }


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

LRESULT CMRF_imgSegDlg::OnRedraw(WPARAM wParam, LPARAM lParam)
{
	MessageBox("OnMyMessage!");
	if (m_mrf.m_result!=NULL) {
		cvShowImage("Result",m_mrf.m_result);
	}
	return 0;
}

//void  CMRF_imgSegDlg::OnRedraw(UINT nID, LPARAM lParam)
//{
//	
////	if (m_mrf.m_result!=NULL) {
////		cvShowImage("Result",m_result);
////	}
//
//	



// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMRF_imgSegDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMRF_imgSegDlg::OnOpen() 
{
	// TODO: Add your control notification handler code here
	CFileDialog fileDlg(TRUE);
	fileDlg.m_ofn.lpstrTitle="打开文图像件";
	//fileDlg.m_ofn.lpstrFilter="*.jpg/*.bmp";
	//fileDlg.m_ofn.lpstrInitialDir="D:\\multimedia\\movies\\avi";
	fileDlg.m_ofn.lpstrInitialDir=NULL;
	if (IDOK==fileDlg.DoModal())
	{
		m_fileName=fileDlg.GetPathName();
		img=cvLoadImage(m_fileName,CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
		m_img=cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
		cvCopy(img,m_img);
		if (img==NULL)
			{
				return;
			}
	}
	cvNamedWindow("Origin",1);
	cvNamedWindow("Result",1);
	cvShowImage("Origin",img);
//	cvShowImage("Result",img);
	cvSetMouseCallback( "Origin", on_mouse, 0 );
	// cvWaitKey(0);
	
}

void CMRF_imgSegDlg::OnOK() 
{
	// TODO: Add extra validation here
	CString str;
	GetDlgItem(IDC_CLASSNUM)->GetWindowText(str);
	classN=atoi(str);
	if (classN>0) {
		mouseEnable=1;
	}
    
	OnReset();
	
//	CDialog::OnOK();
}

void CMRF_imgSegDlg::OnReset() 
{
	// TODO: Add your control notification handler code here
	cvCopy(m_img,img);
	cvShowImage("Origin",img);
//	cvShowImage("Result",img);
	
	
}


void CMRF_imgSegDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if (selected) {
		selected=0;
		cvRectangle(img, cvPoint(selection.x,selection.y), cvPoint(selection.x+selection.width,selection.y+selection.height), CV_RGB(255,0,0),
			1, 8,0 );
	}
	if (Number==classN) {
		mouseEnable=0;
	}
	cvShowImage("Origin",img);
	
	CDialog::OnLButtonUp(nFlags, point);
}

void CMRF_imgSegDlg::OnSegment() 
{
	// TODO: Add your control notification handler code here
	IplImage * gray;
	gray = cvCreateImage(cvSize(m_img->width,m_img->height),8,1);
	cvCvtColor(m_img,gray,CV_RGB2GRAY);
//	cvShowImage("Result",gray);
	m_mrf.SetMember(gray,area,classN);
	m_mrf.Gibbs();
	cvReleaseImage(&gray);
}

//DEL float CMRF_imgSegDlg::CalculEnergy()
//DEL {
//DEL 
//DEL }

⌨️ 快捷键说明

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