使用opencv以特定图案放大图像

问题描述 投票:0回答:1

我正在尝试使用 opencv 以特定模式调整图像大小,但遇到一些问题。

所以这是我写的代码:

x1 = cv2.resize(x, (1500, 1600), interpolation = cv2.INTER_AREA)

以下是我的输入图像:

这就是我得到的:

虽然我需要这样的东西:

那么实现这一目标的最佳方法是什么?

谢谢。

python opencv image-processing image-resizing
1个回答
3
投票

您可以通过两种方式在 Python/OpenCV 中实现这一点 - 1) 简单平铺和 2) 无缝平铺。

其概念是将图像缩小某个因子,然后按相同因子平铺。如果是无缝平铺,则水平翻转图像并与原始图像连接。然后垂直翻转并与前一个连接图像垂直连接。

输入:

import cv2
import numpy as np

# read image
img1 = cv2.imread('pattern.jpg')

# -----------------------------------------------------------------------------
# SIMPLE TILING

# reduce size by 1/20
xrepeats = 20
yrepeats = 20
xfact = 1/xrepeats
yfact = 1/yrepeats
reduced1 = cv2.resize(img1, (0,0), fx=xfact, fy=yfact, interpolation=cv2.INTER_AREA)

# tile (repeat) pattern 20 times in each dimension
result1 = cv2.repeat(reduced1, yrepeats, xrepeats)
# -----------------------------------------------------------------------------


# -----------------------------------------------------------------------------
# SEAMLESS TILING

# flip horizontally
img2 = cv2.flip(img1, 1)

# concat left-right
#img3 = np.hstack((img1, img2))
img3 = cv2.hconcat([img1, img2])

# flip vertically
img4 = cv2.flip(img3, 0)

# concat top-bottom
#img = np.vstack((img3, img4))
img5 = cv2.vconcat([img3, img4])

# reduce size by 1/20
xrepeats = 10
yrepeats = 10
xfact = 1/(2*xrepeats)
yfact = 1/(2*yrepeats)
reduced2 = cv2.resize(img5, (0,0), fx=xfact, fy=yfact, interpolation=cv2.INTER_AREA)

# tile (repeat) pattern 10 times in each dimension
result2 = cv2.repeat(reduced2, yrepeats, xrepeats)
# -----------------------------------------------------------------------------

# save results
cv2.imwrite("pattern_tiled1.jpg", result1)
cv2.imwrite("pattern_tiled2.jpg", result2)

# show the results
cv2.imshow("result", result1)
cv2.imshow("result2", result2)
cv2.waitKey(0)

简单平铺结果:

无缝平铺结果:

© www.soinside.com 2019 - 2024. All rights reserved.