python叠加图像轮廓_大图像轮廓上的Python开放式CV叠加图像

我有一个可行的解决方案。希望这就是你想要的。在

代码:import cv2

import numpy as np

image = cv2.imread('C:/Users/524316/Desktop/shapes.png', 1)

monkey = cv2.imread('C:/Users/524316/Desktop/monkey.png', 1)

image2 = image.copy()

image3 = image.copy()

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

blurred = cv2.GaussianBlur(gray, (5, 5), 0)

thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]

#cv2.imshow('thresh', thresh)

_, cnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for c in cnts:

# making sure to avoid small unwanted contours -

if cv2.contourArea(c) > 150:

# - selecting contours having 5 sides -

if len(cv2.approxPolyDP(c, 0.04 * cv2.arcLength(c, True), True)) == 5:

cv2.drawContours(image2, [c], -1, (0, 255, 0), 2)

# - finding bounding box dimensions of the contour -

x, y, w, h = cv2.boundingRect(c)

print(x, y, w, h)

# - overlaying the monkey in place of pentagons using the bounding box dimensions -

image3[y:y+h, x:x+w] = cv2.resize(monkey, (np.abs(x - (x+w)), np.abs(y - (y+h))))

cv2.imshow('image2', image2)

cv2.imshow('image3', image3)

cv2.waitKey(0)

cv2.destroyAllWindows()

结果:

c8bca552c7eb90d8b09fe727bec81099.png

7a108c4739e93dd2713a84a5153de546.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值