在矩形python中绘制图像

在矩形python中绘制图像,python,image,opencv,matplotlib,rectangles,Python,Image,Opencv,Matplotlib,Rectangles,我已经使用matplotlib绘制了一个矩形,并希望在其中放置一个图像,如下图所示。有人知道我如何用python实现这一点吗 您可以使用imshow将图像放置在给定位置。并添加一个变换,使图像具有与矩形相同的旋转 为了避免可能的版权问题,以下代码使用了(作者:Fernando Revilla)的图像: 导入matplotlib.pyplot作为plt 从matplotlib导入转换 从matplotlib.patches导入矩形 锉刀https://upload.wikimedia.org/wi

我已经使用matplotlib绘制了一个矩形,并希望在其中放置一个图像,如下图所示。有人知道我如何用python实现这一点吗


您可以使用
imshow
将图像放置在给定位置。并添加一个变换,使图像具有与矩形相同的旋转

为了避免可能的版权问题,以下代码使用了(作者:Fernando Revilla)的图像:

导入matplotlib.pyplot作为plt
从matplotlib导入转换
从matplotlib.patches导入矩形
锉刀https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Giant_Panda_Tai_Shan.JPG/1200px-Giant_Panda_Tai_Shan.JPG'
img=plt.imread(文件,格式='jpg')
图,ax=plt.子批次()
#假设在绘图上绘制了一个矩形
x、 y=20,30
宽度,高度=12,9
角度=70
矩形=矩形((x,y),宽度,高度,角度=角度,ec='黑色',fc='无',lw=3)
ax.add_补丁(rect)
#使用矩形位置和旋转绘制图像
tr=transforms.Affine2D().translate(-x,-y).旋转度(角度).translate(x,y)
imshow(img,extent=[x,x+宽度,y,y+高度],transform=tr+ax.transData)
ax.set_aspect('equal')#保持直角
ax.relim()
ax.自动缩放()
plt.show()

这里有一种使用Python/OpenCV/Numpy的方法。使用熊猫图像的4个角和矩形的4个角对其进行透视扭曲。然后对扭曲图像中的黑色多余区域进行遮罩。最后,使用遮罩混合扭曲图像和背景图像

输入:

图形图像:

将numpy导入为np
进口cv2
输入数学
#读取要处理的图像
img=cv2.imread(“panda.png”)
hh,ww=img.shape[:2]
#读取背景图像
bck=cv2.imread(“rectangle_graph.png”)
hhh,www=bck.shape[:2]
#按TL、TR、BR、BL的顺序指定img角点的坐标,作为x、y对
img_pts=np.float32([[0,0],[ww-1,0],[ww-1,hh-1],[0,hh-1])
#在背景图像中手动拾取矩形角点的坐标
bck_pts=np.float32([221245]、[333,26]、[503111]、[390331])
#计算透视矩阵
矩阵=cv2.getPerspectiveTransform(img\u pts,bck\u pts)
#打印(矩阵)
#将每个通道中的“黑色”和“近黑色”更改为“灰度1”,以便不显示任何值
#在随后的遮罩中,内部熊猫图像将为黑色

img[np.where((img)在图像周围放置边框,然后绘制旋转图像+边框对您有用吗?您在这方面帮了我很多,谢谢!
import numpy as np
import cv2
import math

# read image to be processed
img = cv2.imread("panda.png")
hh, ww = img.shape[:2]

# read background image
bck = cv2.imread("rectangle_graph.png")
hhh, www = bck.shape[:2]

# specify coordinates for corners of img in order TL, TR, BR, BL as x,y pairs
img_pts = np.float32([[0,0], [ww-1,0], [ww-1,hh-1], [0,hh-1]])

# manually pick coordinates of corners of rectangle in background image
bck_pts = np.float32([[221,245], [333,26], [503,111], [390,331]])

# compute perspective matrix
matrix = cv2.getPerspectiveTransform(img_pts,bck_pts)
#print(matrix)

# change black and near-black to graylevel 1 in each channel so that no values 
# inside panda image will be black in the subsequent mask
img[np.where((img<=[5,5,5]).all(axis=2))] = [1,1,1]

# do perspective transformation setting area outside input to black
img_warped = cv2.warpPerspective(img, matrix, (www,hhh), cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0,0,0))

# make mask for area outside the warped region
# (black in image stays black and rest becomes white)
mask = cv2.cvtColor(img_warped, cv2.COLOR_BGR2GRAY)
mask = cv2.threshold(mask, 0, 255, cv2.THRESH_BINARY)[1]
mask = cv2.merge([mask,mask,mask])
mask_inv = 255 - mask

# use mask to blend between img_warped and bck
result = ( 255 * (bck * mask_inv + img_warped * mask) ).clip(0, 255).astype(np.uint8)

# save images
cv2.imwrite("panda_warped.png", img_warped)
cv2.imwrite("panda_warped_mask.png", mask)
cv2.imwrite("panda_in_graph.png", result)

# show the result
cv2.imshow("warped", img_warped)
cv2.imshow("mask", mask)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()