【问题标题】:Put image on another image using python使用python将图像放在另一个图像上
【发布时间】:2020-11-15 13:54:09
【问题描述】:

您好,我正在使用 python 来跟踪视频中的对象,我想在对象顶部显示图像而不是文本。

我用来在目标框顶部显示文本的当前行:

cv2.putText(img_current_frame ,"object name",(int(bbox[0]), int(bbox[1]-45)),0, 0.75, (0,0,0),2)

【问题讨论】:

    标签: python-3.x opencv object-detection tracking cv2


    【解决方案1】:

    您可能更喜欢使用 PIL .text() 方法合成文本:

    from PIL import Image, ImageDraw
    img = Image.open("blank.png").convert("RGBA")
    d = ImageDraw.Draw(img)
    d.text((10, 10), "Hello world!")
    img.show()
    

    或者如果你有像素,那么使用alpha_composite()

    如果你坚持使用 cv2, 然后addWeighted() 将帮助您将带有 alpha 设置的徽标图像融入其中。 或者您可以只使用矩阵加法运算符:

    output = img_current_frame + logo
    

    【讨论】:

      【解决方案2】:

      已经有答案了:overlay a smaller image on a larger image python OpenCv

      在您的情况下,您可以使用以下内容包装此功能:

      def overlay_image(im1, im2, x_offset, y_offset):
          '''Mutates im1, placing im2 over it at a given offset.'''
          im1[y_offset:y_offset+im2.shape[0], x_offset:x_offset+im2.shape[1]] = im2
      

      然后调用它:

      im_over = cv2.imread("my_overlay_image.png")
      overlay_image(img_current_frame, im_over, 10, 10)
      

      (根据引用的解决方案)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-27
        相关资源
        最近更新 更多