版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、關(guān)于Touch Panel校準(zhǔn)算法的研究Haiyong Lei1. 概述由于存在機(jī)械誤差和ADC誤差, touch panel獲取的點(diǎn)坐標(biāo)與屏幕上的點(diǎn)坐標(biāo)總是存在偏差, 所以touch panel需要通過(guò)一定的軟件校準(zhǔn).2. 校準(zhǔn)算法目前普遍采用的是5點(diǎn)校準(zhǔn), 即取屏幕四角各一個(gè)點(diǎn)加上中心一個(gè)點(diǎn), 作為校準(zhǔn)的基準(zhǔn)參考, 如圖1.圖1: 校準(zhǔn)點(diǎn)的位子五點(diǎn)的坐標(biāo)定義為:左下: x0y0 (屏幕坐標(biāo)), X0Y0 (TP坐標(biāo))左上: x0y1 (屏幕坐標(biāo)), X0'Y1 (TP坐標(biāo))右下: x1y0 (屏幕坐標(biāo)), X1Y0' (TP坐標(biāo))右上: x1y1 (屏幕坐標(biāo)), X1
2、9;Y1' (TP坐標(biāo))中間: xcyc (屏幕坐標(biāo)), XcYc (TP坐標(biāo))2.1 算法1算法1基于如下公式把TP坐標(biāo)轉(zhuǎn)換成屏幕坐標(biāo):x_screen = Kx * (X_tp - X_offset) / SCF + x_offsety_screen = Ky * (Y_tp - Y_offset) / SCF + y_offset其中Kx,y是X/Y軸比例因子:Kx = (x1 - x0) / (X1 - X0) * SCFKy = (y1 - y0) / (Y1 - Y0) * SCFX_offset用于消除偏離誤差:x_offset = xcy_offset = ycX_of
3、fset = XcY_offset = YcSCF是SCALE FACTOR這里用于整數(shù)計(jì)算時(shí)保持計(jì)算精度。五個(gè)基準(zhǔn)點(diǎn)包含的冗余信息, 可用于排除人為誤差, 假定一個(gè)合理誤差sigma,則以下公式近式成立:|X0 - X0'| < sigma, |X1 - X1'| < sigma|Y0 - Y0'| < sigma, |Y1 - Y1'| < sigma|Xc - (X1 + X0 + X1' + X0') / 4| < sigma|Yc - (Y1 + Y0 + Y1' + Y0') / 4 |
4、< sigma校準(zhǔn)完成后,Kx,y和X/Y_offset四個(gè)參數(shù)予以永久保存。算法1的優(yōu)點(diǎn)是比較簡(jiǎn)單,缺點(diǎn)是無(wú)法消除TP屏傾斜誤差。對(duì)于精度要求不高場(chǎng)合是個(gè)合適的選擇。2.2 算法2算法2是tslib中采用的校準(zhǔn)算法, tslib是開(kāi)源TP算法庫(kù), 廣泛使用在各種touch panel應(yīng)用場(chǎng)合,特別是Android平臺(tái)。tslib包含多種算法, 校準(zhǔn)是其中之一, 具體公式如下:x_screen = (A0 * X_tp + A1 * Y_tp + A2) / A6y_screen = (A3 * X_tp + A4 * Y_tp + A5) / A6系數(shù)A0到A5經(jīng)計(jì)算得出, A6是SC
5、ALE FACTOR.算法2的優(yōu)點(diǎn)是效果最好,缺點(diǎn)是計(jì)算復(fù)雜,特別A0到A5的計(jì)算需要用到浮點(diǎn),如果處理器不支持浮點(diǎn)運(yùn)算的話,需要轉(zhuǎn)換為定點(diǎn)運(yùn)算。3 實(shí)現(xiàn)3.1 算法1/* * Copyright (C) 2015. * * Author: Haiyong Lei * */#ifndef TS_CALIB_H#define TS_CALIB_H/ DEBUG#define TS_DEBUG#ifdef TS_DEBUG#include <stdio.h>#define TS_LOG printf#else#define TS_LOG#endif#define N_CALIB_POI
6、NTS5 / number points for calibration/ NAME OF POINTS#define LEFT_BOTTOM0#define LEFT_TOP1#define RIGHT_BOTTOM2#define RIGHT_TOP3#define CENTER4/ FLIP FLAGS#define FLIP_X1#define FLIP_Y2/ ERRORS#define E_SUCCESS0#define E_TOO_MANY_POINTS-1#define E_BAD_POINT_X0-2#define E_BAD_POINT_X1-3#define E_BAD_
7、POINT_Y0-4#define E_BAD_POINT_Y1-5#define E_BAD_POINT_XCENTER-6#define E_BAD_POINT_YCENTER-7struct ts_calib_data int scr_xN_CALIB_POINTS;/ screen calibration pointsint scr_yN_CALIB_POINTS;int scr_xres;/ screen resolutionint scr_yres;int scr_xcenter;/ screen centerint scr_ycenter;int tp_xN_CALIB_POIN
8、TS;/ touch panel calibration pointsint tp_yN_CALIB_POINTS;int tp_xres;/ touch panel resolutionint tp_yres;int tp_xcenter;/ touch panel centerint tp_ycenter;int tp_xyflip;/ touch panel axis xy flip flagsint Kx;/ axis x ratioint Ky;/ axis y ratioint precision;/ calibration precisionint scaling;/ scali
9、ng factor;/* initialize touch panel calibration data: Params: scr_xres: screen x resolution scr_yres: screen i resolution tp_xres: tp x resolution tp_yres: tp y resolution Return:*/void ts_init(struct ts_calib_data *calib, int scr_xres, int scr_yres,int tp_xres, int tp_yres);/* set touch panel xy di
10、rection flip flags: Params: tp_xyflip: set the flag to flip Return:*/void ts_set_xyflip(struct ts_calib_data *calib, int tp_xyflip);/* reset touch panel xy direction flip flags: Params: tp_xyflip: set the flag to no flip Return:*/void ts_reset_xyflip(struct ts_calib_data *calib, int tp_xyflip);/* se
11、t calibration precision: Params: precision: the minimum in distance of two touch panel readingsthey can be considered the same. The unit is the touchpanel reading unit. Return:*/void ts_set_precision(struct ts_calib_data *calib, int precision);/* set calibration scaling: Params: scaling: the scaling
12、 factor, the default is 8192. Return:*/void ts_set_scaling(struct ts_calib_data *calib, int scaling);/* set calibration data: Params: Kx: the ratio of axis x. Ky: the ratio of axis y. tp_xcenter: touch panel center point's axis x. tp_ycenter: touch panel center point's axis y. Return:*/void
13、ts_set_calib_data(struct ts_calib_data *calib, int Kx, int Ky,int tp_xcenter, int tp_ycenter);/* smooth N of touch panel continous readings: Params: x_in: N x reading of touch panel. y_in: N y reading of touch panel. n: the number of N. x_out: smoothed x reading output. y_out: smoothed y reading out
14、put. Return:*/void ts_smooth_xy(int *x_in, int *y_in, int n, int* x_out, int* y_out);/* convert touch panel coordinate to screen using the calibration data: Params: tp_x: x reading of touch panel. tp_y: y reading of touch panel. scr_x: screen x position output. scr_y: screen y position output. Retur
15、n:*/void ts_linear_map_xy(struct ts_calib_data *calib, int tp_x, int tp_y, int *scr_x, int *scr_y);/* add calibration point Params: n: the Nth point used by calibration, N < 5. scr_x: x position in screen coordinate. scr_y: y position in screen coordinate. tp_x: x reading of touch panel. tp_y: y
16、reading of touch panel. Return: E_SUCCESS if succeed.*/int ts_add_calib_point(struct ts_calib_data *calib, int n, int scr_x, int scr_y, int tp_x, int tp_y);/* process calibration: Params: Return: E_SUCCESS if succeed. Note: use ts_add_calib_point to feed 5 points before calibration.*/int ts_calibrat
17、e(struct ts_calib_data *calib);#endif/* * Copyright (C) 2015. * * Author: Haiyong Lei * * The touchscreen calibration libray. * */#include <string.h>#include "ts_calib.h"#define PRECISION40/ related to touch panel resolution, / example: if touch panel resolution is 4096,/ the precisi
18、on is 40/4096.#define SCALING8192void ts_init(struct ts_calib_data *calib, int scr_xres, int scr_yres,int tp_xres, int tp_yres)TS_LOG("ts_calib> init with resultion: screen: W%dx,H%d, tp: W%dx,H%d",scr_xres, scr_yres, tp_xres, tp_yres);memset(calib, 0, sizeof (struct ts_calib_data);cali
19、b->scr_xres = scr_xres;calib->scr_yres = scr_yres;calib->scr_xcenter = scr_xres / 2;calib->scr_ycenter = scr_yres / 2;calib->tp_xres = tp_xres;calib->tp_yres = tp_yres;calib->tp_xcenter = tp_xres / 2;calib->tp_ycenter = tp_yres / 2;calib->Kx = scr_xres * SCALING / tp_xres;
20、calib->Ky = scr_yres * SCALING / tp_yres;calib->precision = PRECISION;calib->scaling = SCALING;void ts_set_xyflip(struct ts_calib_data *calib, int tp_xyflip)TS_LOG("ts_calib> set xyflip: %d",tp_xyflip);calib->tp_xyflip |= tp_xyflip;void ts_reset_xyflip(struct ts_calib_data *
21、calib, int tp_xyflip)TS_LOG("ts_calib> reset xyflip: %d",tp_xyflip);calib->tp_xyflip &= tp_xyflip;void ts_set_precision(struct ts_calib_data *calib, int precision)TS_LOG("ts_calib> set precision: %d",precision);calib->precision = precision;void ts_set_scaling(stru
22、ct ts_calib_data *calib, int scaling)TS_LOG("ts_calib> set scaling: %d",scaling);calib->scaling = scaling;void ts_set_calib_data(struct ts_calib_data *calib, int Kx, int Ky,int tp_xcenter, int tp_ycenter)TS_LOG("ts_calib> set Kx/Ky: %d,%d, center: X%d,Y%d",Kx, Ky, tp_xce
23、nter, tp_ycenter);calib->Kx = Kx;calib->Ky = Ky;calib->tp_xcenter = tp_xcenter;calib->tp_ycenter = tp_ycenter;/ smooth N points by averagingvoid ts_smooth_xy(int *x_in, int *y_in, int n, int* x_out, int* y_out)int x_sum = 0;int y_sum = 0;int i;/ average n samplesfor (i = 0; i < n; i+)
24、if (x_in)x_sum += *x_in +;if (y_in)y_sum += *y_in +;if (x_out)*x_out = x_sum / n;if (y_out)*y_out = y_sum / n;/ linear conversion:/ Xscreen = Kx * (Xtp - Xtp_center) / scaling + Xscreen_center/ Yscreen = Ky * (Ytp - Ytp_center) / scaling + Yscreen_center/ div by scaling because Kx/y is pre-scaled./v
25、oid ts_linear_map_xy(struct ts_calib_data *calib, int tp_x, int tp_y, int *scr_x, int *scr_y)if (calib->tp_xyflip & FLIP_X)tp_x = calib->tp_xres - tp_x;if (calib->tp_xyflip & FLIP_Y)tp_y = calib->tp_yres - tp_y;if (*scr_x)int x = (calib->Kx * (tp_x - calib->tp_xcenter) / ca
26、lib->scaling+ calib->scr_xcenter;/ boundary checkif (x < 0)x = 0;if (x > calib->scr_xres)x = calib->scr_xres;*scr_x = x;if (*scr_y)int y = (calib->Ky * (tp_y - calib->tp_ycenter) / calib->scaling+ calib->scr_ycenter;/ boundary checkif (y < 0)y = 0;if (y > calib-&g
27、t;scr_yres)y = calib->scr_yres;*scr_y = y;int ts_add_calib_point(struct ts_calib_data *calib, int n, int scr_x, int scr_y, int tp_x, int tp_y)if (n < 0 | n >= N_CALIB_POINTS)TS_LOG("ts_calib> error: %dth point is out of range.",n);return E_TOO_MANY_POINTS;if (calib->tp_xyfli
28、p & FLIP_X)tp_x = calib->tp_xres - tp_x;if (calib->tp_xyflip & FLIP_Y)tp_y = calib->tp_yres - tp_y;calib->scr_xn = scr_x;calib->scr_yn = scr_y;calib->tp_xn = tp_x;calib->tp_yn = tp_y;TS_LOG("ts_calib> add %dth point: screen: X%d,Y%d, tp: X%d,Y%d",n, scr_x, s
29、cr_y, tp_x, tp_y);return E_SUCCESS;/ variance(a0, a1, ., an) =/ 1/n * (a0 - a')2 + (a1 - a')2 + . (an - a')2,/ where, a' = (a0 + a1 + . an) / n/static int variance(int p0, int p1)int pt = (p0 + p1) /2;int v = (p0 - pt) * (p0 - pt) +(p1 - pt) * (p1 - pt);return v /2;/* * the calibrati
30、on procedure requires to collect five points: * * * (x0',y1 )-(x1',y1') * | | * | | * | | * | | * | | * | (x2,y2) | * | | * | | * | | * | | * | | * (x0 ,y0 )-(x1 ,y0') * * To linear convert touch panel coordinate to screen, we * use formula: * * x = Kx * (X - X2) / SCALING + x2 * y =
31、 Ky * (Y - Y2) / SCALING + x2 * * The ratio Kx,y can be computed by: * * Kx = (x1 - x0) / (X1 - X0) * SCALING * Ky = (y1 - y0) / (Y1 - Y0) * SCALING * * x, y: screen coordinates, X, Y: touch panel coordinates. * The SCALING factor is to keep calculation precision. * * The equation needs two points t
32、o solve, but we have five.The * redundant points can be used to valid points themselves, * points are considered enough good if: * * variance(X0, X0') < precision2 * variance(X1, X1') < precision2 * variance(Y0, Y0') < precision2 * variance(Y1, Y1') < precision2 * * and f
33、or the center point: * * X2' = (X1 + X0) + (X1' + X0') / 4 * Y2' = (Y1 + Y0) + (Y1' + Y0') / 4 * variance(X2, X2') < precision2 * variance(Y2, Y2') < precision2 * */int ts_calibrate(struct ts_calib_data *calib)int x00, x01;int x10, x11;int y00, y01;int y10, y11;
34、int x20, x21;int y20, y21;int scr_xdist;int scr_ydist;int tp_xdist;int tp_ydist;int v, square_prec = calib->precision * calib->precision;x00 = calib->tp_xLEFT_BOTTOM;x01 = calib->tp_xLEFT_TOP;x10 = calib->tp_xRIGHT_BOTTOM;x11 = calib->tp_xRIGHT_TOP;y00 = calib->tp_yLEFT_BOTTOM;y
35、01 = calib->tp_yRIGHT_BOTTOM;y10 = calib->tp_yLEFT_TOP;y11 = calib->tp_yRIGHT_TOP;if (v = variance(x00, x01) > square_prec)TS_LOG("ts_calib> error: bad point x0: %d,%d, variance: %d>%d.",x00, x01, v, square_prec);return E_BAD_POINT_X0;if (v = variance(x10, x11) > squar
36、e_prec)TS_LOG("ts_calib> error: bad point x1: %d,%d, variance: %d>%d.",x10, x11, v, square_prec);return E_BAD_POINT_X1;if (v = variance(y00, y01) > square_prec)TS_LOG("ts_calib> error: bad point y0: %d,%d, variance: %d>%d.",y00, y01, v, square_prec);return E_BAD_PO
37、INT_Y0;if (v = variance(y10, x11) > square_prec)TS_LOG("ts_calib> error: bad point y1: %d,%d, variance: %d>%d.",y10, y11, v, square_prec);return E_BAD_POINT_Y1;x20 = calib->tp_xCENTER;y20 = calib->tp_yCENTER;x21 = (x10 + x11 + x00 + x01) / 4;y21 = (y10 + y11 + y00 + y01) / 4
38、;if (v = variance(x20, x21) > square_prec)TS_LOG("ts_calib> error: bad point x2: %d,%d, variance: %d>%d.",x20, x21, v, square_prec);return E_BAD_POINT_XCENTER;if (v = variance(y20, y21) > square_prec)TS_LOG("ts_calib> error: bad point y2: %d,%d, variance: %d>%d.",y20, y21, v, square_prec);r
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 虛擬現(xiàn)實(shí)技術(shù)在腦癱患兒家庭教育中的應(yīng)用-洞察分析
- 文化差異對(duì)跨國(guó)公司客戶關(guān)系管理的影響-洞察分析
- 威脅狩獵與追蹤-洞察分析
- 云計(jì)算在商務(wù)服務(wù)中的創(chuàng)新應(yīng)用-洞察分析
- 提升柑橘生產(chǎn)效率研究-洞察分析
- 虛擬化天線技術(shù)研究-洞察分析
- 《服裝公司工作總結(jié)》課件
- 人工智能在工業(yè)互聯(lián)網(wǎng)中的應(yīng)用與創(chuàng)新
- 內(nèi)容創(chuàng)作者如何應(yīng)對(duì)信息過(guò)載風(fēng)險(xiǎn)
- 利用電影資源輔助提升學(xué)生的文學(xué)理解能力
- 合同工合同期滿考核表
- 口腔護(hù)理膏在治療口腔潰瘍的臨床研究
- 口腔門(mén)診部院內(nèi)管理規(guī)章制度匯編
- 水產(chǎn)養(yǎng)殖生產(chǎn)記錄表模板
- 部編版二年級(jí)數(shù)學(xué)上冊(cè)知識(shí)點(diǎn)匯總復(fù)習(xí)統(tǒng)編課件ppt
- 各種骨折英文名稱
- 機(jī)動(dòng)車排放檢驗(yàn)檢測(cè)方法內(nèi)部審批程序
- 加熱爐溫度控制系統(tǒng)
- 二次供水工程技術(shù)規(guī)程(CJJ140—2010 )
- 小說(shuō)HOTEL介紹
- MTK平臺(tái)modem配置
評(píng)論
0/150
提交評(píng)論