首页 > 解决方案 > OpenCv:翻译图像,将像素环绕在边缘 (C++)

问题描述

我正在尝试将图像水平平移 x 像素,垂直平移 y 像素,所以 . 但是,我希望像素环绕边缘。基本上...

我们从图片一开始...

移动 x 个像素...

并移动 y 个像素...

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

标签: c++algorithmopencvimage-processing

解决方案


您可以使用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()

推荐阅读