机器视觉(定位源码)_第1页
机器视觉(定位源码)_第2页
机器视觉(定位源码)_第3页
机器视觉(定位源码)_第4页
机器视觉(定位源码)_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

机器视觉定位识别源码(完全基于几何算法的定位,精度高、速度快、识别能力及抗干扰能力强;引用地址:qianli-sz注册会员下载VB,VC实现源码,其中对图片的处理代码)//KMDVisionDemoDlg.cpp:implementationfile//#include"stdafx.h"#include"KMDVisionDemo.h"#include"KMDVisionDemoDlg.h"#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#endif///////////////////////////////////////////////////////////////////////////////CAboutDlgdialogusedforAppAboutclassCAboutDlg:publicCDialog{public: CAboutDlg();//DialogData //{{AFX_DATA(CAboutDlg) enum{IDD=IDD_ABOUTBOX}; //}}AFX_DATA //ClassWizardgeneratedvirtualfunctionoverrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport //}}AFX_VIRTUAL//Implementationprotected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg():CDialog(CAboutDlg::IDD){ //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT}voidCAboutDlg::DoDataExchange(CDataExchange*pDX){ CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg,CDialog) //{{AFX_MSG_MAP(CAboutDlg) //Nomessagehandlers //}}AFX_MSG_MAPEND_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////////CKMDVisionDemoDlgdialogCKMDVisionDemoDlg::CKMDVisionDemoDlg(CWnd*pParent/*=NULL*/) :CDialog(CKMDVisionDemoDlg::IDD,pParent){ //{{AFX_DATA_INIT(CKMDVisionDemoDlg) m_Score=0; m_DownZoom=1.0f; m_UpZoom=1.0f; m_MaxAngle=45; m_MinAngle=-45; m_PyrLevel=1;m_MatchNum=1; m_pRawData=NULL; m_pDispData=NULL; m_hMVC1000=NULL;IsMatching=FALSE; IsCamera=FALSE; //}}AFX_DATA_INIT //NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32 m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);}voidCKMDVisionDemoDlg::DoDataExchange(CDataExchange*pDX){ CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CKMDVisionDemoDlg) DDX_Control(pDX,IDC_VIEWWND,m_ViewWnd); DDX_Control(pDX,IDC_SCROLLBARV,m_VScroBar); DDX_Control(pDX,IDC_SCROLLBARH,m_HScroBar); DDX_Text(pDX,IDC_MATCHNUM,m_MatchNum); DDV_MinMaxInt(pDX,m_MatchNum,1,80); DDX_Text(pDX,IDC_ZOOMBEDIT,m_UpZoom); DDV_MinMaxFloat(pDX,m_UpZoom,1.f,5.f); DDX_Text(pDX,IDC_ZOOMSEDIT,m_DownZoom); DDV_MinMaxFloat(pDX,m_DownZoom,0.1f,1.f); DDX_Text(pDX,IDC_SCORE,m_Score); DDV_MinMaxInt(pDX,m_Score,20,100); DDX_Text(pDX,IDC_PYRLEVEL,m_PyrLevel); DDV_MinMaxInt(pDX,m_PyrLevel,1,4); DDX_Text(pDX,IDC_MAXANGLEEDIT,m_MaxAngle); DDV_MinMaxInt(pDX,m_MaxAngle,-180,180); DDX_Text(pDX,IDC_MINANGLEEDIT,m_MinAngle); DDV_MinMaxInt(pDX,m_MinAngle,-180,180); //}}AFX_DATA_MAP}voidCALLBACKRawCallBack(LPVOIDlpParam,LPVOIDlpUser){ BYTE*pDataBuffer=(BYTE*)lpParam; CKMDVisionDemoDlg*pDemoDlg=(CKMDVisionDemoDlg*)lpUser; for(inti=0;i<480;i++) memcpy(pDemoDlg->m_pDispData+i*640,pDataBuffer+(479-i)*640,640); pDemoDlg->DrawImage(); }BEGIN_MESSAGE_MAP(CKMDVisionDemoDlg,CDialog) //{{AFX_MSG_MAP(CKMDVisionDemoDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_SETMODELAREA,OnSetmodelarea) ON_BN_CLICKED(IDC_OPENMODELIMAGE,OnOpenmodelimage) ON_BN_CLICKED(IDC_MAKEMODEL,OnMakemodel) ON_BN_CLICKED(IDC_OPENSEARCHIMAGE,OnOpensearchimage) ON_BN_CLICKED(IDC_SETSEARCHAREA,OnSetsearcharea) ON_BN_CLICKED(IDC_TRYMODEL,OnTrymodel) ON_WM_HSCROLL() ON_WM_VSCROLL() ON_WM_LBUTTONDOWN() ON_BN_CLICKED(IDC_OPENMODEL,OnOpenmodel) ON_EN_CHANGE(IDC_SCORE,OnChangeScore) ON_EN_CHANGE(IDC_ZOOMSEDIT,OnChangeZoomsedit) ON_EN_CHANGE(IDC_ZOOMBEDIT,OnChangeZoombedit) ON_EN_CHANGE(IDC_PYRLEVEL,OnChangePyrlevel) ON_BN_CLICKED(IDC_SAVEMODEL,OnSavemodel) ON_EN_CHANGE(IDC_MATCHNUM,OnChangeMatchnum) ON_BN_CLICKED(IDC_OPENCAMERA,OnOpencamera) ON_WM_TIMER() ON_BN_CLICKED(IDC_OPENMODELIMAGE2,OnOpenmodelimage2) ON_BN_CLICKED(IDC_DISPLAYREAL,OnDisplayreal) //}}AFX_MSG_MAPEND_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////////CKMDVisionDemoDlgmessagehandlersBOOLCKMDVisionDemoDlg::OnInitDialog(){ CDialog::OnInitDialog(); //Add"About..."menuitemtosystemmenu. //IDM_ABOUTBOXmustbeinthesystemcommandrange. ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX<0xF000); CMenu*pSysMenu=GetSystemMenu(FALSE); if(pSysMenu!=NULL) { CStringstrAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if(!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu); } } //Settheiconforthisdialog.Theframeworkdoesthisautomatically //whentheapplication'smainwindowisnotadialog SetIcon(m_hIcon,TRUE); //Setbigicon SetIcon(m_hIcon,FALSE); //Setsmallicon //TODO:Addextrainitializationhere CClientDCdc(this); m_dcMem.CreateCompatibleDC(&dc);m_VScroBar.ShowWindow(false);//HideVerticalScrollBarm_HScroBar.ShowWindow(false);//HideHorizontalScrollBar m_Score=60; m_MinAngle=-45; m_MaxAngle=45; m_DownZoom=0.8; m_UpZoom=1.2; m_MatchNum=1; m_hBmpNew=NULL; ImgWidth=640; ImgHeight=480; OriginCenterX=0; OriginCenterY=0; OriginCornerX=0; OriginCornerY=0; m_maxMatchNumbers=1; m_minMatchScore=m_Score; DownScale=m_DownZoom; UpScale=m_UpZoom; FinalScale=0; ModelOffsetX=0; ModelOffsetY=0; ResultX=0; ResultY=0;// FocusVisionModel=NULL;// FocusVisionModel = newCFocusVision; CStringss; ss.Format("%d",m_minMatchScore); GetDlgItem(IDC_SCORE)->SetWindowText(ss); ss.Format("%f",DownScale); GetDlgItem(IDC_ZOOMSEDIT)->SetWindowText(ss); ss.Format("%f",UpScale); GetDlgItem(IDC_ZOOMBEDIT)->SetWindowText(ss); ss.Format("%d",m_MinAngle); GetDlgItem(IDC_MINANGLEEDIT)->SetWindowText(ss); ss.Format("%d",m_MaxAngle); GetDlgItem(IDC_MAXANGLEEDIT)->SetWindowText(ss); ss.Format("%d",m_MatchNum); GetDlgItem(IDC_MATCHNUM)->SetWindowText(ss); m_ViewWnd.GetClientRect(&m_DisplayRect); initData(); initImageParam(); initcamera(); Start(); returntrue;//returnTRUEunlessyousetthefocustoacontrol}voidCKMDVisionDemoDlg::OnSysCommand(UINTnID,LPARAMlParam){ if((nID&0xFFF0)==IDM_ABOUTBOX) { CAboutDlgdlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID,lParam); }}//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,//thisisautomaticallydoneforyoubytheframework.voidCKMDVisionDemoDlg::OnPaint(){ if(IsIconic()) { CPaintDCdc(this);//devicecontextforpainting SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0); //Centericoninclientrectangle intcxIcon=GetSystemMetrics(SM_CXICON); intcyIcon=GetSystemMetrics(SM_CYICON); CRectrect; GetClientRect(&rect); intx=(rect.Width()-cxIcon+1)/2; inty=(rect.Height()-cyIcon+1)/2; //Drawtheicon dc.DrawIcon(x,y,m_hIcon); } else { CPaintDCdc(this); dc.BitBlt(offsetx,offsety,m_size.cx,m_size.cy,&m_dcMem,sourcex,sourcey,SRCCOPY); CDC*pDC=m_ViewWnd.GetDC(); PatKernal.Draw(pDC,0x00,0xff,0x00); SearchKernal.Draw(pDC,0x00,0xff,0x00); ReleaseDC(pDC); erase=false; CDialog::OnPaint(); }}//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags//theminimizedwindow.HCURSORCKMDVisionDemoDlg::OnQueryDragIcon(){ return(HCURSOR)m_hIcon;}voidCKMDVisionDemoDlg::OnDisplayreal(){ //TODO:Addyourcontrolnotificationhandlercodehere Start();}voidCKMDVisionDemoDlg::OnOpenmodelimage(){ //TODO:Addyourcontrolnotificationhandlercodehere MV_Usb2GetRawData(m_hMVC1000,&m_CapInfo); CStringstrFile="c:\\rawData.bmp"; SaveRawAsBmp(&m_CapInfo,strFile); Stop();////////////////////////////////////////////////////////// SearchKernal.m_rect.SetRect(0,0,0,0); PathName2="c:\\rawData.bmp"; ShowImage(); OpenModImg(PathName2.GetBuffer(50)); }voidCKMDVisionDemoDlg::OnOpenmodelimage2(){ //TODO:Addyourcontrolnotificationhandlercodehere Stop(); CStringsFilter=("图象文件(*.bmp)|*.bmp"); CFileDialogopendlg(TRUE,NULL,NULL, OFN_HIDEREADONLY|OFN_FILEMUSTEXIST,sFilter,this);if(opendlg.DoModal()==IDOK){ PathName2=opendlg.GetPathName(); } SearchKernal.m_rect.SetRect(0,0,0,0); ShowImage(); OpenModImg(PathName2.GetBuffer(50)); }voidCKMDVisionDemoDlg::OnSetmodelarea(){ //TODO:Addyourcontrolnotificationhandlercodehere SearchKernal.m_rect.SetRect(0,0,0,0); CDC*pDC=m_ViewWnd.GetDC(); PatKernal.m_rect.SetRect(10,10,180,120); PatKernal.m_nStyle=CRectTracker::solidLine; PatKernal.Draw(pDC,0x00,0xff,0x00); Invalidate(false); ReleaseDC(pDC); }voidCKMDVisionDemoDlg::OnMakemodel(){ //TODO:Addyourcontrolnotificationhandlercodehere SearchKernal.m_rect.SetRect(0,0,0,0); SetModAreas(PatKernal.m_rect.left+ModelOffsetX,PatKernal.m_rect.top+ModelOffsetY,PatKernal.m_rect.Width(),PatKernal.m_rect.Height()); PatKernal.m_rect.SetRect(0,0,0,0); Invalidate(false); BOOLrst=AddMod(0,60); if(!rst) MessageBox("几何特征过少,几何模板定制失败!程序执行灰度定位!"); SetMinAngles(-30); SetMaxAngles(30);}voidCKMDVisionDemoDlg::OnSavemodel(){ //TODO:Addyourcontrolnotificationhandlercodehere BOOLrst=SaveMod("c:\\KMDVi"); if(!rst) MessageBox("几何特征过少,保存灰度模板!");}voidCKMDVisionDemoDlg::OnOpenmodel(){ //TODO:Addyourcontrolnotificationhandlercodehere LoadMod("c:\\KMDVi");}voidCKMDVisionDemoDlg::OnOpencamera(){ //TODO:Addyourcontrolnotificationhandlercodehere IsCamera=TRUE; Start();}voidCKMDVisionDemoDlg::OnOpensearchimage(){ //TODO:Addyourcontrolnotificationhandlercodehere Stop(); IsCamera=FALSE; ModelOffsetX=0; ModelOffsetY=0; CStringsFilter=("图象文件(*.bmp)|*.bmp"); CFileDialogopendlg(TRUE,NULL,NULL, OFN_HIDEREADONLY|OFN_FILEMUSTEXIST,sFilter,this);if(opendlg.DoModal()==IDOK) PathName2=opendlg.GetPathName(); ShowImage(); OpenSearchImg(PathName2.GetBuffer(50));}voidCKMDVisionDemoDlg::OnSetsearcharea(){ //TODO:Addyourcontrolnotificationhandlercodehere CDC*pDC=m_ViewWnd.GetDC(); SearchKernal.m_rect.SetRect(10,10,620,460); SearchKernal.m_nStyle=CRectTracker::solidLine; SearchKernal.Draw(pDC,0x00,0xff,0x00); Invalidate(false); ReleaseDC(pDC); }voidCKMDVisionDemoDlg::OnTrymodel(){ //TODO:Addyourcontrolnotificationhandlercodehere if(IsCamera) { if(!IsMatching) { SetTimer(1,50,NULL); IsMatching=TRUE; } else { KillTimer(1); IsMatching=FALSE; Stop(); } } else { SetMinAngles(-60); SetMaxAngles(60); SearchKernalRect=SearchKernal.m_rect; MatchTime.Reset(); SetSearchAreas(SearchKernalRect.left,SearchKernalRect.top,SearchKernalRect.Width(),SearchKernalRect.Height()); //SetSearchAreas(10,10,620,460); RunSearchs(0); floatMatchSpeed=MatchTime.GetTime(TRUE); floatAngle=GetSearchAngles(0); intModelWidth=GetModWidths(); intModelHeight=GetModHeights(); for(inti=0;i<m_MatchNum;i++) { ResultX=GetSearchResultsX(i,"Null"); ResultY=GetSearchResultsY(i,"Null"); CStringss; ss.Format("定位时间Time=%f\n模板的中心位置为X=%f Y=%f\n定位后的中心位置为X=%fY=%f(单位:像素)Angle=%f\n分数是%d\n", MatchSpeed,GetModOrgCornerX(),GetModOrgCornerY(),ResultX,ResultY, Angle,GetSearchScores(i)); GetDlgItem(IDC_RESULTTEXT)->SetWindowText(ss); if(ResultX!=0&&ResultY!=0) DrawResult(ResultX-ModelOffsetX,ResultY-ModelOffsetY,GetModWidths(),GetModHeights()); } }}voidCKMDVisionDemoDlg::OnHScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar){ switch(nSBCode) { caseSB_TOP:sourcex=0;break; caseSB_BOTTOM:sourcex=INT_MAX;break; caseSB_THUMBTRACK:sourcex=nPos;break; } m_HScroBar.SetScrollPos(sourcex); ModelOffsetX=sourcex; erase=true; InvalidateRect(&rectStaticClient); CDialog::OnHScroll(nSBCode,nPos,pScrollBar); CDialog::OnVScroll(nSBCode,nPos,pScrollBar);}voidCKMDVisionDemoDlg::OnVScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar){ switch(nSBCode) { caseSB_TOP: sourcey=0; break; caseSB_BOTTOM: sourcey=INT_MAX; break; caseSB_THUMBTRACK: sourcey=nPos; break; } m_VScroBar.SetScrollPos(sourcey); ModelOffsetY=sourcey; erase=true; InvalidateRect(&rectStaticClient); CDialog::OnHScroll(nSBCode,nPos,pScrollBar); CDialog::OnVScroll(nSBCode,nPos,pScrollBar);}voidCKMDVisionDemoDlg::ShowImage(){ if(m_hBmpNew!=NULL)DeleteObject(m_hBmpNew); sourcex=sourcey=0; m_hBmpNew= (HBITMAP)LoadImage( AfxGetInstanceHandle(),//handletoinstance PathName2,//nameoridentifieroftheimage(rootiswhereprojectis) IMAGE_BITMAP,//imagetypes 0,//desiredwidth 0,//desiredheight LR_LOADFROMFILE ); if(m_hBmpNew==NULL) { return; } //puttheHBITMAPinfointotheCBitmap(butnotthebitmapitself) else { m_ViewWnd.GetClientRect(&rectStaticClient); rectStaticClient.NormalizeRect(); m_size.cx=rectStaticClient.Width();//zerobased m_size.cy=rectStaticClient.Height();//zerobased m_ViewWnd.ClientToScreen(&rectStaticClient); ScreenToClient(&rectStaticClient); m_pt.x=rectStaticClient.left; m_pt.y=rectStaticClient.top; GetObject(m_hBmpNew,sizeof(BITMAP),&m_bmInfo); ImgWidth=m_bmInfo.bmWidth; ImgHeight=m_bmInfo.bmHeight; VERIFY(m_hBmpOld=(HBITMAP)SelectObject(m_dcMem,m_hBmpNew)); offsetx=m_pt.x; offsety=m_pt.y; m_VScroBar.MoveWindow(offsetx+m_size.cx,offsety,16,m_size.cy); m_HScroBar.MoveWindow(offsetx,offsety+m_size.cy,m_size.cx,16); horz.cbSize=sizeof(SCROLLINFO); horz.fMask=SIF_ALL; horz.nMin=0; horz.nMax=m_bmInfo.bmWidth-m_size.cx; horz.nPage=0; horz.nPos=0; horz.nTrackPos=0; if(m_bmInfo.bmWidth<=m_size.cx) { offsetx=m_pt.x; m_VScroBar.MoveWindow(offsetx+m_bmInfo.bmWidth,offsety,16,m_size.cy); m_HScroBar.ShowWindow(false); } else m_HScroBar.ShowWindow(true); m_HScroBar.SetScrollInfo(&horz); vert.cbSize=sizeof(SCROLLINFO);vert.fMask=SIF_ALL; vert.nMin=0;vert.nMax=m_bmInfo.bmHeight-(m_size.cy); vert.nPage=0; vert.nTrackPos=0; if(m_bmInfo.bmHeight<=m_size.cy) { offsety=m_pt.y; m_HScroBar.MoveWindow(offsetx,offsety+m_bmInfo.bmHeight,m_size.cx,16); m_VScroBar.ShowWindow(false); } else m_VScroBar.ShowWindow(true); m_VScroBar.SetScrollInfo(&vert); InvalidateRect(&rectStaticClient); }}voidCKMDVisionDemoDlg::OnLButtonDown(UINTnFlags,CPointpoint){ CPointpt=point-rectStaticClient.TopLeft(); CPointTopLeft,BottomRight; CPointSTopLeft,SBottomRight; if(PatKernal.HitTest(pt)!=PatKernal.hitNothing) if(PatKernal.Track(&m_ViewWnd,pt)) { TopLeft=PatKernal.m_rect.TopLeft(); BottomRight=PatKernal.m_rect.BottomRight(); TopLeft.x+=rectStaticClient.TopLeft().x; TopLeft.y+=rectStaticClient.TopLeft().y; BottomRight.x=TopLeft.x+PatKernal.m_rect.Width(); BottomRight.y=TopLeft.y+PatKernal.m_rect.Height(); if(!(rectStaticClient.PtInRect(TopLeft)&&rectStaticClient.PtInRect(BottomRight))) PatKernal.m_rect.SetRect(10,10,180,120); InvalidateRect(&rectStaticClient,false); } if(SearchKernal.HitTest(pt)!=SearchKernal.hitNothing) if(SearchKernal.Track(&m_ViewWnd,pt)) { STopLeft=SearchKernal.m_rect.TopLeft(); SBottomRight=SearchKernal.m_rect.BottomRight(); STopLeft.x+=rectStaticClient.TopLeft().x; STopLeft.y+=rectStaticClient.TopLeft().y; SBottomRight.x=STopLeft.x+SearchKernal.m_rect.Width(); SBottomRight.y=STopLeft.y+SearchKernal.m_rect.Height(); if(!(rectStaticClient.PtInRect(STopLeft)&&rectStaticClient.PtInRect(SBottomRight))) SearchKernal.m_rect.SetRect(10,10,620,460); InvalidateRect(&rectStaticClient,false); } CDialog::OnLButtonDown(nFlags,point);}voidCKMDVisionDemoDlg::DrawResult(floatx,floaty,intWidth,intHeight){ CDC*pDC; pDC=m_ViewWnd.GetDC(); CPenNewPen(PS_SOLID,2,0x0000ff); pDC->SelectObject(&NewPen); pDC->MoveTo((int)x,(int)y+10); pDC->LineTo((int)x,(int)y-10); pDC->MoveTo((int)x+10,(int)y); pDC->LineTo((int)x-10,(int)y); pDC->MoveTo((int)(x-(Width-8)/2),(int)(y-(Height-8)/2)); pDC->LineTo((int)(x-(Width-8)/2),(int)(y+(Height-8)/2)); pDC->MoveTo((int)(x-(Width-8)/2),(int)(y-(Height-8)/2)); pDC->LineTo((int)(x+(Width-8)/2),(int)(y-(Height-8)/2)); pDC->MoveTo((int)(x+(Width-8)/2),(int)(y+(Height-8)/2)); pDC->LineTo((int)(x+(Width-8)/2),(int)(y-(Height-8)/2)); pDC->MoveTo((int)(x+(Width-8)/2),(int)(y+(Height-8)/2)); pDC->LineTo((int)(x-(Width-8)/2),(int)(y+(Height-8)/2)); pDC->SelectStockObject(BLACK_PEN); ReleaseDC(pDC);}voidCKMDVisionDemoDlg::OnChangeScore(){ UpdateData(TRUE); SetMinScores(m_Score);}voidCKMDVisionDemoDlg::OnChangeZoomsedit(){ UpdateData(TRUE); SetMinScales(m_DownZoom);}voidCKMDVisionDemoDlg::OnChangeZoombedit(){ UpdateData(TRUE); SetMaxScales(m_UpZoom);}voidCKMDVisionDemoDlg::OnChangePyrlevel(){UpdateData(TRUE); SetpyramidLevels(m_PyrLevel); }voidCKMDVisionDemoDlg::OnChangeMatchnum(){ UpdateData(TRUE); SetSearchNums(m_MatchNum); }voidCKMDVisionDemoDlg::OnOK(){ //TODO:Addextravalidationhere CDialog::OnOK();}voidCKMDVisionDemoDlg::initData(){ //allocmem DWORDdwRawDataSize=640*480;m_pRawData=(BYTE*)malloc(dwRawDataSize*sizeof(BYTE)); memset(m_pRawData,0,dwRawDataSize); m_pDispData=(BYTE*)malloc(dwRawDataSize*sizeof(BYTE)); memset(m_pDispData,0,dwRawDataSize); //初始化位图信息 m_pBmpInfo =(BITMAPINFO*)chBmpBuf; m_pBmpInfo->bmiHeader.biSize =sizeof(BITMAPINFOHEADER); m_pBmpInfo->bmiHeader.biWidth =640; m_pBmpInfo->bmiHeader.biHeight =480; m_pBmpInfo->bmiHeader.biBitCount =8; m_pBmpInfo->bmiHeader.biPlanes =1; m_pBmpInfo->bmiHeader.biCompression =BI_RGB; m_pBmpInfo->bmiHeader.biSizeImage =0; m_pBmpInfo->bmiHeader.biXPelsPerMeter =0; m_pBmpInfo->bmiHeader.biYPelsPerMeter =0; m_pBmpInfo->bmiHeader.biClrUsed =0; m_pBmpInfo->bmiHeader.biClrImportant =0; for(inti=0;i<256;i++) { m_pBmpInfo->bmiColors[i].rgbBlue =(BYTE)i; m_pBmpInfo->bmiColors[i].rgbGreen =(BYTE)i; m_pBmpInfo->bmiColors[i].rgbRed =(BYTE)i; m_pBmpInfo->bmiColors[i].rgbReserved =0; }}/*=====================================================================Initialvideoparameter =====================================================================*/voidCKMDVisionDemoDlg::initImageParam(){ memset(&m_CapInfo,0,sizeof(CapInfoStruct)); m_CapInfo.Buffer=m_pRawData; m_CapInfo.Width =640; m_CapInfo.Height =480; m_CapInfo.HorizontalOffset=0; m_CapInfo.VerticalOffset=0; m_CapInfo.Exposure=100; m_CapInfo.Gain[0]=17; m_CapInfo.Gain[1]=9; m_CapInfo.Gain[2]=15; m_CapInfo.Control=0; memset(m_CapInfo.Reserved,0,8); m_CapInfo.Reserved[0] =2; }voidCKMDVisionDemoDlg::initcamera(){ intnIndex=0; ULONGi=0; intrt=MV_Usb2Init("MVC-F",&nIndex,&m_CapInfo,&m_hMVC1000); if(ResSuccess!=rt) { AfxMessageBox("CannotopenUSBcamera!"); MV_Usb2Uninit(&m_hMVC1000); m_hMVC1000=NULL; return; } MV_Usb2SetOpMode(m_hMVC1000,0,FALSE); MV_Usb2SetRawCallBack(m_hMVC1000,RawCallBack,this);}voidCKMDVisionDemoDlg::Start(){ MV_Usb2StartCapture(m_hMVC1000,TRUE);}voidCKMDVisionDemoDlg::Stop(){ MV_Usb2StartCapture(m_hMVC1000,FALSE);}intCKMDVisionDemoDlg::SaveRawAsBmp(CapInfoStruct*pCapInfo,LPCTSTRszFileName){ BITMAPINFOHEADERFrameBmi; MakeBmpInfoHeader(pCapInfo,(BITMAPINFO*)(&FrameBmi)); DWORDdwRowLength=4*((FrameBmi.biWidth*8+31)/32); DWORDdwFrameSize=dwRowLength*FrameBmi.biHeight; FILE*fp; BITMAPFILEHEADERpf; fp=fopen(szFileName,"wb"); if(fp==NULL) return-1;pf.bfType=0x4d42; pf.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)+dwFrameSize;pf.bfReserved1=0;pf.bfReserved2=0;pf.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD); fwrite(&pf,sizeof(BYTE),sizeof(BITMAPFILEHEADER),fp); RGBQUAD*pPallete=(RGBQUAD*)newBYTE[256*sizeof(RGBQUAD)]; for(inti=0;i<256;i++) { pPallete[i].rgbRed=(BYTE)i; pPallete[i].rgbBlue=(BYTE)i; pPallete[i].rgbGreen=(BYTE)i; pPallete[i].rgbReserved=(BYTE)0; } fwrite(&FrameBmi,sizeof(BYTE),sizeof(BITMAPINFOHEADER),fp); fwrite(pPallete,sizeof(BYTE),256*sizeof(RGBQUAD),fp); fwrite(pCapInfo->Buffer,sizeof(BYTE),dwFrameSize,fp); deletepPallete; fclose(fp); return0;}intCKMDVisionDemoDlg::MakeBmpInfoHeader(CapInfoStruct*pCapInfo,BITMAPINFO*pBmpInfo){ UCHARbtScale=(pCapInfo->Control&0x18); intnScaleMode; switch(btScale) { case0:nScaleMode=1;break; case8:nScaleMode=4;break; case16:nScaleMode=8;break; case24:nScaleMode=2;break; default:nScaleMode=1;break; } pCapInfo->Width/=nScaleMode; pCapInfo->Height/=nScaleMode; pBmpInfo->bmiHeader.biSize=sizeof(BITMAPINFOHEADER); pBmpInfo->bmiHeader.biPlanes=1; pBmpInfo->bmiHeader.biCompression=BI_RGB; pBmpInfo->bmiHeader.biClrImportant=0; pBmpInfo->bmiHeader.biSizeImage=0; pBmpInfo->bmiHeader.biClrUsed=0; pBmpInfo->bmiHeader.biXPelsPerMeter=0; pBmpInfo->bmiHeader.biYPelsPerMeter=0; pBmpInfo->bmiHeader.biBitCount=8; pBmpInfo->bmiHeader.biWidth=pCapInfo->Width; pBmpInfo->bmiHeader.biHeight=pCapInfo->Height; return0;}voidCKMDVisionDemoDlg::OnTimer(UINTnIDEvent){ KillTimer(1); Start(); OpenSearchImgs(m_pDispData,640,480); SearchKernalRect=SearchKernal.m_rect; SetSearchAreas(SearchKernalRect.left,SearchKernalRect.top,SearchKernalRect.Width(),SearchKernalRect.Height()); //SetSearchAreas(0,0,620,460); MatchTime.Reset(); RunSearchs(0); floatMatchSpeed =MatchTime.GetTime(TRUE); floatAngledd=GetSearchAngles(0); for(inti=0;i<m_MatchNum;i++) { ResultX=GetSearchResultsX(i,"Null"); ResultY=GetSearchResultsY(i,"Null"); CStringss; ss.Format("模板的中心位置为X=%f Y=%f\n定位后的中心位置为X=%f Y=%f(单位:像素)\n分数是%d\n角度是%f\n定位时间%f" ,GetModOrgCenterX(),GetModOrgCenterY(),ResultX,ResultY,GetSearchScores(i),Angledd,MatchSpeed); GetDlgItem(IDC_RESULTTEXT)->SetWindowText(ss); if(ResultX!=0&&ResultY!=0) DrawResult(ResultX,ResultY,GetModWidths(),GetModHeights()); else { GetDlgItem(IDC_RESULTTEXT)->SetWindowText("定位失败!"); } } SetTimer(1,50,NULL); CDialog::OnTimer(nIDEvent);}voidCKMDVisionDemoDlg::DrawImage(){ CDC*pDC=GetDlgItem(IDC_VIEWWND)->GetDC(); CDCMemDC; CBitmapbm; MemDC.CreateCompatibleDC(pDC); bm.CreateCompatibleBitmap(pDC,640,480); CBitmap*pOldBm=MemDC.SelectObject(&bm); ::SetStretchBltMode(MemDC,COLORONCOLOR); ::StretchDIBits(MemDC,0,0, 640,480,0,0, 640,480, m_pDispData,m_pBmpInfo,DIB_RGB_COLORS,SRCCOPY); ShowResult(&MemDC); pDC->BitBlt(m_DisplayRect.left,m_DisplayRect.top, m_Displ

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论