2017-02-23 35 views
0

我期待在OpenCV Python顯示一些圖像與每個子圖周圍的標題和邊框。像這樣(禮貌以下計算器帖子:OpenCV (Python) video subplots):OpenCV子圖與標題和空間周圍的空間

我想: 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的? 我知道一個解決方法是將文本放在圖像上,但這不是我想要的,因爲它會覆蓋重要信息。

UPDATE

我的壞,我起初並沒有具體說明:我有4個次要情節(SO 4不同的圖像),而不是兩個相似的例子。另外,我想解決儘可能快,因爲我有視頻(時間限制)

enter image description here

回答

2

我有一個非常快速和骯髒的解決方案。您可以對其進行優化以滿足您的需求。我旁邊的代碼的解釋,以及:

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

+0

希望這個答案幫助。您可以修改它以適應您的要求。 –

+1

一個小的評論。爲什麼不增加邊界的大小,例如'constant = cv2.copyMakeBorder(img1,100,10,10,10,cv2.BORDER_CONSTANT,value = black)'。這樣紫羅蘭色的矩形就不再需要了。我不尋找任何幻想,但爲快速和簡單:) – Roxanne

+0

正如我所說,你可以隨時修改它。您也可以調整文字的位置。 :D –

1

一般的想法是與width += width/10height += height/20創建一個新的形象。寫一些文字作爲標題,並把輸入圖像沿中心爲:

import cv2 
import numpy as np 


img = cv2.imread("/Users/anmoluppal/Downloads/Lenna.png") 

height, width, ch = img.shape 
new_width, new_height = width + width/20, height + height/8 

# Crate a new canvas with new width and height. 
canvas = np.ones((new_height, new_width, ch), dtype=np.uint8) * 125 

# New replace the center of canvas with original image 
padding_top, padding_left = 60, 10 
if padding_top + height < new_height and padding_left + width < new_width: 
    canvas[padding_top:padding_top + height, padding_left:padding_left + width] = img 
else: 
    print "The Given padding exceeds the limits." 

text1 = "Sample Image 1" 
text2 = "Sample Image 2" 
img1 = cv2.putText(canvas.copy(), text1, (int(0.25*width), 30), cv2.FONT_HERSHEY_COMPLEX, 1, np.array([255, 0, 0])) 
img2 = cv2.putText(canvas.copy(), text2, (int(0.25*width), 30), cv2.FONT_HERSHEY_COMPLEX, 1, np.array([255, 0, 0])) 

final = cv2.hconcat((img1, img2)) 
cv2.imwrite("./debug.png", final) 

enter image description here

+1

嘿,這是一個更優化的解決方案。偉大的工作夥伴!我的是相當快和骯髒:D –

+1

沒有煩惱,夥計,您的解決方案是使用內置的方法,如'cv2.copyMakeBorder'而不是數學,這對我來說似乎很好,感謝欣賞:) – ZdaR

+1

不要試圖卸載你在OpenCV社區的工作,研究代碼片段,並試圖讓它按照你的想法工作。 – ZdaR