我正在嘗試使用 opencv 以特定模式調整影像大小,但遇到了一些問題。
所以這是我寫的代碼:
x1 = cv2.resize(x, (1500, 1600), interpolation = cv2.INTER_AREA)
以下是我的輸入影像:

這就是我得到的:

雖然我需要這樣的東西:

那么實作這一目標的最佳方法是什么?
謝謝。
uj5u.com熱心網友回復:
您可以在 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)
簡單的平鋪結果:

無縫平鋪結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/326511.html
