本文介绍了大图像轮廓上的Python Open CV覆盖图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个很大的形状图像,我希望将图像重叠在基于轮廓的形状上
I have a big image of shape where i'm looking to overlap an image on a shape based on the contour
我有这张图片
我在此Contur中检测图像的形状和颜色
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
需要的输出权:-
代码:-
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.
代码:
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()
结果:
这篇关于大图像轮廓上的Python Open CV覆盖图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!