如何在不使用 OpenCV 中的内置函数 flip 的情况下进行翻转? [英] How to do flipping without using the inbuilt function flip in OpenCV?
问题描述
谁能帮我解决这个问题,如何在不使用内置翻转功能的情况下翻转图像,即使用 OpenCV 在 C++ 中翻转(src 图像,目标图像,1 或 0).我是这个软件的新手,所以请帮忙.
Can anyone help me with this problem, how to do flipping of an image without using the inbuilt flipping function i.e. flip(src image, destination image , 1 or 0) in C++ using OpenCV. I am new to this software so please help.
推荐答案
假设您有充分的理由不使用 OpenCV flip 函数,您可以编写自定义函数.
Assuming you have a good reason not to use OpenCV flip function, you can write your custom one.
对于本示例,我将使用 CV_8UC3
图像.我会在最后指出如何将其扩展为不同的格式.
For this example, I'll use CV_8UC3
images. I'll point out at the end how to expand this to different formats.
我们先来看看如何翻转图像x轴,对应cv::flip(src, dst, 1)
.给定一个 src
图像,dst
图像将具有与 相同的
坐标.在实践中:y
坐标和 x
坐标src.cols - 1 - x
Let's see first how to flip an image x axis, which corresponds to cv::flip(src, dst, 1)
. Given an src
image, the dst
image will have the same y
coordinate, and x
coordinate as src.cols - 1 - x
coordinates. In practice:
void flip_lr(const Mat3b& src, Mat3b& dst)
{
Mat3b _dst(src.rows, src.cols);
for (int r = 0; r < _dst.rows; ++r) {
for (int c = 0; c < _dst.cols; ++c) {
_dst(r, c) = src(r, src.cols - 1 - c);
}
}
dst = _dst;
}
然后,围绕y轴翻转(对应于cv::flip(src, dst, 0)
),dst
将具有相同的x
坐标,y
为 src.rows - 1 - y
.但是,您可以重用上述函数,只需转置 dst
矩阵,在 x 轴上应用翻转,然后转回即可.在实践中:
Then, to flip around y axis (corresponding to cv::flip(src, dst, 0)
), dst
will have the same x
coordinate, and y
as src.rows - 1 - y
. However, you can reuse the above-mentioned function, simply transposing the dst
matrix, apply flip on x axis, and then transpose back. In practice:
dst = src.t();
flip_lr(dst, dst);
dst = dst.t();
然后,要翻转两个轴,对应于 cv::flip(src, dst, -1)
,您只需将 x 轴和 y 轴上的翻转组合起来:
Then, to flip both axis, corresponding to cv::flip(src, dst, -1)
, you need simply to combine the flip on x and y axis:
flip_lr(src, dst);
dst = dst.t();
flip_lr(dst, dst);
dst = dst.t();
您可以将此功能包装在自定义翻转函数中,该函数采用与 cv::flip
相同的参数:
You can wrap this functionality in a custom flip function that takes the same parameters as cv::flip
:
void custom_flip(const Mat3b& src, Mat3b& dst, int code)
{
if (code > 0)
{ // Flip x axis
flip_lr(src, dst);
}
else if (code == 0)
{
// Flip y axis
dst = src.t();
flip_lr(dst, dst);
dst = dst.t();
}
else // code < 0
{
// Flip x and y axis
flip_lr(src, dst);
dst = dst.t();
flip_lr(dst, dst);
dst = dst.t();
}
}
请注意,您只需修改 flip_lr
函数,并注意在 custom_flip
中调用适当的版本,即可将其调整为不同的格式,现在将接受 Mat
而不是 Mat3b
.
Note that you can adapt this to different format simply modifing the flip_lr
function, and taking care to call the appropriate version inside custom_flip
, that will now accept Mat
instead of Mat3b
.
完整代码供参考:
void flip_lr(const Mat3b& src, Mat3b& dst)
{
Mat3b _dst(src.rows, src.cols);
for (int r = 0; r < _dst.rows; ++r) {
for (int c = 0; c < _dst.cols; ++c) {
_dst(r, c) = src(r, src.cols - 1 - c);
}
}
dst = _dst;
}
void custom_flip(const Mat3b& src, Mat3b& dst, int code)
{
if (code > 0)
{ // Flip x axis
flip_lr(src, dst);
}
else if (code == 0)
{
// Flip y axis
dst = src.t();
flip_lr(dst, dst);
dst = dst.t();
}
else // code < 0
{
// Flip x and y axis
flip_lr(src, dst);
dst = dst.t();
flip_lr(dst, dst);
dst = dst.t();
}
}
int main(void)
{
Mat3b img = imread("path_to_image");
Mat3b flipped;
flip(img, flipped, -1);
Mat3b custom;
custom_flip(img, custom, -1);
imshow("OpenCV flip", flipped);
imshow("Custom flip", custom);
waitKey();
return 0;
}
这篇关于如何在不使用 OpenCV 中的内置函数 flip 的情况下进行翻转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!