将图像拼接在一起Opencv-Python
我的程序接收一幅图像,并根据比例参数将图像裁剪成单独的图像,例如,比例=3会生成9幅大小相等的图像。然后我计算出每个裁剪图像的平均rgb,并将图像中的所有像素值设置为平均rgb值 我想知道如何将裁剪后的图像粘在一起以输出一张图像?在这种情况下,它将是一个由九种不同颜色组成的网格 这是我的密码:将图像拼接在一起Opencv-Python,python,image,opencv,image-processing,computer-vision,Python,Image,Opencv,Image Processing,Computer Vision,我的程序接收一幅图像,并根据比例参数将图像裁剪成单独的图像,例如,比例=3会生成9幅大小相等的图像。然后我计算出每个裁剪图像的平均rgb,并将图像中的所有像素值设置为平均rgb值 我想知道如何将裁剪后的图像粘在一起以输出一张图像?在这种情况下,它将是一个由九种不同颜色组成的网格 这是我的密码: # import packages import numpy as np import cv2 import dateutil import llist from matplotlib import py
# 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)
正如它所代表的那样,嵌套for循环在图像上迭代,并按照我想要将它们缝合在一起的顺序输出图像(只是一种颜色的块),但我不确定如何实现这一点。我不知道OpenCV中是否存在这种情况,但在ImageMagick中,您可以简单地将图像大小调整为平铺大小(隐式平均像素)并将图像重新缩放到原始大小,无需插值-也称为最近邻重采样。如下所示:
# 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
根据您的原创作品,3x3变成:
convert face1.jpg -resize 3x3! -scale "${geom}"! out.jpg
3x5变成:
convert face1.jpg -resize 3x5! -scale "${geom}"! out.jpg
不要“缝合”-这更难。创建一个具有所需最终大小的新图像,然后使用(
cv2
图像只是numpy
a数组)将每个子图像复制到最终图像的适当区域中@goncalopp如何将每个子映像“复制”到新映像?它必须在for循环中进行,并且在每次迭代时,都会将一个新的子映像添加到输出映像中。因为您执行了crapped\u img=img[…]
然后对裁剪的图像进行操作,原来的img
将被更改。在numpy数组中切片不会创建新数组。因此,您的缝合应该已经完成。@HannesOvrén Genius!我怎么没看到,谢谢:)