gpt4 book ai didi

python - OpenCV 子图带有标题和边框周围的空间

转载 作者:太空宇宙 更新时间:2023-11-03 21:55:40 24 4
gpt4 key购买 nike

我希望在 OpenCV Python 中显示一些图像,每个子图都有标题和边框。像这样的事情(由以下 stackoverflow 帖子提供:OpenCV (Python) video subplots):

我想要什么: enter image description here

但我只能通过修改代码来实现这一点。

 import cv2
im1 = cv2.imread('Lenna.png')
final_frame = cv2.hconcat((im1, im1))
cv2.imshow('lena', final_frame)

我有什么 enter image description here

是否可以使用 OpenCV 获取此信息? 我知道一个解决方法是在图像上放置文本,但这不是我想要的,因为那样会覆盖重要信息。

更新

糟糕的是,我最初没有指定:我有 4 个子图(所以有 4 个不同的图像),而不是示例中的两个。另外,我希望解决方案尽可能快,因为我有视频(时间限制)

enter image description here

最佳答案

我有一个非常快速但肮脏的解决方案。您可以对其进行优化以满足您的需要。我在代码旁边也有解释:

import cv2
import numpy as np

img1 = cv2.imread('lena.jpg')

#--- Here I am creating the border---
black = [0,0,0] #---Color of the border---
constant=cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=black )
cv2.imshow('constant',constant)

enter image description here

您可以为不同的边框找到许多其他选项 ON THIS PAGE

#--- Here I created a violet background to include the text ---
violet= np.zeros((100, constant.shape[1], 3), np.uint8)
violet[:] = (255, 0, 180)

enter image description here

#--- I then concatenated it vertically to the image with the border ---

vcat = cv2.vconcat((violet, constant))
cv2.imshow('vcat', vcat)

enter image description here

#--- Now I included some text ---
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(vcat,'FRAME',(30,50), font, 2,(0,0,0), 3, 0)
cv2.imshow('Text', vcat)

enter image description here

#--- I finally concatenated both the above images horizontally---
final_img = cv2.hconcat((vcat, vcat))
cv2.imshow('Final', final_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

enter image description here

关于python - OpenCV 子图带有标题和边框周围的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42420470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com