【问题标题】:How to insert a picture in a circle using opencv?如何使用opencv在圆圈中插入图片?
【发布时间】:2023-02-25 02:52:10
【问题描述】:

我需要用我附加的第二张图片填充第一张图片的圆圈的白色部分,我怎么能在 opencv 中做到这一点? enter image description here

【问题讨论】:

  • 使用cv2.circle(image, center_coordinates, radius, color, thickness)geeksforgeeks.org/python-opencv-cv2-circle-method
  • 这回答了你的问题了吗? draw a circle over image opencv
  • 我已经画了一个圆圈,我需要在里面插入另一张图片
  • 哦,我现在明白了。您要生成的不是您发布的图片吗?那你为什么张贴你不想要的东西的照片?你想要的是用另一张图片填充那张图片的白色区域?所以请完全改变你的问题。您想要的是用第二张图像填充图像的白色像素。基本上你想给这只狗换一张脸。如果你这样解释会更清楚

标签: python opencv


【解决方案1】:

这是 Python/OpenCV 中的一种方法。

  • 读取每张图片
  • 将圆形图像转换为灰度
  • 阈值圆图像
  • 将第二张图片裁剪成第一张图片的大小
  • 使用阈值图像作为要使用的区域的选择器,将裁剪的第二幅图像与第一幅图像组合
  • 保存结果

输入 1:

输入 2:

import cv2
import numpy as np

# read image 1
img1 = cv2.imread('white_circle.png')
h1, w1 = img1.shape[:2]

# read image 2
img2 = cv2.imread('bear_heart.jpg')
h2, w2 = img2.shape[:2]

# convert img1 to grayscale
gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)

# threshold to binary
thresh = cv2.threshold(gray, 254, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)

# crop second image to size of first image
img2_crop = img2[0:h1, 0:w1]

# combine img1, img2_crop with threshold as mask
result = np.where(thresh==255, img2_crop, img1)

# save results
cv2.imwrite('white_circle_thresh.jpg', thresh)
cv2.imwrite('white_circle_bear_heart.jpg', result)

cv2.imshow("thresh", thresh)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

阈值图像:

结果图片:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多