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

下载本文档

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

文档简介

1、 机器视觉-定位识别源码(完全基于几何算法的定位, 精度高、速度快、识别能力及抗干扰能力强;引用地址:www.qianli- 注册会员下载VB,VC实现源码, 其中对图片的处理代码)/ KMDVisionDemoDlg.cpp : implementation file/#include "stdafx.h"#include "KMDVisionDemo.h"#include "KMDVisionDemoDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic cha

2、r THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic:CAboutDlg();/ Dialog Data/AFX_DATA(CAboutDlg)enum IDD = IDD_ABOUTBOX ;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataEx

3、change* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:/AFX_MSG(CAboutDlg)/AFX_MSGDECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAbout

4、Dlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/ CKMDVisionDemoDlg dialogCKMDVisionDemoDlg:CKMDVisionDemoDlg(CWnd* pParent /*=NULL*/): CDialog(CKMDVisionDemoDlg:IDD, pParent)/AFX_DATA_INIT(CKMDVisionDemoDlg)m_Score = 0;m

5、_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/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon

6、(IDR_MAINFRAME);void CKMDVisionDemoDlg: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_MatchN

7、um);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_PYRL

8、EVEL, 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_MAPvoid CALLBACK RawCallBack(LPVOID lpParam, LPVOID lpUser)BYTE

9、 *pDataBuffer = (BYTE*)lpParam;CKMDVisionDemoDlg *pDemoDlg = (CKMDVisionDemoDlg*)lpUser;for(int i=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

10、_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,

11、 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_SAVEMO

12、DEL, 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()/ CKMDVisionDemoDlg message handlersBOOL CKMDVisionDemoD

13、lg: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 strAbout

14、Menu;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 dialogSetI

15、con(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE);/ Set small icon/ TODO: Add extra initialization hereCClientDC dc(this);m_dcMem.CreateCompatibleDC( &dc ); m_VScroBar.ShowWindow(false); /Hide Vertical Scroll Bar m_HScroBar.ShowWindow(false); /Hide Horizontal Scroll Bar m_Score=60;m_MinAn

16、gle=-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;Resu

17、ltX=0;ResultY=0;/FocusVisionModel=NULL;/FocusVisionModel=new CFocusVision;CString ss;ss.Format("%d",m_minMatchScore);GetDlgItem(IDC_SCORE)->SetWindowText(ss);ss.Format("%f",DownScale);GetDlgItem(IDC_ZOOMSEDIT)->SetWindowText(ss);ss.Format("%f",UpScale);GetDlgItem(

18、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_V

19、iewWnd.GetClientRect(&m_DisplayRect); initData();initImageParam();initcamera();Start();return true ; / return TRUE unless you set the focus to a controlvoid CKMDVisionDemoDlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDial

20、og: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 CKMDVisionDemoDlg:OnPaint() if (IsIconic()CPaintDC dc(this); / dev

21、ice context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() -

22、cyIcon + 1) / 2;/ Draw the icondc.DrawIcon(x, y, m_hIcon);elseCPaintDC dc(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;CDia

23、log:OnPaint();/ The system calls this to obtain the cursor to display while the user drags/ the minimized window.HCURSOR CKMDVisionDemoDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;void CKMDVisionDemoDlg:OnDisplayreal() / TODO: Add your control notification handler code hereStart();void CKMDVisionDe

24、moDlg:OnOpenmodelimage() / TODO: Add your control notification handler code hereMV_Usb2GetRawData(m_hMVC1000, &m_CapInfo);CString strFile = "c:rawData.bmp"SaveRawAsBmp(&m_CapInfo, strFile );Stop();/SearchKernal.m_rect.SetRect(0,0,0,0);PathName2="c:rawData.bmp"ShowImage();

25、OpenModImg(PathName2.GetBuffer(50);void CKMDVisionDemoDlg:OnOpenmodelimage2() / TODO: Add your control notification handler code hereStop();CString sFilter = ("图象文件(*.bmp)|*.bmp");CFileDialog opendlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, sFilter, this); if( opendlg.DoModa

26、l ()=IDOK ) PathName2 = opendlg.GetPathName();SearchKernal.m_rect.SetRect(0,0,0,0);ShowImage();OpenModImg(PathName2.GetBuffer(50);void CKMDVisionDemoDlg:OnSetmodelarea() / TODO: Add your control notification handler code hereSearchKernal.m_rect.SetRect(0,0,0,0);CDC *pDC = m_ViewWnd.GetDC(); PatKerna

27、l.m_rect.SetRect(10,10,180,120);PatKernal.m_nStyle = CRectTracker:solidLine;PatKernal.Draw(pDC,0x00,0xff,0x00);Invalidate(false);ReleaseDC(pDC);void CKMDVisionDemoDlg:OnMakemodel() / TODO: Add your control notification handler code hereSearchKernal.m_rect.SetRect(0,0,0,0);SetModAreas(PatKernal.m_rec

28、t.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);BOOL rst=AddMod(0,60);if(!rst)MessageBox("几何特征过少,几何模板定制失败!程序执行灰度定位!");SetMinAngles(-30);SetMaxAngles(30);void CKMDVisionDemoDlg:OnSav

29、emodel() / TODO: Add your control notification handler code hereBOOL rst=SaveMod("c:KMDVi");if(!rst)MessageBox("几何特征过少,保存灰度模板!");void CKMDVisionDemoDlg:OnOpenmodel() / TODO: Add your control notification handler code hereLoadMod("c:KMDVi");void CKMDVisionDemoDlg:OnOpenc

30、amera() / TODO: Add your control notification handler code hereIsCamera=TRUE;Start();void CKMDVisionDemoDlg:OnOpensearchimage() / TODO: Add your control notification handler code hereStop();IsCamera=FALSE;ModelOffsetX=0;ModelOffsetY=0;CString sFilter = ("图象文件(*.bmp)|*.bmp");CFileDialog ope

31、ndlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, sFilter, this); if( opendlg.DoModal ()=IDOK )PathName2 = opendlg.GetPathName();ShowImage();OpenSearchImg(PathName2.GetBuffer(50);void CKMDVisionDemoDlg:OnSetsearcharea() / TODO: Add your control notification handler code hereCDC *pDC = m_V

32、iewWnd.GetDC(); SearchKernal.m_rect.SetRect(10,10,620,460);SearchKernal.m_nStyle = CRectTracker:solidLine;SearchKernal.Draw(pDC,0x00,0xff,0x00);Invalidate(false);ReleaseDC(pDC);void CKMDVisionDemoDlg:OnTrymodel() / TODO: Add your control notification handler code hereif(IsCamera)if(!IsMatching)SetTi

33、mer(1,50,NULL);IsMatching=TRUE;elseKillTimer(1);IsMatching=FALSE;Stop();elseSetMinAngles(-60);SetMaxAngles(60);SearchKernalRect=SearchKernal.m_rect;MatchTime.Reset();SetSearchAreas(SearchKernalRect.left,SearchKernalRect.top,SearchKernalRect.Width(),SearchKernalRect.Height();/SetSearchAreas(10,10,620

34、,460);RunSearchs(0);float MatchSpeed=MatchTime.GetTime(TRUE);float Angle= GetSearchAngles(0);int ModelWidth=GetModWidths();int ModelHeight=GetModHeights();for(int i=0;i<m_MatchNum;i+)ResultX=GetSearchResultsX(i,"Null");ResultY=GetSearchResultsY(i,"Null");CString ss;ss.Format(&

35、quot;定位时间Time=%fn模板的中心位置为 X=%fY=%fn定位后的中心位置为 X=%f Y=%f(单位:像素) Angle=%fn分数是%dn",MatchSpeed,GetModOrgCornerX(),GetModOrgCornerY(),ResultX,ResultY,Angle,GetSearchScores(i);GetDlgItem(IDC_RESULTTEXT)->SetWindowText(ss);if(ResultX!=0 && ResultY!=0)DrawResult(ResultX-ModelOffsetX, ResultY-

36、ModelOffsetY,GetModWidths(),GetModHeights();void CKMDVisionDemoDlg:OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) switch (nSBCode)case SB_TOP:sourcex = 0;break;case SB_BOTTOM:sourcex = INT_MAX;break;case SB_THUMBTRACK:sourcex= nPos;break;m_HScroBar.SetScrollPos(sourcex);ModelOffsetX=sour

37、cex;erase=true;InvalidateRect(&rectStaticClient);CDialog:OnHScroll(nSBCode, nPos, pScrollBar);CDialog:OnVScroll(nSBCode, nPos, pScrollBar);void CKMDVisionDemoDlg:OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) switch (nSBCode)case SB_TOP:sourcey = 0;break;case SB_BOTTOM:sourcey = INT_

38、MAX;break;case SB_THUMBTRACK:sourcey = nPos;break;m_VScroBar.SetScrollPos(sourcey);ModelOffsetY=sourcey;erase=true;InvalidateRect(&rectStaticClient);CDialog:OnHScroll(nSBCode, nPos, pScrollBar);CDialog:OnVScroll(nSBCode, nPos, pScrollBar);void CKMDVisionDemoDlg:ShowImage()if(m_hBmpNew != NULL) D

39、eleteObject(m_hBmpNew);sourcex=sourcey=0;m_hBmpNew =(HBITMAP) LoadImage(AfxGetInstanceHandle(), / handle to instancePathName2, / name or identifier of the image (root is where project is)IMAGE_BITMAP, / image types0, / desired width0, / desired heightLR_LOADFROMFILE); if( m_hBmpNew = NULL ) return;/

40、 put the HBITMAP info into the CBitmap (but not the bitmap itself)elsem_ViewWnd.GetClientRect( &rectStaticClient );rectStaticClient.NormalizeRect();m_size.cx = rectStaticClient.Width(); / zero basedm_size.cy = rectStaticClient.Height(); / zero basedm_ViewWnd.ClientToScreen( &rectStaticClient

41、 );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_p

42、t.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&

43、lt;=m_size.cx) offsetx= m_pt.x;m_VScroBar.MoveWindow(offsetx+m_bmInfo.bmWidth,offsety,16,m_size.cy);m_HScroBar.ShowWindow(false);elsem_HScroBar.ShowWindow(true);m_HScroBar.SetScrollInfo(&horz);vert.cbSize = sizeof(SCROLLINFO);vert.fMask = SIF_ALL;vert.nMin = 0;vert.nMax = m_bmInfo.bmHeight-(m_si

44、ze.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);elsem_VScroBar.ShowWindow(true);m_VScroBar.SetScrollInfo(&vert);InvalidateRect(&rectStaticClient);void

45、CKMDVisionDemoDlg:OnLButtonDown(UINT nFlags, CPoint point) CPoint pt =point-rectStaticClient.TopLeft();CPoint TopLeft,BottomRight;CPoint STopLeft,SBottomRight;if (PatKernal.HitTest(pt) != PatKernal.hitNothing )if (PatKernal.Track(&m_ViewWnd, pt)TopLeft=PatKernal.m_rect.TopLeft();BottomRight=PatK

46、ernal.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)PatKe

47、rnal.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.TopLef

48、t().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);Invalidate

49、Rect(&rectStaticClient, false);CDialog:OnLButtonDown(nFlags, point);void CKMDVisionDemoDlg:DrawResult(float x,float y,int Width,int Height)CDC* pDC;pDC=m_ViewWnd.GetDC();CPen NewPen(PS_SOLID,2,0x0000ff);pDC->SelectObject(&NewPen);pDC->MoveTo(int)x,(int)y+10);pDC->LineTo(int)x,(int)y

50、-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

51、->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);void CKMDVisionDemoDlg:OnChangeScore() UpdateData(TRUE);SetMinScores(m_Score);void CKMDVisionDemoDlg:OnChangeZoomsedit() UpdateData(TRUE);SetMi

温馨提示

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

评论

0/150

提交评论