美文网首页
Python 图片对比及标记

Python 图片对比及标记

作者: 是瑞瀛呀 | 来源:发表于2021-05-19 23:07 被阅读0次
import aircv
import cv2


class image_util:

    # 判断img_target是否在img_source内出现,若存在则提供在图片img_source中的位置
    def match_pic(self,img_source, img_target):
        """
        进行图片的匹配
        """
        imsrc = aircv.imread(img_source)
        imobj = aircv.imread(img_target)
        pos = aircv.find_template(imsrc, imobj, rgb=True, bgremove=True)
        if not pos:
            return None, None, None, None

        left_top = pos['rectangle'][0]  # 左上
        right_bottom = pos['rectangle'][3]  # 右下
        circle_center_pos = pos['result']  # 中心点坐标
        circle_center_pos = tuple(map(int, circle_center_pos))
        confidence = pos['confidence'] # 准确率
        print('准确率:', confidence)
        #print(circle_center_pos)  # 坐标位置
        return circle_center_pos, left_top, right_bottom, confidence

    # 在图片img_source内标记出 中心点为pos 左上点为left_top 右下点为right_bottom 的空心图形
    def draw_box(self,img_source, pos, left_top, right_bottom, circle_radius,  color, line_width):
        """
        将小图在大图中圈出来
        """
        if not pos:
            print('pos is null')
            return
        # 框出圆形
        cv2.circle(img_source, pos, circle_radius, color, line_width)
        # 框出矩形
        cv2.rectangle(img_source, left_top, right_bottom, color, line_width)  # 左上,右下
        # 创建窗口时候可以鼠标随意拖动窗口改变大小
        cv2.namedWindow("objDetect", 0)
        # 设置长宽大小为640*480
        cv2.resizeWindow("objDetect", 640, 480)
        # 移动窗口到(200,200)坐标
        cv2.moveWindow("objDetect", 200, 200)
        cv2.imshow('objDetect', imsrc)
        cv2.waitKey(0)
        cv2.destroyAllWindows()



if __name__ == "__main__":
    dir="D:/Program/Python/wg/yaomo"
    iu = image_util()
    img_source = dir+"/base.png"
    img_target = dir+"/gj.png"
    circle_center_pos, left_top, right_bottom, pos = iu.match_pic(img_source, img_target)
    print(circle_center_pos[0],circle_center_pos[1])
    # 画线的属性
    circle_radius = 40
    color = (255, 0, 0)
    line_width = 5
    imsrc = aircv.imread(img_source)
    iu.draw_box(imsrc, circle_center_pos, left_top, right_bottom, circle_radius, color, line_width)

相关文章

网友评论

      本文标题:Python 图片对比及标记

      本文链接:https://www.haomeiwen.com/subject/ifwmjltx.html