2017-11-12 9 views
-1

私はいくつかの画像(例えば5)とそれぞれが異なる形をしています。プロジェクトレポートのために1つのイメージに連結したいと思っています。あなたはopencvとpythonを使って簡単な方法を提供できますか?pythonとopencvを使ってさまざまな形の画像を連結する方法は?

結果イメージは以下のようになります。

numpyでは、このようなものを試しましたが、2つの画像に対してのみ動作します。

r = np.concatenate((images[1][:, :, 1], images[1][:, :, 3]), axis=1) 

enter image description here

答えて

3

あなたはスクリーンショットに示した結果は、いくつかのより多くの工夫が必要になる場合があります取得、単にお互いの上に画像を積み重ねることは、このようacomplishedすることができます。

import cv2 
import numpy as np 

image_names = ['original_field_1_0.PNG','original_field_1_1.PNG','original_field_1_3.PNG','original_field_1_4.PNG','original_field_1_5.PNG'] 
images = [] 
max_width = 0 # find the max width of all the images 
total_height = 0 # the total height of the images (vertical stacking) 

for name in image_names: 
    # open all images and find their sizes 
    images.append(cv2.imread(name)) 
    if images[-1].shape[1] > max_width: 
     max_width = images[-1].shape[1] 
    total_height += images[-1].shape[0] 

# create a new array with a size large enough to contain all the images 
final_image = np.zeros((total_height,max_width,3),dtype=np.uint8) 

current_y = 0 # keep track of where your current image was last placed in the y coordinate 
for image in images: 
    # add an image to the final array and increment the y coordinate 
    final_image[current_y:image.shape[0]+current_y,:image.shape[1],:] = image 
    current_y += image.shape[0] 

cv2.imwrite('fin.PNG',final_image) 

基本的な考え方は、最初に画像の合計サイズを見つけ、次にそのサイズの配列を作成し、最終的にそれらの範囲のピクセルを個々の画像のピクセルに設定しながら下向きに(または望むものに応じて横向きに)繰り返します。

別の行または列を開始する場合のしきい値を実装することもできます。

0

私はそれを単純な関数にするためのコードを変更しました。他の人には役に立ちます。

def get_one_image(images): 
     img_list = [] 
     padding = 200 
     for img in images: 
      img_list.append(cv2.imread(img)) 
     max_width = [] 
     max_height = 0 
     for img in img_list: 
      max_width.append(img.shape[0]) 
      max_height += img.shape[1] 
     w = np.max(max_width) 
     h = max_height + padding 

     # create a new array with a size large enough to contain all the images 
     final_image = np.zeros((h, w, 3), dtype=np.uint8) 

     current_y = 0 # keep track of where your current image was last placed in the y coordinate 
     for image in img_list: 
      # add an image to the final array and increment the y coordinate 
      final_image[current_y:image.shape[0] + current_y, :image.shape[1], :] = image 
      current_y += image.shape[0] 
     cv2.imwrite('out.png', final_image) 
関連する問題