首页 > 代码库 > 摄像头控制鼠标,机器视觉(opencv)
摄像头控制鼠标,机器视觉(opencv)
核心代码:
1 private void Process() 2 { 3 IplImage imgShow = new IplImage(new CvSize(mCvCapture.FrameWidth / 2, mCvCapture.FrameHeight / 2), BitDepth.U8, 3); 4 5 IplImage iplImgSrc =http://www.mamicode.com/ mCvCapture.QueryFrame(); 6 if (iplImgSrc =http://www.mamicode.com/= null) 7 { 8 Debug.WriteLine("CvCapture QueryFrame 取像为空。"); 9 return;10 }11 12 //反转13 Cv.Flip(iplImgSrc, iplImgSrc, FlipMode.Y);14 15 //缩小图片16 IplImage iplImgSrc2 = new IplImage(new CvSize(iplImgSrc.Width / 2, iplImgSrc.Height / 2), iplImgSrc.Depth, 3);17 iplImgSrc.Resize(iplImgSrc2);18 iplImgSrc =http://www.mamicode.com/ iplImgSrc2;19 20 iplImgSrc.Copy(imgShow);21 22 //图像预处理23 IplImage imgGray = new IplImage(iplImgSrc.GetSize(), BitDepth.U8, 1);24 iplImgSrc.CvtColor(imgGray, ColorConversion.RgbaToGray);25 //Cv.ReleaseImage(iplImgSrc);26 27 //imgGray.Smooth(imgGray, SmoothType.Gaussian, 5);28 //imgGray.Threshold(imgGray, 128, 255, ThresholdType.Binary);29 //Cv.Threshold(imgGray, imgGray, 100, 255, ThresholdType.Binary);30 31 iplImgSrc =http://www.mamicode.com/ imgGray;32 //----------------------------------------------------------------------33 34 #region 定位跟踪35 36 mWagSearchFlow.mInputImage = iplImgSrc;37 mWagSearchFlow.Run();38 39 #endregion40 41 //----------------------------------------------------------------------42 43 CvPoint cvPointFocusCenter = new CvPoint((int)mWagSearchFlow.mResultPoint[0].X,44 (int)mWagSearchFlow.mResultPoint[0].Y);45 46 //Cv.DrawCircle(imgShow, cvPointFocusCenter, 4, CvColor.Red);47 48 CvPoint2D32f calcMousePos = this.mPointImageToScreen.CalculateZoomPos(cvPointFocusCenter, this.mPointImageToScreen.mPointMouseSet);49 this.mPointImageToScreen.mPointMouseSet = calcMousePos;50 51 52 if (this.mPointImageToScreen.mbEnabledControlCursor)53 {54 WagPointImageToScreen.SetCursorPos((int)calcMousePos.X, (int)calcMousePos.Y);//控制鼠标位置55 56 }57 58 59 //|| mWagSearchFlow.Lost60 //wagDisplayPoint1.SetPaintHandler(WagPaintHandlerType.CenterSelect);61 62 bool bStop = this.mPointImageToScreen.CheckMoveStop();63 if (bStop)64 {65 this.mPointImageToScreen.mbEnabledControlCursor = false;66 mlFreeTimeStart = Environment.TickCount;67 }68 69 if (this.mbAutoControl)70 {71 if (!this.mPointImageToScreen.mbEnabledControlCursor)72 {73 long nDiff = Environment.TickCount - mlFreeTimeStart;74 if (nDiff > mlFreeTimeMax)75 {76 this.mPointImageToScreen.mbEnabledControlCursor = true;77 }78 }79 }80 81 this.Invoke(new Action(82 delegate83 {84 this.wagDisplayPoint1.UpdateImage(imgShow.ToBitmap());85 }));86 87 wagDisplayPoint1.SetPaintPoint((int)mWagSearchFlow.mResultPoint[0].X, (int)mWagSearchFlow.mResultPoint[0].Y);88 89 Cv.ReleaseImage(imgShow);90 91 //Cv.ReleaseImage(iplImgSrc);92 //Cv.ReleaseImage(null);93 94 Thread.Sleep(5);95 }
public void Run() { mInputImage.Copy(m_CvArr_Curr); if (m_CvArr_Prev == null) { throw new ArgumentNullException("m_CvArr_Prev"); } if (m_CvArr_Curr == null) { throw new ArgumentNullException("m_CvArr_Curr"); } if (m_CvPoint_PrevFeatures == null) { throw new ArgumentNullException("m_CvPoint_PrevFeatures"); } //CvWindow win = new CvWindow(imgGray); //win.ShowImage(imgGray); //System.Windows.Forms.Application.DoEvents(); //Thread.Sleep(1000000); //交换坐标 this.SwapPoints(ref this.m_CvPoint_CurrFeatures[0], ref this.m_CvPoint_PrevFeatures[0]); //执行 try { Cv.CalcOpticalFlowPyrLK( m_CvArr_Prev, //(01) 在时间 t 的第一帧。First frame, at time t. m_CvArr_Curr, //(02) 在时间 t + dt 的第二个帧。Second frame, at time t + dt. m_CvArr_PrevPyr, //(03) 金字塔的第一帧缓冲。Buffer for the pyramid for the first frame. If the pointer is not null , the buffer must have a sufficient size to store the pyramid from level 1 to level #level ; the total size of (image_width+8)*image_height/3 bytes is sufficient. m_CvArr_CurrPyr, //(04) 类似于 prevPyr,用于第二个帧。Similar to prev_pyr, used for the second frame. m_CvPoint_PrevFeatures, //(05) 流量需要被发现的点数组。Array of points for which the flow needs to be found. out m_CvPoint_CurrFeatures, //(06) 二维数组指向包含计算新职位的输入特征中第二个图像。Array of 2D points containing calculated new positions of input features in the second image. _sizeWin, //(07) 每个金字塔级别搜索窗口的大小。Size of the search window of each pyramid level. _nlevel, //(08) 最大的金字塔级别编号。如果为 0,则不使用金字塔 (单水平),如果为 1,两个级别使用,等等。Maximal pyramid level number. If 0 , pyramids are not used (single level), if 1 , two levels are used, etc. out status, //(09) Array. Every element of the array is set to 1 if the flow for the corresponding feature has been found, 0 otherwise. out trackError, //(10) Array of double numbers containing difference between patches around the original and moved points. Optional parameter; can be NULL . criteria, //(11) 指定何时应停止流动寻找每个点上每个金字塔级别的迭代过程。Specifies when the iteration process of finding the flow for each point on each pyramid level should be stopped. flags //(12) 混杂。Miscellaneous flags. ); } catch (Exception ex) { string sErr = "CalcOpticalFlowPyrLK : --> " + ex.Message; Debug.WriteLine(sErr); } Debug.WriteLine("追踪状态: --> " + status[0].ToString()); if (!mbLost && status[0] == 0) { bool bLost = false; bLost = true; if (bLost) { mbLost = true; Debug.WriteLine("失去焦点: ---> " + DateTime.Now.ToString("yyyyMMdd_HHmmss_ffff")); return; } } this.Limit(ref this.m_CvPoint_CurrFeatures[0], this.m_CvPoint_PrevFeatures[0]); CvPoint2D32f p = new CvPoint2D32f(this.m_CvPoint_CurrFeatures[0].X, this.m_CvPoint_CurrFeatures[0].Y); this.mResultPoint[0] = p; this.SwapImages(ref m_CvArr_Curr, ref m_CvArr_Prev); this.SwapImages(ref m_CvArr_CurrPyr, ref m_CvArr_PrevPyr); }
有兴趣的朋友可联系以获得源码。
摄像头控制鼠标,机器视觉(opencv)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。