📄 picture trainingdlg.cpp
字号:
// Picture TrainingDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Picture Training.h"
#include "Picture TrainingDlg.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern int IsNormalize;
extern int TargetHeight;
extern int TargetWidth;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CPictureTrainingDlg dialog
CPictureTrainingDlg::CPictureTrainingDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPictureTrainingDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPictureTrainingDlg)
m_RawImgFolder = _T("");
m_ResultsFolder = _T("");
m_Cuurent_Deal_Number = 0;
m_Total_Number = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPictureTrainingDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPictureTrainingDlg)
DDX_Text(pDX, IDC_Raw_Folder, m_RawImgFolder);
DDX_Text(pDX, IDC_Results_Folder, m_ResultsFolder);
DDX_Text(pDX, IDC_EDIT_Current, m_Cuurent_Deal_Number);
DDX_Text(pDX, IDC_EDIT_Total, m_Total_Number);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPictureTrainingDlg, CDialog)
//{{AFX_MSG_MAP(CPictureTrainingDlg)
ON_WM_ERASEBKGND()
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_Raw_Folder_Location, OnBUTTONRawFolderLocation)
ON_BN_CLICKED(IDC_BUTTON_Results_Folder, OnBUTTONResultsFolder)
ON_BN_CLICKED(IDC_BUTTON_Exit, OnBUTTONExit)
ON_BN_CLICKED(IDC_BUTTON_Setting, OnBUTTONSetting)
ON_BN_CLICKED(IDC_BUTTON_Next, OnBUTTONNext)
ON_BN_CLICKED(IDC_BUTTON_Previous, OnBUTTONPrevious)
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_KILLFOCUS()
ON_WM_CAPTURECHANGED()
ON_BN_CLICKED(IDC_BUTTON_WRITE_TO_FILE, OnButtonWriteToFile)
ON_BN_CLICKED(IDC_BUTTON_POINT, OnButtonPoint)
ON_BN_CLICKED(IDC_BUTTON_Confirm, OnBUTTONConfirm)
ON_BN_CLICKED(IDC_BUTTON_EraseLast, OnBUTTONEraseLast)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPictureTrainingDlg message handlers
BOOL CPictureTrainingDlg::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);
}
}
OnStart=FALSE;
LosingCapture=TRUE;
// 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
CurrentFileNum=0;
FilePathName=NULL;
CurrentImg=NULL;
SaveImg=NULL;
CurrentDealNum=0;
IsWorking=NULL;
IsInRegion=FALSE;
IsLocating=FALSE;
IsAdd=FALSE;
IsReady=0;
BoundaryPoint[0]=(0,0);
BoundaryPoint[1]=(0,0);
PicRegion.bottom=510;
PicRegion.left=20;
PicRegion.right=490;
PicRegion.top=20;
m_ResultsFolder="";
return TRUE; // return TRUE unless you set the focus to a control
}
void CPictureTrainingDlg::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.
void CPictureTrainingDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
CPoint Leftup(20,20);
CSize size(470,490);
//用双缓冲绘图
CDC *pDC;
pDC=GetDC(); //当前DC
CDC dcMem; //内存DC
CBitmap bm;
CRect rc;
CPen Pen(PS_SOLID, 2, RGB(0, 255,0 )),Pen1(PS_SOLID, 2, RGB(255, 0,0 ));
rc.bottom=510;
rc.top=20;
rc.left=20;
rc.right=490;
// Step 1:为屏幕DC创建兼容的内存DC :CreateCompatibleDC()
dcMem.CreateCompatibleDC(pDC);
// Step 2:创建位图:CreateCompatibleBitmap()
bm.CreateCompatibleBitmap(pDC,rc.Width(),rc.Height());
// Step 3:把位图选入设备环境:SelectObject(),可以理解为选择画布
dcMem.SelectObject(&bm);
dcMem.SelectObject(Pen);
dcMem.SelectStockObject(NULL_BRUSH);
if (CurrentImg) {
dcMem.SetStretchBltMode(HALFTONE);
CurrentImg->Stretch(&dcMem,(0,0),size);
}
dcMem.Rectangle(BoundaryPoint[0].x-20,BoundaryPoint[0].y-20,BoundaryPoint[1].x-20,BoundaryPoint[1].y-20);
if(IsReady==2)
{
dcMem.SelectObject(Pen1);
dcMem.Rectangle(ScreenCenter.x-20,ScreenCenter.y-20,ScreenCenter.x+3-20,ScreenCenter.y+3-20);
}
// Step 4:把绘制好的图形“拷贝“到屏幕上:BitBlt()
pDC->BitBlt(rc.left,rc.top,rc.Width(),rc.Height(),&dcMem,0,0,SRCCOPY);
dcMem.DeleteDC();
bm.DeleteObject();
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CPictureTrainingDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPictureTrainingDlg::OnBUTTONRawFolderLocation()
{
// TODO: Add your control notification handler code here
//////////////////////////////////////////////////////////////////////////
FileNum=0;
CurrentFileNum=0;
//弹出文件夹选项
LPMALLOC g_pMalloc;
if (!SUCCEEDED(SHGetMalloc(&g_pMalloc))) return;
BROWSEINFO bi;
LPTSTR lpBuffer;
LPITEMIDLIST pidlRoot;
LPITEMIDLIST pidlBrowse;
if ((lpBuffer = (LPTSTR)g_pMalloc->Alloc(MAX_PATH)) == NULL)
return;
if (!SUCCEEDED(SHGetSpecialFolderLocation(m_hWnd, CSIDL_DESKTOP, &pidlRoot)))
{
g_pMalloc->Free(lpBuffer);
return;
}
bi.hwndOwner = m_hWnd;
bi.pidlRoot = pidlRoot;
bi.pszDisplayName = lpBuffer;
bi.lpszTitle = _T("选择图片库所在的文件夹");
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
pidlBrowse = SHBrowseForFolder(&bi);
if (pidlBrowse != NULL)
{ IsWorking=TRUE;
if (SHGetPathFromIDList(pidlBrowse, lpBuffer))
{
//这就是你要得到的返回的目标路径
m_RawImgFolder=(CString)lpBuffer;
}
g_pMalloc->Free(pidlBrowse);
UpdateData(FALSE);
FindFileNumRecurse(m_RawImgFolder);
if (FilePathName!=NULL) {
delete [] FilePathName;
}
FilePathName=new CString [FileNum];
CreateFileNameRecurse(m_RawImgFolder);
CurrentDealNum=FileNum*10;
if (CurrentImg) {
CurrentImg->ImageClear();
CurrentImg=NULL;
}
if (FileNum!=0) {
CurrentImg=new CImage;
CurrentImg->ReadFromFile(FilePathName[0]);
m_Cuurent_Deal_Number++;
}
else
IsWorking=FALSE;
Invalidate(TRUE);
}
else
IsWorking=FALSE;
m_Total_Number=FileNum;
UpdateData(FALSE);
}
void CPictureTrainingDlg::OnBUTTONResultsFolder()
{
// TODO: Add your control notification handler code here
//////////////////////////////////////////////////////////////////////////
//弹出文件夹选项
LPMALLOC g_pMalloc;
if (!SUCCEEDED(SHGetMalloc(&g_pMalloc))) return;
BROWSEINFO bi;
LPTSTR lpBuffer;
LPITEMIDLIST pidlRoot;
LPITEMIDLIST pidlBrowse;
if ((lpBuffer = (LPTSTR)g_pMalloc->Alloc(MAX_PATH)) == NULL)
return;
if (!SUCCEEDED(SHGetSpecialFolderLocation(m_hWnd, CSIDL_DESKTOP, &pidlRoot)))
{
g_pMalloc->Free(lpBuffer);
return;
}
bi.hwndOwner = m_hWnd;
bi.pidlRoot = pidlRoot;
bi.pszDisplayName = lpBuffer;
bi.lpszTitle = _T("选择图片库所在的文件夹");
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
pidlBrowse = SHBrowseForFolder(&bi);
if (pidlBrowse != NULL)
{
if (SHGetPathFromIDList(pidlBrowse, lpBuffer))
{
//这就是你要得到的返回的目标路径
m_ResultsFolder=(CString)lpBuffer;
}
g_pMalloc->Free(pidlBrowse);
CStdioFile mFile; //先查找是否有信息文件,如果有,就跳过,如果没有新建立文件
if(mFile.Open(_T(m_ResultsFolder+"\\Label.inf "),CFile::modeRead)==1)
{
CString info,CSTotalNumber;
mFile.ReadString(info);
mFile.ReadString(CSTotalNumber);
TotalSample=atoi(CSTotalNumber);
MessageBox("原有训练集共有"+CSTotalNumber+"个图片,将继续添加!");
mFile.Close();
}
else
{
CStdioFile mFile1(m_ResultsFolder+"\\Label.inf ", CFile::modeCreate|CFile::modeWrite);
mFile1.WriteString("The Number of the Train Set is:(Filename\\Width\\Height\\CenterX\\CnterY)\n");
mFile1.WriteString("0");
TotalSample=0;
mFile1.Close();
MessageBox("没有训练集,已经创建新的训练文件!");
}
}
UpdateData(FALSE);
}
void CPictureTrainingDlg::OnBUTTONExit()
{
// TODO: Add your control notification handler code here
OnCancel();
}
void CPictureTrainingDlg::OnBUTTONSetting()
{
// TODO: Add your control notification handler code here
CSettingDlg Set;
Set.DoModal();
}
//////////////////////////////////////////////////////////////////////////
//找到文件夹中有效的文件数目,用递归的方法
VOID CPictureTrainingDlg::FindFileNumRecurse(CString pstr)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -