我正在开发一个视频处理项目,需要一些帧翻转。我尝试使用 cvFlip 但似乎没有沿 y 轴翻转(x 轴工作...)并导致分段错误。还有其他选择吗??
cv::Mat dst=src; //src= source image from cam
cv::flip(dst, dst, 1); //segmentation fault shown
imshow("flipped",dst);
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)
cv::flip
并将 1
传递为 flipcode
。
用示例代码查看你的编辑,你无法原地翻转。您需要一个单独的目的地
cv::Mat
:
cv::Mat dst;
cv::flip(src, dst, 1);
imshow("flipped",dst);
关键是创建与
dst
完全相同的src
:
cv::Mat dst = cv::Mat(src.rows, src.cols, CV_8UC3);
cv::flip(src, dst, 1);
imshow("flipped", dst);
至少在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);