?? imageobjectdlg.cpp
字號:
// ImageObjectDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ImageObject.h"
#include "ImageObjectDlg.h"
#include "DSStream.h"
#include "childfrm.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CImageObjectDlg dialog
long RoundFloat(float x)
{
float intptr ;
float fractional ;
fractional=modf(x,&intptr);
if(fractional<0.5)
{
return (long)intptr;
}else
{
return((long)intptr+1);
}
}
long RoundDouble(double x)
{
double intptr ;
double fractional ;
fractional=modf(x,&intptr);
if(fractional<0.5)
{
return (long)intptr;
}else
{
return((long)intptr+1);
}
}
CImageObjectDlg::CImageObjectDlg(CWnd* pParent /*=NULL*/)
: CDialog(CImageObjectDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CImageObjectDlg::OnCancel()
{
// DSStream_GetVideoStream(m_CardID,NULL,0);
::SendMessageA(m_lpShowVideoWnd->m_hWnd,WM_CLOSE,NULL,NULL);
KillTimer(m_GetImageTimer);
DSStream_DisconnectDevice( m_CardID);
::DestroyWindow(m_lpShowVideoWnd->m_hWnd);
::DeleteObject(m_Palette.m_hObject);
::DeleteObject(m_bmpCurve.m_hObject);
::DeleteObject(m_bmpCurve.m_hObject);
// delete m_regionImageData;
delete m_lpbmiVideo;
delete m_lpbmiCurve;
CDialog::OnCancel();
}
void CImageObjectDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_SLIDER_LIGHT, m_ImageLightSlider);
DDX_Control(pDX, IDC_SLIDER_CONTRAST, m_ImageContrastSlider);
DDX_Control(pDX, IDC_RE2_DETECT1_LIGHT, m_DetectRegion1Light);
DDX_Control(pDX, IDC_RE2_DETECT2_LIGHT, m_DetectRegion2Light);
DDX_Control(pDX, IDC_RE2_DETECT3_LIGHT, m_DetectRegion3Light);
DDX_Control(pDX, IDC_RE2_DETECT4_LIGHT, m_DetectRegion4Light);
DDX_Control(pDX, IDC_RE2_DETECT5_LIGHT, m_DetectRegion5Light);
DDX_Control(pDX, IDC_RE2_DETECT6_LIGHT, m_DetectRegion6Light);
DDX_Control(pDX, IDC_RE2_DETECT1_THRESHOLD, m_DetectRegion1Threshold);
DDX_Control(pDX, IDC_RE2_DETECT2_THRESHOLD, m_DetectRegion2Threshold);
DDX_Control(pDX, IDC_RE2_DETECT3_THRESHOLD, m_DetectRegion3Threshold);
DDX_Control(pDX, IDC_RE2_DETECT4_THRESHOLD, m_DetectRegion4Threshold);
DDX_Control(pDX, IDC_RE2_DETECT5_THRESHOLD, m_DetectRegion5Threshold);
DDX_Control(pDX, IDC_RE2_DETECT6_THRESHOLD, m_DetectRegion6Threshold);
DDX_Control(pDX, IDC_RE2_COUNTER, m_Counter);
DDX_Control(pDX, IDC_RE2_REGION_COUNT_SET, m_SetDetectNum);
DDX_Control(pDX, IDC_COMBO_SELECT_REGION, m_SelectDetectRegion);
DDX_Control(pDX, IDC_RE2_REGION_HEIGHT, m_SetDetectRegionHeight);
DDX_Control(pDX, IDC_RE2_REGION_WIDTH, m_SetDetectRegionWidth);
DDX_Control(pDX, IDC_RE2_REGION_THRESHOLD, m_SetDetectThreshold);
DDX_Control(pDX, IDC_BTN_SET, m_bntSetDetect);
DDX_Control(pDX, IDC_SHOW_IMAGE, m_ShowImage);
DDX_Control(pDX, IDC_SHOW_CURVE, m_CurveWindow);
}
BEGIN_MESSAGE_MAP(CImageObjectDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_EN_CHANGE(IDC_RE2_DETECT6_THRESHOLD, &CImageObjectDlg::OnEnChangeRe2Detect6Threshold)
ON_BN_CLICKED(IDC_BTN_SET, &CImageObjectDlg::OnBnClickedBtnSet)
ON_EN_CHANGE(IDC_RE2_REGION_COUNT_SET, &CImageObjectDlg::OnEnChangeRe2RegionCountSet)
ON_EN_KILLFOCUS(IDC_RE2_REGION_COUNT_SET, &CImageObjectDlg::OnEnKillfocusRe2RegionCountSet)
ON_CBN_SELCHANGE(IDC_COMBO_SELECT_REGION, &CImageObjectDlg::OnCbnSelchangeComboSelectRegion)
ON_MESSAGE(UM_PROCESS_IMAGE_DATA,&CImageObjectDlg::ImageProcess)
ON_WM_HSCROLL()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_EN_KILLFOCUS(IDC_RE2_REGION_HEIGHT, &CImageObjectDlg::OnEnKillfocusRe2RegionHeight)
ON_EN_KILLFOCUS(IDC_RE2_REGION_WIDTH, &CImageObjectDlg::OnEnKillfocusRe2RegionWidth)
ON_EN_KILLFOCUS(IDC_RE2_REGION_THRESHOLD, &CImageObjectDlg::OnEnKillfocusRe2RegionThreshold)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON_DEBUG, &CImageObjectDlg::OnBnClickedButtonDebug)
END_MESSAGE_MAP()
// CImageObjectDlg message handlers
BOOL CImageObjectDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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_CardID=1;
m_SetEnable=FALSE;
m_iDetectCountTotal=0;
m_iDetectCountTemp=0;
m_iCurrentDetectRegionID=0;
m_CurveSpeed=0;
m_indexofLightHist=0;
m_RegionPosiAdjust=FALSE;
{
this->GetParameter();
int k=m_SysSetPara.detect_num;
CString str;
for(int i=0;i<k;i++)
{
str.Format(_T("%d%s"),(i+1),_T("號檢測區域"));
this->m_SelectDetectRegion.InsertString(i,(LPCTSTR)str);
}
m_DetectRegion6Light.ShowWindow(SW_HIDE);
m_DetectRegion6Threshold.ShowWindow(SW_HIDE);
m_DetectRegion5Light.ShowWindow(SW_HIDE);
m_DetectRegion5Threshold.ShowWindow(SW_HIDE);
m_DetectRegion4Light.ShowWindow(SW_HIDE);
m_DetectRegion4Threshold.ShowWindow(SW_HIDE);
m_DetectRegion3Light.ShowWindow(SW_HIDE);
m_DetectRegion3Threshold.ShowWindow(SW_HIDE);
m_DetectRegion2Light.ShowWindow(SW_HIDE);
m_DetectRegion2Threshold.ShowWindow(SW_HIDE);
m_DetectRegion1Light.ShowWindow(SW_HIDE);
m_DetectRegion1Threshold.ShowWindow(SW_HIDE);
switch(k)
{
case 6:
m_DetectRegion6Light.ShowWindow(SW_SHOW);
m_DetectRegion6Threshold.ShowWindow(SW_SHOW);
case 5:
m_DetectRegion5Light.ShowWindow(SW_SHOW);
m_DetectRegion5Threshold.ShowWindow(SW_SHOW);
case 4:
m_DetectRegion4Light.ShowWindow(SW_SHOW);
m_DetectRegion4Threshold.ShowWindow(SW_SHOW);
case 3:
m_DetectRegion3Light.ShowWindow(SW_SHOW);
m_DetectRegion3Threshold.ShowWindow(SW_SHOW);
case 2:
m_DetectRegion2Light.ShowWindow(SW_SHOW);
m_DetectRegion2Threshold.ShowWindow(SW_SHOW);
case 1:
m_DetectRegion1Light.ShowWindow(SW_SHOW);
m_DetectRegion1Threshold.ShowWindow(SW_SHOW);
break;
default:
break;
}
this->m_ImageContrastSlider.SetRange(0,100);
this->m_ImageContrastSlider.SetPos(m_SysSetPara.image_contrast);
this->m_ImageLightSlider.SetRange(0,100);
this->m_ImageLightSlider.SetPos(m_SysSetPara.image_light);
this->m_SelectDetectRegion.EnableWindow(0);
this->m_SetDetectNum.EnableWindow(0);
this->m_SetDetectRegionHeight.EnableWindow(0);
this->m_SetDetectRegionWidth.EnableWindow(0);
this->m_SetDetectThreshold.EnableWindow(0);
this->m_ImageContrastSlider.EnableWindow(0);
this->m_ImageLightSlider.EnableWindow(0);
this->m_SelectDetectRegion.ShowWindow(SW_HIDE );
this->m_SetDetectNum.ShowWindow(SW_HIDE );
this->m_SetDetectRegionHeight.ShowWindow(SW_HIDE );
this->m_SetDetectRegionWidth.ShowWindow(SW_HIDE );
this->m_SetDetectThreshold.ShowWindow(SW_HIDE );
this->m_ImageContrastSlider.ShowWindow(SW_HIDE );
this->m_ImageLightSlider.ShowWindow(SW_HIDE );
this->m_ShowImage.SetWindowPos(NULL,5,5,IMAGE_WIDTH,IMAGE_HEIGHT,SWP_NOMOVE);
}
/////////////////////////////////////////////////////////////////////////////////
////////////創建8bit灰階調色板
LPLOGPALETTE pLogPal = (LPLOGPALETTE) new char[2 * sizeof(WORD) +256 * sizeof(PALETTEENTRY)];
// 設置邏輯調色板的信息
pLogPal->palVersion = 0x300;
pLogPal->palNumEntries = 256;
// 拷貝DIB中的顏色表到邏輯調色板
for(int i = 0; i < 256; i++) {
pLogPal->palPalEntry[i].peRed = i;
pLogPal->palPalEntry[i].peGreen = i;
pLogPal->palPalEntry[i].peBlue = i;
pLogPal->palPalEntry[i].peFlags = 0;
}
// 創建邏輯調色板
m_Palette.CreatePalette(pLogPal);
// 刪除臨時變量并返回TRUE
delete pLogPal;
////////////為位圖數據分配存儲空間
// m_bufferCurveBlock=new BYTE[256*100];
memset((BYTE*)m_bufferCurveBlock,240,256*100);
// m_bufferCurveHist=new BYTE[256*100];
BYTE *templp=(BYTE *)memset(m_bufferCurveHist,240,256*100);
// templp=(BYTE *)memset(m_bufferCurveHist,230,256*100);
// m_bufferVideo=(BYTE*)malloc(IMAGE_WIDTH*IMAGE_HEIGHT*6);
// m_bufferVideo=new BYTE[IMAGE_WIDTH*IMAGE_HEIGHT*6];
// templp=(BYTE *)memset(m_bufferVideo,10,IMAGE_WIDTH*IMAGE_HEIGHT*6);
////////////為DIB位圖信息初始化
m_lpbmiVideo=(BITMAPINFO*)new BYTE[sizeof(BITMAPINFOHEADER)];
m_lpbmiCurve=(BITMAPINFO *)new BYTE[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256];
BITMAPINFOHEADER bmi_header;
bmi_header.biSize=40;
bmi_header.biHeight=256;
bmi_header.biWidth=100;
bmi_header.biPlanes=1;
bmi_header.biBitCount=8;
bmi_header.biCompression=BI_RGB;
bmi_header.biSizeImage=25600;
bmi_header.biClrUsed=0;
bmi_header.biClrImportant=0;
bmi_header.biXPelsPerMeter=0;
bmi_header.biYPelsPerMeter=0;
::memcpy(m_lpbmiCurve,&bmi_header,40);
bmi_header.biSize=40;
bmi_header.biHeight=IMAGE_HEIGHT;
bmi_header.biWidth=IMAGE_WIDTH;
bmi_header.biPlanes=1;
bmi_header.biBitCount=24;
bmi_header.biCompression=BI_RGB;
bmi_header.biSizeImage=IMAGE_HEIGHT*IMAGE_WIDTH*3;
bmi_header.biClrUsed=0;
bmi_header.biClrImportant=0;
bmi_header.biXPelsPerMeter=0;
bmi_header.biYPelsPerMeter=0;
::memcpy(m_lpbmiVideo,&bmi_header,40);
RGBQUAD *lprgb=m_lpbmiCurve->bmiColors;
for(int i=0;i<256;i++)
{
lprgb[i].rgbBlue=i;
lprgb[i].rgbGreen =i;
lprgb[i].rgbRed =i;
lprgb[i].rgbReserved =i;
}
BOOL ret=m_bmpMain.CreateBitmap(IMAGE_WIDTH,IMAGE_HEIGHT,1,24,NULL);
CClientDC dc(this);//this
HBITMAP hnewbmp=CreateDIBSection(dc.m_hDC , // handle to DC
m_lpbmiVideo, // bitmap data
DIB_RGB_COLORS, // data type indicator
0, // bit values
NULL, // handle to file mapping object
0 // offset to bitmap bit values
);
m_bmpMain.DeleteObject();
m_bmpMain.Attach((HGDIOBJ)hnewbmp);
m_bmpCurve.CreateBitmap(100,256,1,8,NULL);
HBITMAP hnewvurve=CreateDIBSection( dc.m_hDC , // handle to DC
m_lpbmiCurve, // bitmap data
DIB_RGB_COLORS, // data type indicator
(void**)&m_bufferCurveBlock, // bit values
NULL, // handle to file mapping object
0 // offset to bitmap bit values
);
m_bmpCurve.DeleteObject();
m_bmpCurve.Attach((HGDIOBJ)hnewvurve);
// m_bmpCurve.DeleteObject();
// ret=m_bmpCurve.Attach((HGDIOBJ)hnewbmp);
// DeleteObject((HGDIOBJ)hnewbmp);
////////////////////////////////////////////////////////////////////////////
// m_regionImageData= new BYTE[MAX_REGION_WIDTH*MAX_REGION_HEIGHT];
// memset(m_regionImageData,0,MAX_REGION_WIDTH*MAX_REGION_HEIGHT);
for(int i=0;i<MAX_DETECT_REGION_NUM;i++)
{
m_CurveAverage[i]=0;
m_iRegionDetectStatu[i]=NO_OBJECT;
m_BrightnessAverage[i]=0;
// m_RegionLightHist[i]=new BYTE[100];
memset(m_RegionLightHist[i],0,100);
// m_VAverageLight[i]=new BYTE[MAX_REGION_WIDTH];
memset(m_VAverageLight[i],0,MAX_REGION_WIDTH);
}
for(int i=0;i<MAX_DETECT_REGION_NUM;i++)
{
DetectRegion[i]=m_SysSetPara.dtr[i].region;
}
////////////////////////////////////////////////////////////////////////////
InitVideoCaptureCard();
m_GetImageTimer=this->SetTimer(GET_IMAGE_EVENT,250,0);
/*
m_lpShowVideoWnd= new CChildFrame;
m_lpShowVideoWnd->CreateEx(WS_EX_OVERLAPPEDWINDOW ,
::AfxRegisterWndClass(0), // standard CMDIChildWnd class
_T("VideoShowWnd"), // caption of MDI child window
WS_CHILD | WS_VISIBLE , // window styles
rectDefault ,// ); // default rectangle size
NULL,
1);
m_lpShowVideoWnd->SetOwner(this);
*/
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CImageObjectDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
{
DisplayResult();
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CImageObjectDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CImageObjectDlg::OnEnChangeRe2Detect6Threshold()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
bool CImageObjectDlg::GetParameter()
{
FILE * fp;
char szFileName[20];
//從文件讀入數據
sprintf(szFileName, "c:\\detectconfig.cfg");
fp = fopen(szFileName, "rb");
if(!fp) return false;
int sz=fread(&m_SysSetPara, sizeof(SystemSetParameter), 1, fp);
if(sz==0)
{
m_SysSetPara.detect_num=3;
m_SysSetPara.image_contrast=50;
m_SysSetPara.image_light=50;
for(int i=0;i<6;i++)
{
m_SysSetPara.dtr[i].region.top=100+70*i;
m_SysSetPara.dtr[i].region.bottom=100+70*i+25;
m_SysSetPara.dtr[i].region.left=500;
m_SysSetPara.dtr[i].region.right=600;
m_SysSetPara.dtr[i].threshold=180;
}
this->SaveParameter();
}
fclose(fp);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -