繁体   English   中英

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

[英]OpenCv: Translate Image, Wrap Pixels Around Edges (C++)

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

我们从图片一开始...

移动 x 个像素...

并移动 y 个像素...

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM