简体   繁体   English

大图像轮廓上的Python Open CV覆盖图像

[英]Python Open CV overlay image on contour of a big image

I have a big image of shape where i'm looking to overlap an image on a shape based on the contour 我有一个很大的形状图像,我想在基于轮廓的形状上重叠图像

I have this image 我有这张图片 在此处输入图片说明

I have this contur where it is detecting the shape and color of the image 我在检测图像的形状和颜色的地方

在此处输入图片说明

I want to place an image on this shape 我想在此形状上放置图像

I want to custom resize the image 我想自定义调整图像大小

在此处输入图片说明

Out Put Desired:- 期望的输出:-

在此处输入图片说明

Code:- 码:-

if color == "blue" and shape == "pentagon":
    A_img = cv2.imread("frame.png")
    print(A_img)
    x_offset=y_offset=50
    B_img[y_offset:y_offset+A_img.shape[0], x_offset:x_offset+A_img.shape[1]] = A_img

That code is not working the monkey is getting printed on the top left 该代码不起作用,猴子被打印在左上方

I have a working solution. 我有一个可行的解决方案。 Hope it is what you were looking for. 希望这是您想要的。

Code: 码:

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()

Result: 结果:

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM