一:选取ROI与图像进行匹配
openv的aircv库中提供了图像匹配的方法:
以下是demo:
import cv2
import aircv as ac
def draw_rectangle(img, pos_1, pos_4, color, line_width):
cv2.rectangle(img, pos_1, pos_4, color, line_width)
cv2.imshow('objDetect', imsrc)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
imsrc = ac.imread('D:\\python_opencv\\source_image\\image_match\\1.jpg')
imobj = imsrc[248:328, 187:261] #第一个参数代表y方向,第二个参数代表x方向,因此在这里取的是(224±37, 288±40)
imsrc = ac.imread('D:\\python_opencv\\source_image\\image_match\\2.jpg')
# find the match position
pos = ac.find_template(imsrc, imobj)
print(pos)
circle_center_pos = pos['result']
color = (0, 255, 0)
line_width = 3
# draw rectangle
draw_rectangle(imsrc, (int(circle_center_pos[0])-50, int(circle_center_pos[1])-50), (int(circle_center_pos[0])+ 50, int(circle_center_pos[1])+50), (0, 255, 0), 2)
结果为:
以下是拍摄的一组照片,其中7、8、9为特意偏移一段距离后拍摄的。 可以得知对于照片1中所取的ROI(Region Of Interest),图片1-10所对应的坐标为:
1: (224.0, 288.0)
2: (228.0, 291.0)
3: (232.0, 292.0)
4: (228.0, 289.0)
5: (230.0, 291.0)
6: (232.0, 290.0)
7: (230.0, 292.0)
8: (259.0, 290.0)
9: (259.0, 286.0)
10: (265.0, 284.0)
但是假如所取区域为特征不明显区域结果会怎么样呢?
假如取的区域如下图所示:
那么得到的1-10图像匹配结果为:
1: (310.0, 240.0)
2: (314.0, 243.0)
3: (350.0, 229.0)
4: (313.0, 241.0)
5: (316.0, 242.0)
6: (339.0, 232.0)
7: (345.0, 248.0)
8: (35.0, 425.0)
9: (376.0, 233.0)
10:(384.0, 202.0)
容易出现很明显的误匹配,如图8与ROI匹配结果为: