如何使用python和opencv连接不同形状的图像?

时间:2017-11-12 21:39:00

标签: python numpy opencv matplotlib opencv3.0

我有一些图像(比如5个),每个图像都有不同的形状。我想连接到我的项目报告的单个图像。你能用opencv和python提供一个简单的方法吗?

生成的图像类似于以下内容。

在numpy中,我尝试了类似这样的东西,它只能用于两个图像。

<Route Path=“/someplace” component={someComponent} extra-prop-data={dataPassedToSomeComponent} />

enter image description here

3 个答案:

答案 0 :(得分:3)

获取您在屏幕截图中显示的结果可能需要更多修改,但只需将图像堆叠在彼此顶部就可以完成这样的操作:

String[] arr = {"a", "b", "c"};
System.out.println(Arrays.toString(arr)); 
// Output is: [a, b, c]

arr = Arrays.copyOf(arr, 10); // new size will be 10 elements
arr[3] = "d";
arr[4] = "e";
arr[5] = "f";

System.out.println(Arrays.toString(arr));
// Output is: [a, b, c, d, e, f, null, null, null, null]

基本思想是首先找到图像的总大小,然后创建一个大小的数组,最后将这些范围内的像素设置为每个单独图像的像素,同时向下(或横向,取决于你想要的东西) )。

您还可以为想要启动另一行或列的时间实现阈值。

答案 1 :(得分:1)

@ajayramesh 解决方案的这种修改对我有用。此函数接收图像列表并输出单个图像,其中所有输入图像垂直堆叠:

def get_one_image(img_list):
    max_width = 0
    total_height = 200  # padding
    for img in img_list:
        if img.shape[1] > max_width:
            max_width = img.shape[1]
        total_height += img.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 img_list:
        # add an image to the final array and increment the y coordinate
        image = np.hstack((image, np.zeros((image.shape[0], max_width - image.shape[1], 3))))
        final_image[current_y:current_y + image.shape[0], :, :] = image
        current_y += image.shape[0]
    return final_image

答案 2 :(得分: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)