翻转图像以获得镜面效果

问题描述 投票:0回答:4

我正在开发一个视频处理项目,需要一些帧翻转。我尝试使用 cvFlip 但似乎没有沿 y 轴翻转(x 轴工作...)并导致分段错误。还有其他选择吗??

cv::Mat dst=src;      //src= source image from cam
cv::flip(dst, dst, 1);     //segmentation fault shown

imshow("flipped",dst);
c++ opencv image-processing
4个回答
16
投票
cv::Mat src=imload("bla.png");
cv::Mat dst;               // dst must be a different Mat
cv::flip(src, dst, 1);     // because you can't flip in-place (leads to segfault)

9
投票

使用

cv::flip
并将
1
传递为
flipcode

用示例代码查看你的编辑,你无法原地翻转。您需要一个单独的目的地

cv::Mat
:

cv::Mat dst;
cv::flip(src, dst, 1);
imshow("flipped",dst);

6
投票

关键是创建与

dst
完全相同的
src

cv::Mat dst = cv::Mat(src.rows, src.cols, CV_8UC3);
cv::flip(src, dst, 1);

imshow("flipped", dst);

0
投票

至少在openCV 4.8中,可以原地翻转。

可能的示例代码:

// buffer is a pointer to data, for example:
// uint8_t* buffer = new uint8_t[w*h*4]
// CV_8UC4 means unsigned byte, 4 channel image (for example RGBA)
// UMat allows to use GPU acceleration, it may be useful or not depending
// on the scenario, or you can simply just use cv::Mat.
cv::UMat m = cv::Mat(h , w, CV_8UC4, buffer).getUMat(cv::ACCESS_READ);
// flip accepts a flag that can be 0, <0 or >0
// here I put zero cause if the image is taken in landscape mode, although // looks like it needs vertical mirroring, it is actually horizontal. So you // may want to try the various options if 1 doesn't work as expected
flip(m, m, 0);
© www.soinside.com 2019 - 2024. All rights reserved.