Pythonはopencvまたはmatplotlibを使用して、1つのウィンドウに複数の画像を表示します



Python Uses Opencv Matplotlib Display Multiple Pictures One Window



たとえば、特定のフォルダに複数の画像がある場合、imshowに1枚ずつ表示するのではなく、ウィンドウに表示する必要があります。
または、複数のウィンドウを同時に開くのではなく、比較と表示のために1つのウィンドウに複数の画像を配置します。

単にcv2.imshowを使用して複数のウィンドウを開き、次のような複数の画像を表示する場合:



cv2.namedWindow('original_img', cv2.WINDOW_NORMAL) cv2.resizeWindow('original_img', 1000, 1000) cv2.imshow('original_img', original_img)

アイコン:
画像
次に、複数のウィンドウが異なる画像に対応していることを示しています。表示は私たちが望むものではありません。

2.opencvを使用して複数の画像を表示します

opencvを使用して、複数の画像を表示します。
簡単な例:



def opecv_muti_pic(): # figure 1 img = cv.imread('E:\tmp\cat1.jpg') # figure 2 img2 = cv.imread('E:\tmp\cat2.jpg') # Atlas imgs = np.hstack([img,img2]) # Show multiple cv.imshow('mutil_pic', imgs) #Wait to close cv.waitKey(0)

効果画像:
画像

3. python3 + opencv3を使用して、1つのウィンドウに複数の画像を表示します

python3 + opencv3バージョン:
(マルチピクチャー機能パッケージ)

import argparse import glob import cv2 import numpy as np import os # One window to display multiple pictures: python3 + opencv3 version. # The parameters passed in are: # 1. The collection of pictures (the size and the number of channels need to be consistent, otherwise the screen will be black) # 2. Want to display the size of a picture # 3. Picture interval size. # If there are too many pictures, too many pictures will be automatically omitted, which is not rigorous enough. def show_in_one(images, show_size=(500, 500), blank_size=2, window_name='merge'): small_h, small_w = images[0].shape[:2] column = int(show_size[1] / (small_w + blank_size)) row = int(show_size[0] / (small_h + blank_size)) shape = [show_size[0], show_size[1]] for i in range(2, len(images[0].shape)): shape.append(images[0].shape[i]) merge_img = np.zeros(tuple(shape), images[0].dtype) max_count = len(images) count = 0 for i in range(row): if count >= max_count: break for j in range(column): if count < max_count: im = images[count] t_h_start = i * (small_h + blank_size) t_w_start = j * (small_w + blank_size) t_h_end = t_h_start + im.shape[0] t_w_end = t_w_start + im.shape[1] merge_img[t_h_start:t_h_end, t_w_start:t_w_end] = im count = count + 1 else: break if count < max_count: print('The total number of pictures is: %s' % (max_count - count)) cv2.namedWindow(window_name) cv2.imshow(window_name, merge_img) if __name__ == '__main__': parser = argparse.ArgumentParser(description='Demonstrate mouse interaction with images') parser.add_argument('-i', '--input', help='Input directory.') args = parser.parse_args() path = args.input if path is None: # Modify the folder where your pictures are stored test_dir = '/home/xxy/PycharmProjects/different_ocr/Auto_Cutting/test' path = test_dir debug_images = [] for infile in glob.glob(os.path.join(path, '*.*')): ext = os.path.splitext(infile)[1][1:] # get the filename extenstion if ext == 'png' or ext == 'jpg' or ext == 'bmp' or ext == 'tiff' or ext == 'pbm': print(infile) img = cv2.imread(infile) if img is None: continue else: debug_images.append(img) show_in_one(debug_images) cv2.waitKey(0) cv2.destroyWindow()

効果画像:
画像
注意:



opencvは通常、1つのウィンドウに複数の画像を表示できますが、同じサイズで同じカラーチャネルの画像のみを一緒に表示できます。 (もちろん、画像を正規化するために画像の前処理を行うことができます。)

同じ画像、色、グレースケール画像など、複数の異なる画像をopencvフォームで表示する場合、それらをフォームに入れることはできません。

たとえば、異なるサイズまたは異なるカラーチャネルの画像が一緒に表示される場合、画面は黒になります。
画像

アピールの理由から、ほとんどの場合、matplotlibを使用してこのタスクを実行できます。

import cv2 import matplotlib.pyplot as plt # Use matplotlib to display multiple pictures def matplotlib_multi_pic1(): for i in range(9): img = cv2.imread('/home/xxy/PycharmProjects/different_ocr/Auto_Cutting/test/20171217171857880.png') title='title'+str(i+1) #Row, column, index plt.subplot(3,3,i+1) plt.imshow(img) plt.title(title,fontsize=8) plt.xticks([]) plt.yticks([]) plt.show() matplotlib_multi_pic1()

効果画像:
画像


上記は、Pythonでopencvまたはmatplotlibを使用して1つのウィンドウに複数の画像を表示する方法です。写真を簡単に比較できます。