問(wèn)題描述
我使用 EmguCV(openCV 的包裝器)在 C# 中編寫了一個(gè)程序.該程序使用 camshift 算法跟蹤對(duì)象.圍繞對(duì)象繪制一個(gè)矩形.光標(biāo)移動(dòng)到矩形的中心.輸入來(lái)自網(wǎng)絡(luò)攝像頭.
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.
最初的問(wèn)題是光標(biāo)無(wú)法覆蓋整個(gè)屏幕.它的運(yùn)動(dòng)僅限于框架尺寸.所以我應(yīng)用了一個(gè)乘法因子:
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:
X 方向移動(dòng)的屏幕寬度/幀寬度.
screenwidth/framewidth for movement in X direction.
Y 方向移動(dòng)的屏幕高度/幀高度
screenheight/frameheight for movement in Y direction
這樣鼠標(biāo)就可以覆蓋整個(gè)區(qū)域.但是鼠標(biāo)移動(dòng)不再流暢了.我無(wú)法指出靠近的兩個(gè)圖標(biāo).如何在覆蓋整個(gè)屏幕時(shí)使鼠標(biāo)移動(dòng)平滑,就像使用真正的鼠標(biāo)一樣?
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?
推薦答案
指出明顯的一點(diǎn): 你的鼠標(biāo)實(shí)現(xiàn)的不流暢是因?yàn)?camshift 給出的矩形只是精確到一幀像素的精度,因此可能的最小移動(dòng)將屏幕大小/幀大小四舍五入到最近的屏幕像素.
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.
如果是這種情況,可以應(yīng)用某種指針加速,就像使用真實(shí)的低質(zhì)量鼠標(biāo)時(shí)那樣(當(dāng)然,如果有數(shù)千個(gè) dpi 激光鼠標(biāo),則無(wú)需這樣的事情).基本上,光標(biāo)在屏幕上移動(dòng)的距離不是指針輸入所走的距離(在本例中為 camshift 矩形位移),而是它的一個(gè)巧妙選擇的函數(shù).因此,使用加速度函數(shù) f(x),移動(dòng)指針的步驟如下:
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:
- 計(jì)算指針輸入位移向量,記為v.
- 計(jì)算對(duì)應(yīng)的單位長(zhǎng)度向量,記為u.
- 屏幕指針位移為v'=f(|v|) * u
- Calculate vector of pointer input displacement, let that be denoted by v.
- Calculate the corresponding unit-length vector, let that be denoted by u.
- The on-screen pointer displacement is v'=f(|v|) * u
我會(huì)以 beta * e^(alpha * x - 1) 之類的形式選擇 f(x),其中 0 <alpha 和 0 <beta <= 1 是應(yīng)該根據(jù)經(jīng)驗(yàn)選擇的參數(shù).
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.
基本上,任何在 0 處導(dǎo)數(shù)為 1 或更少的函數(shù)都會(huì)執(zhí)行此操作(允許您使用輸入的完全準(zhǔn)確度來(lái)進(jìn)行精確的光標(biāo)移動(dòng)),隨著 x 的增加變?yōu)闊o(wú)窮大(大的移動(dòng)應(yīng)該對(duì)應(yīng)于大光標(biāo)的移動(dòng)),是單調(diào)遞增的,并且具有單調(diào)遞增的一階導(dǎo)數(shù).還需要加速函數(shù)在0時(shí)的值為0,否則會(huì)出現(xiàn)很奇怪的動(dòng)作.:)
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) = screenwidth,這樣在框架上移動(dòng)被跟蹤對(duì)象會(huì)導(dǎo)致光標(biāo)在屏幕上移動(dòng).指數(shù)公式很容易使用,但使用二次或更高次多項(xiàng)式可能會(huì)在計(jì)算上更簡(jiǎn)單,具體取決于那里的性能要求......
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...
這篇關(guān)于通過(guò) OpenCV 的 camshift 算法控制鼠標(biāo)指針(或鼠標(biāo)的基本功能)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!