OpenCV使用遇到的问题
图像翻转
The example scenarios of using the function are the following:
- Vertical flipping of the image (flipCode == 0) to switch between top-left and bottom-left image origin. This is a typical operation in video processing on Microsoft Windows* OS.
- Horizontal flipping of the image with the subsequent horizontal shift and absolute difference calculation to check for a vertical-axis symmetry (flipCode > 0).
- Simultaneous horizontal and vertical flipping of the image with the subsequent shift and absolute difference calculation to check for a central symmetry (flipCode < 0).
- Reversing the order of point arrays (flipCode > 0 or flipCode == 0).
简而言之,就是:
flipCode >0: 沿y轴翻转;==0: 沿x轴翻转; <0: x、y轴同时翻转 。图片中的坐标系,原点在左上角,向右为x轴,向下为y轴。 效果图如下:
- x轴翻转

- y轴翻转

- x,y都翻转

图片旋转
transpose():相当于逆时针旋转90°,然后取镜像。效果如下:

顺时针旋转90°
transpose(img2, img2);
flip(img2, img3, 1);
imshow("先转置,再y轴翻转", img3);

顺时针旋转270°(逆时针旋转90°)
transpose(img2, img2);
flip(img2, img3, 0);
imshow("先转置,再x轴翻转", img3);

旋转180°
- 方法1:
flip(img2, img3, 0);
flip(img3, img3, 1);
imshow("先x轴翻转,再y轴翻转", img3);

- 方法2: 先顺时针旋转90°,再顺时针旋转90°。 略
遇到的错误
描述: OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat
在使用OpenCV的裁剪功能时,传入了cv::Rect,遇到如上错误。后来经过检查,错误来源自传给cv::Rect的四个参数。在JNI中获得的来自Java层的floag[]在转化过程中出现了错误,让原本的参数,变成了其它的莫名其妙的负数,致使如上错误出现。后面经过对参数的修正,错误就未出现了,裁剪图片成功。
大致理解,此错误来源自所需要裁剪的区域,应该是超过了原本图片大小。