使用opencv以特定模式放大图像
Enlarge Image in specific pattern using opencv
我正在尝试使用 opencv 以特定模式调整图像大小,但遇到了一些问题。
所以这是我写的代码:
x1 = cv2.resize(x, (1500, 1600), interpolation = cv2.INTER_AREA)
以下是我的输入图像:
这就是我得到的:
虽然我需要这样的东西:
那么实现这一目标的最佳方法是什么?
谢谢。
您可以通过 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)
简单平铺结果:
无缝平铺结果:
我正在尝试使用 opencv 以特定模式调整图像大小,但遇到了一些问题。
所以这是我写的代码:
x1 = cv2.resize(x, (1500, 1600), interpolation = cv2.INTER_AREA)
以下是我的输入图像:
这就是我得到的:
虽然我需要这样的东西:
那么实现这一目标的最佳方法是什么?
谢谢。
您可以通过 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)
简单平铺结果:
无缝平铺结果: