[英]OpenCv: Translate Image, Wrap Pixels Around Edges (C++)
您可以使用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()
从我的角度来看,最“有效”的方法是使用cv::Rect
设置四个相应的 ROI,并使用cv::copyTo
手动复制内容。 也许,也有可能不复制实际内容,只需指向输入cv::Mat
中的数据 - 但不幸的是,至少我找不到。
不过,这是我的代码:
// Shift input image by sx pixels to the left, and sy pixels to the top.
cv::Mat transWrap(cv::Mat& input, const int sx, const int sy)
{
// Get image dimensions.
const int w = input.size().width;
const int h = input.size().height;
// Initialize output with same dimensions and type.
cv::Mat output = cv::Mat(h, w, input.type());
// Copy proper contents manually.
input(cv::Rect(sx, sy, w - sx, h - sy)).copyTo(output(cv::Rect(0, 0, w - sx, h - sy)));
input(cv::Rect(0, sy, sx, h - sy)).copyTo(output(cv::Rect(w - sx, 0, sx, h - sy)));
input(cv::Rect(sx, 0, w - sx, sy)).copyTo(output(cv::Rect(0, h - sy, w - sx, sy)));
input(cv::Rect(0, 0, sx, sy)).copyTo(output(cv::Rect(w - sx, h - sy, sx, sy)));
return output;
}
int main()
{
cv::Mat input = cv::imread("images/tcLUa.jpg", cv::IMREAD_COLOR);
cv::resize(input, input, cv::Size(), 0.25, 0.25);
cv::Mat output = transWrap(input, 300, 150);
return 0;
}
当然,代码看起来是重复的,但是包裹在一个自己的 function 中,它不会在你的主代码中打扰你。 ;-)
output 应该是,你想要实现的:
希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.