通过的OpenCV算法CAMSHIFT控制鼠标指针(或鼠标如何基本功能) [英] Controlling mouse pointer through camshift algorithm of OpenCV (or how mouse basically functions)

查看:467
本文介绍了通过的OpenCV算法CAMSHIFT控制鼠标指针(或鼠标如何基本功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了使用EmguCV(OpenCV中的包装)在C#中的程序。该方案采用跟踪算法CAMSHIFT的对象。矩形围绕对象绘制。光标是由矩形的中心方向移动。输入是从摄像头拍摄。

I have written a program in C# using EmguCV (wrapper of openCV). The program tracks an object using camshift algorithm. A rectangle is drawn around the object. The cursor is moved by center of the rectangle. Input is taken from a webcam.

最初的问题是,光标不能覆盖整个屏幕。其运动被限制在帧大小。所以我应用了倍频系数:

Initially the problem was that the cursor couldn't cover the entire screen. Its movement was limited to frame size. And so i applied a multiplication factor:

屏幕宽度/ framewidth在X方向移动

screenwidth/framewidth for movement in X direction.

screenheight / frameheight在Y方向移动

screenheight/frameheight for movement in Y direction

通过这个鼠标覆盖整个区域。但是,鼠标移动不顺畅了。我无法指向位于接近两个图标。我怎样才能让鼠标移动顺畅,同时覆盖了整个屏幕,就像一个真正的老鼠会发生什么?

With this the mouse covers entire area. But the mouse movement is not smooth any more. I am unable to point to two icons closely situated. How can I make mouse movement smooth while covering the entire screen, just as it happens with a real mouse?

推荐答案

要指出显而易见的:的鼠标实现的非光滑来自事实由CAMSHIFT给出的矩形是只精确到一帧的像素的精度,所以可以最小运动将屏幕尺寸/框架尺寸四舍五入至最接近屏幕像素

To point out the obvious: the non-smoothness of your mouse implementation comes from the fact that the rectangle given by camshift is only accurate to the accuracy of one frame pixel, so the smallest movement possible will be screensize/framesize rounded to the nearest screen pixel.

如果即,某种指针加速度可以应用,就像当使用真实的,低质量的小鼠它完成的情况下(当然,在多个千dpi的激光的小鼠的情况下,没有必要对这样的东西)。基本上,光标在屏幕上移动的距离不是由指针输入采取的距离(在这种情况下,CAMSHIFT矩形位移),但是它的一个巧妙选择功能。因此,使用加速函数f(x),移动指针的步骤将是这样的:

If that is the case, some kind of pointer acceleration could be applied, just like it is done when real, low-quality mice are used (of course, in case of multiple thousand dpi laser mice, there is no need for such thing). Basically, the distance the cursor moves on screen is not the distance taken by the pointer input (in this case, camshift rectangle displacement), but a cleverly chosen function of it. So, using an acceleration function f(x), the steps of moving the pointer will be like this:


  1. 计算指针输入位移矢量,让我们通过来表示的 v

  2. 计算相应的单位长度矢量,让我们通过来表示的 U

  3. 屏幕上的指针位移的 v' = F(| v |) U

  1. Calculate vector of pointer input displacement, let that be denoted by v.
  2. Calculate the corresponding unit-length vector, let that be denoted by u.
  3. The on-screen pointer displacement is v'=f(|v|) * u

我选择了F(X)像的测试版的* E ^(字母<形式/ em>的* X - 1),其中0℃;的字母的和0℃; 测试&下; = 1是应被凭经验选择的参数

I'd chose f(x) in a form like beta * e^(alpha * x - 1), where 0 < alpha and 0 < beta <= 1 are parameters which should be empirically chosen.

基本上,任何功能会做认为具有1或更小的衍生物在0(允许您使用的输入精确的光标移动的全精度),趋于无穷大作为x的增加(大的动作应该对应光标的大规模迁移),是单调递增并具有单调增加的一阶导数。 修改这也是需要的加速功能具有0 0值,否则很奇怪的动作会发生。 :)

Basically, any function will do it that has a derivate of 1 or less at 0 (allows you to use the full accuracy of the input for precise cursor movements), goes to infinity as x increases (large movements should correspond to large movements of the cursor), is monotonically increasing and has a monotonically increasing first derivate. It is also needed that the acceleration function has a value of 0 at 0, otherwise very strange movements will happen. :)

也希望具有F( framewidth 的)= <青霉>屏幕宽度的,以便在整个运动被跟踪的对象在光标所在帧的结果在屏幕上移动。一个指数公式是相当令人愉悦的工作,但使用二次或更高次多项式可能变成是计算简单,这取决于性能要求有...

It is also desirable to have f(framewidth) = screenwidth so that moving the tracked object across the frame results in the cursor being moved across the screen. An exponential formula is quite pleasing to work with, but using a quadratic or higher degree polynomial may turn out to be computationally simpler, depending on what performance requirements are there...

这篇关于通过的OpenCV算法CAMSHIFT控制鼠标指针(或鼠标如何基本功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆