OpenCv:翻译图像,将像素环绕在边缘 (C++)

pop*_*ppy 4 c++ algorithm opencv image-processing

我试图将图像水平平移 x 像素,垂直平移 y 像素,因此 . 但是,我希望像素环绕边缘。基本上...

我们从图像一开始......

移动 x 像素...

并移动 y 个像素...

据我所知,OpenCv 的 warpAffine() 无法做到这一点。通常,我只会遍历图像并将像素移动一定量,但这样做时我只能水平移动它们。解决此问题的最有效方法是什么?

nat*_*ncy 5

您可以使用 np.roll()

这是一个可视化

我在 Python 中实现了它,但您可以在 C++ 中应用类似的滚动技术

import cv2
import numpy as np

image = cv2.imread('1.jpg')

shift_x = 800
shift_y = 650

# Shift by x-axis
for i in range(image.shape[1] -1, shift_x, -1):
    image = np.roll(image, -1, axis=1)
    image[:, -1] = image[:, 0]
    cv2.imshow('image', image)
    cv2.waitKey(1)

# Shift by y-axis
for i in range(image.shape[1] -1, shift_y, -1):
    image = np.roll(image, -1, axis=0)
    image[:, -1] = image[:, 0]
    cv2.imshow('image', image)
    cv2.waitKey(1)

cv2.imshow('image', image)
cv2.waitKey()
Run Code Online (Sandbox Code Playgroud)

  • 很好的解决方案,但仅限于 Python,而不是作为 OP 标记的 C++。 (2认同)