更新时间: 试题数量: 购买人数: 提供作者:

有效期: 个月

章节介绍: 共有个章节

收藏
搜索
题库预览
import cv2 Import numpy as np def find_objects(target_path, template_path, match_thresh=0.8, nms_thresh=0.3): # 读取图像和模板 origin = cv2.imread(target_path) target = cv2.cvtColor(origin, cv2.COLOR_BGR2GRAY) template = cv2.imread(template_path, 0) if template is None: raise FileNotFoundError(f"模板文件 {template_path} 不存在") H, w = template.shape[:2] # 模板匹配(使用归一化相关系数) res = cv2.matchTemplate(target, template, cv2.TM_CCOEFF_NORMED) # 获取候选匹配坐标 loc_y, loc_x = np.where(res >= match_thresh) scores = res[loc_y, loc_x] boxes = np.column_stack((loc_x, loc_y, np.full_like(loc_x, w), np.full_like(loc_y, h))) # 非极大值抑制(NMS) indices = cv2.dnn.NMSBoxes(boxes.tolist(), scores.tolist(), match_thresh, nms_thresh) # 转换索引格式 if isinstance(indices, tuple): indices = np.array(indices[0] if len(indices) > 0 else []) else: indices = np.array(indices) # 绘制检测结果 final_boxes = [] for i in indices: x, y, _, _ = boxes[i] final_boxes.append((x, y, x + w, y + h)) cv2.rectangle(origin, (x, y), (x + w, y + h), (0, 0, 255), 1) print(f"检测到 {len(final_boxes)} 个目标") cv2.imshow('Optimized Detection', origin) cv2.waitKey(0) cv2.destroyAllWindows() If __name__ == "__main__": find_objects('id.png', 'template/2.png', match_thresh=0.8, nms_thresh=0.3) (1)、h, w = template.shape[:2]中 h 和 w 的数值分别为______, ______。 其中,h 为 ______(A.目标图片的高,B.目标图片的宽,C.目标图片的水平坐标位置,D.目标图片的垂直坐标位置,E.模板图片的高,F.模板图片的宽) (2)、该程序中目标图片 id.png 的尺寸为宽______像素,高______像素。 Res = cv2.matchTemplate(target, template, cv2.TM_CCOEFF_NORMED) 中返回值 res 尺寸为宽______像素,高______像素。 目标图片宽度和 res 的宽度之差为______,两者的高度之差为______。 两者的宽度和高度之差与模板尺寸______(有,无)存在关联。 (3)、res = cv2.matchTemplate(target, template, cv2.TM_CCOEFF_NORMED) 在该项目的程序中,返回值 res 数组中的最大值为______。(四舍五入,取整数) 其中,数值越大表示______(A.目标区域与模板的像素分布高度相似;B.目标区域与模板无显著相关性。) (4)、loc_y, loc_x = np.where(res >= match_thresh) Loc_y 中有3个数字,分别为______, ______, ______(从小到大排列) Loc_x 中有3个数字,分别为______, ______, ______(从小到大排列) 以下哪项描述是正确的?______(不定项选择)A. loc_x 和 loc_y 分别表示满足条件的候选框在目标图像中的 x 和 y 坐标(左上角)。B. 该代码执行非极大值抑制(NMS)以去除重复的候选框。C. np.where 返回的是所有匹配得分大于等于 match_thresh 的位置坐标。D. loc_x 中的每个值对应候选框的宽度,loc_y 对应高度。 (5)、scores = res[loc_y, loc_x] 该项目程序中的 scores 数组有3个数,分别为______, ______, ______(四舍五入,保留1位小数) 以下哪项描述是正确的?______(不定项选择)A. scores 存储了所有候选框的宽度和高度信息。B. 这行代码通过非极大值抑制(NMS)过滤低分候选框。C. scores 中的每个值对应 loc_x 和 loc_y 位置的原始匹配得分。
图片 id_big.png 如下图所示。 (含图) 数字模板详见以下压缩包。 [template.zip] 理解以下程序中通过模板匹配识别数字的逻辑。 Import cv2 Import numpy as np Target0 = cv2.imread("id_big.png", 0) _, binary = cv2.threshold(target0, 50, 255, cv2.THRESH_BINARY) Cv2.imshow('binary', binary) Contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) Print(len(contours)) # contours中的轮廓数量是【第1空】个。 Boxes = [] For contour in contours: box = cv2.boundingRect(contour) boxes.append(box) Print(boxes) # boxes中记录着各个轮廓的外接矩形参数 # 其中第0个轮廓的外接矩形左上角y坐标是【第2空】 # 第0个轮廓的外接矩形的宽度是【第3空】,高度是【第4空】。 # 此时boxes中各个轮廓【第5空】(是,不是)依据x坐标从左到右排列的。 Sorted_boxes = sorted(boxes, key=lambda x: x[0]) Print(sorted_boxes) # sorted_boxes 中第0个轮廓的外接矩形左上角x坐标是【第6空】 # sorted_boxes 中第1个轮廓的外接矩形左上角x坐标是【第7空】 # 此时sorted_boxes中各个轮廓【第8空】(是,不是)依据x坐标从左到右排列的。 # 截取目标数字,存入 digits 列表 Digits = [] For (x, y, w, h) in sorted_boxes: digit = binary[y:y+h, x:x+w] digit = cv2.resize(digit, (30,50)) digits.append(digit) Cv2.imshow('digits[2]', digits[2]) # 以下说法正确的是【第9空】A. digits中存着各个数字轮廓外接矩形的参数B. digits中存着各个数字的图片 # 读取模板数字图片,存入 templates 列表 Templates = [] For i in range(10): template = cv2.imread('template/'+str(i)+'.png', 0) template = cv2.resize(template, (30,50)) templates.append(template) Cv2.imshow('templates[2]', templates[2]) Results = [] For digit in digits: max_score = -1 matched_num = -1 for i, template in enumerate(templates): res = cv2.matchTemplate(digit, template, cv2.TM_CCOEFF_NORMED) score = np.max(res) if score > max_score and score > 0.5: # 阈值过滤 max_score = score matched_num = i results.append(matched_num) Print(f"识别结果: {results}") # 上述程序共进行了【第10空】次模板匹配 Cv2.waitKey(0) Cv2.destroyAllWindows()
1