【问题标题】:Enlarge Image in specific pattern using opencv使用opencv以特定模式放大图像
【发布时间】:2021-12-06 16:32:35
【问题描述】:

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

这是我写的代码:

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

以下是我的输入图像:

这就是我得到的:

虽然我需要这样的东西:

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

谢谢。

【问题讨论】:

  • 我认为您正在寻找图像平铺而不是图像大小调整。

标签: python opencv image-processing opencv3.0 image-resizing


【解决方案1】:

您可以在 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)

简单的平铺结果:

无缝平铺结果:

【讨论】:

    猜你喜欢
    • 2013-10-20
    • 1970-01-01
    • 2021-12-23
    • 2021-06-07
    • 2017-04-14
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 2012-06-28
    相关资源
    最近更新 更多