简体   繁体   English

将图像拼接在一起Opencv -Python

[英]Stitching images together Opencv -Python

My program takes in an image and crops the image into seperate images according to the scale parameter, eg scale = 3 produces 9 images of equal size. 我的程序接收图像并根据比例参数将图像裁剪成单独的图像,例如scale = 3产生9个相同大小的图像。 I then work out mean rgb of each cropped image and set all pixel values in the image equal to the mean rgb value. 然后我计算出每个裁剪图像的平均rgb,并将图像中的所有像素值设置为等于平均rgb值。

I am wondering how I can stich the cropped images back together to output one image? 我想知道如何将裁剪后的图像重新组合在一起输出一张图像? Which in this case would be a grid of nine different colours. 在这种情况下,将是九种不同颜色的网格。

Here is my code: 这是我的代码:

# import packages
import numpy as np
import cv2
import dateutil
import llist
from matplotlib import pyplot as plt
import argparse

#Read in image
img = cv2.imread('images/0021.jpg')

scale = 3
#Get x and y components of image
y_len,x_len,_ = img.shape

mean_values = []
for y in range(scale):
    for x in range(scale):
        #Crop image 3*3 windows
        cropped_img=img[(y*y_len)/scale:((y+1)*y_len)/scale,
                          (x*x_len)/scale:((x+1)*x_len)/scale]

        mean_val=cv2.mean(cropped_img)
        mean_val=mean_val[:3]
        #Set cropped img pixels equal to mean RGB
        cropped_img[:,:,:] = mean_val

        cv2.imshow('cropped',cropped_img)
        cv2.waitKey(0)

        #Print mean_values array
        #mean_values.append([mean_val])
#mean_values=np.asarray(mean_values)
#print mean_values.reshape(3,3,3)

As it stands the nested for loop iterates over the image and outputs the images (which are just blocks of one colour) in the order that I want to stitch them together, but im not sure how to achieve this. 因为它代表嵌套的for循环迭代图像并输出图像(这只是一种颜色的块)按照我想要将它们拼接在一起的顺序,但我不知道如何实现这一点。

I don't know if such things exist in OpenCV, but in ImageMagick you can simply resize the image down to the tile-size (which will implicitly average the pixels) and the re-scale the image back up to the original size without interpolation - also called Nearest Neighbour Resampling . 我不知道OpenCV中是否存在这样的东西,但是在ImageMagick中你可以简单地将图像大小调整到图块大小(这将隐式平均像素)并将图像重新缩放到原始大小而不插值- 也称为最近邻重采样 Like this: 像这样:

在此输入图像描述

# Get original width and height
identify -format "%wx%h" face1.jpg
500x529

# Resize down to, say 10x10 and then back up to the original size
convert face1.jpg -resize 10x10! -scale "${geom}"! out.jpg

在此输入图像描述

Per your original, 3x3 becomes: 根据您的原件,3x3变为:

convert face1.jpg -resize 3x3! -scale "${geom}"! out.jpg

在此输入图像描述

and 3x5 becomes: 和3x5成为:

convert face1.jpg -resize 3x5! -scale "${geom}"! out.jpg

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM