How to use _many_good_pts method in Airtest

Best Python code snippet using Airtest

sift_test.py

Source:sift_test.py Github

copy

Full Screen

...57 if mask is None:58 raise Exception("In _find_homography(), find no mask...")59 else:60 return M, mask61def _many_good_pts(im_source, im_search, kp_sch, kp_src, good):62 """特征点匹配点对数目>=4个,可使用单矩阵映射,求出识别的目标区域."""63 sch_pts, img_pts = np.float32([kp_sch[m.queryIdx].pt for m in good]).reshape(64 -1, 1, 2), np.float32([kp_src[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)65 # M是转化矩阵66 M, mask = _find_homography(sch_pts, img_pts)67 matches_mask = mask.ravel().tolist()68 # 从good中间筛选出更精确的点(假设good中大部分点为正确的,由ratio=0.7保障)69 selected = [v for k, v in enumerate(good) if matches_mask[k]]70 # 针对所有的selected点再次计算出更精确的转化矩阵M来71 sch_pts, img_pts = np.float32([kp_sch[m.queryIdx].pt for m in selected]).reshape(72 -1, 1, 2), np.float32([kp_src[m.trainIdx].pt for m in selected]).reshape(-1, 1, 2)73 M, mask = _find_homography(sch_pts, img_pts)74 # print(M, mask)75 # 计算四个角矩阵变换后的坐标,也就是在大图中的目标区域的顶点坐标:76 h, w = im_search.shape[:2]77 h_s, w_s = im_source.shape[:2]78 pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)79 dst = cv2.perspectiveTransform(pts, M)80 # trans numpy arrary to python list: [(a, b), (a1, b1), ...]81 def cal_rect_pts(dst):82 return [tuple(npt[0]) for npt in dst.astype(int).tolist()]83 pypts = cal_rect_pts(dst)84 # 注意:虽然4个角点有可能越出source图边界,但是(根据精确化映射单映射矩阵M线性机制)中点不会越出边界85 lt, br = pypts[0], pypts[2]86 middle_point = int((lt[0] + br[0]) / 2), int((lt[1] + br[1]) / 2)87 # 考虑到算出的目标矩阵有可能是翻转的情况,必须进行一次处理,确保映射后的“左上角”在图片中也是左上角点:88 x_min, x_max = min(lt[0], br[0]), max(lt[0], br[0])89 y_min, y_max = min(lt[1], br[1]), max(lt[1], br[1])90 # 挑选出目标矩形区域可能会有越界情况,越界时直接将其置为边界:91 # 超出左边界取0,超出右边界取w_s-1,超出下边界取0,超出上边界取h_s-192 # 当x_min小于0时,取0。 x_max小于0时,取0。93 x_min, x_max = int(max(x_min, 0)), int(max(x_max, 0))94 # 当x_min大于w_s时,取值w_s-1。 x_max大于w_s-1时,取w_s-1。95 x_min, x_max = int(min(x_min, w_s - 1)), int(min(x_max, w_s - 1))96 # 当y_min小于0时,取0。 y_max小于0时,取0。97 y_min, y_max = int(max(y_min, 0)), int(max(y_max, 0))98 # 当y_min大于h_s时,取值h_s-1。 y_max大于h_s-1时,取h_s-1。99 y_min, y_max = int(min(y_min, h_s - 1)), int(min(y_max, h_s - 1))100 # 目标区域的角点,按左上、左下、右下、右上点序:(x_min,y_min)(x_min,y_max)(x_max,y_max)(x_max,y_min)101 pts = np.float32([[x_min, y_min], [x_min, y_max], [102 x_max, y_max], [x_max, y_min]]).reshape(-1, 1, 2)103 pypts = cal_rect_pts(pts)104 return middle_point, pypts, [x_min, x_max, y_min, y_max, w, h]105# 匹配点对 >= 4个,使用单矩阵映射求出目标区域,据此算出可信度:106middle_point, pypts, w_h_range = _many_good_pts(im_source, im_search, kp_sch, kp_src, good)107print(middle_point)108print(pypts)109print(w_h_range)110# best_match = generate_result(middle_point, pypts, confidence)111#112# print("[sift] result=%s" % (best_match))113# matchesMask = [[0, 0] for i in range(len(matches))]114# coff = 0.2115# for i,(m,n) in enumerate(matches):116# if m.distance < coff * n.distance:117# matchesMask[i]=[1,0]118#119# print(matchesMask)120# draw_params = dict(matchColor = (0,255,0),...

Full Screen

Full Screen

sift.py

Source:sift.py Github

copy

Full Screen

...30 else:31 middle_point, pypts, w_h_range = _handle_three_good_points(im_source, im_search, kp_src, kp_sch, good)32 else:33 # 匹配点对 >= 4个,使用单矩阵映射求出目标区域,据此算出可信度:34 middle_point, pypts, w_h_range = _many_good_pts(im_source, im_search, kp_sch, kp_src, good)35 # 第四步:根据识别区域,求出结果可信度,并将结果进行返回:36 # 对识别结果进行合理性校验: 小于5个像素的,或者缩放超过5倍的,一律视为不合法直接raise.37 _target_error_check(w_h_range)38 # 将截图和识别结果缩放到大小一致,准备计算可信度39 x_min, x_max, y_min, y_max, w, h = w_h_range40 target_img = im_source[y_min:y_max, x_min:x_max]41 resize_img = cv2.resize(target_img, (w, h))42 confidence = _cal_sift_confidence(im_search, resize_img, rgb=rgb)43 best_match = generate_result(middle_point, pypts, confidence)44 print("[aircv][sift] threshold=%s, result=%s" % (threshold, best_match))45 return best_match if confidence >= threshold else None46def _get_key_points(im_source, im_search, good_ratio):47 """根据传入图像,计算图像所有的特征点,并得到匹配特征点对."""48 # 准备工作: 初始化sift算子...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Airtest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful