//
//函数功能:图片水平镜像
//输入: Mat src原图像
//输出: 镜像变换后的图像
//
Mat mirror(Mat src)
{
Mat result;
result.create(src.size(), src.type());
for (int i = 0; i < src.rows; i++)
{
for (int j = 0; j < src.cols; j++)
{
result.at<Vec3b>(i, j)[0] = src.at<Vec3b>(i, src.cols - 1 - j)[0];
result.at<Vec3b>(i, j)[1] = src.at<Vec3b>(i, src.cols - 1 - j)[1];
result.at<Vec3b>(i, j)[2] = src.at<Vec3b>(i, src.cols - 1 - j)[2];
}
}
return result;
}
更新一下,OpenCV中好像有自带的函数,参考https://blog.csdn.net/wenhao_ir/article/details/51442792?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
//OpenCV版本2.4.9
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main()
{
cv::Mat srcImage = cv::imread("pool.jpg");
if(!srcImage.data)
return -1;
// 水平翻转
cv::Mat resultImage2;
cv::flip(srcImage, resultImage2, 1);
// 垂直翻转
cv::Mat resultImage3;
cv::flip(srcImage, resultImage3, 0);
// 垂直和水平翻转
cv::Mat resultImage4;
cv::flip(srcImage, resultImage4, -1);
cv::imshow("srcImage", srcImage);
cv::imshow("水平翻转后的图像", resultImage2);
cv::imshow("垂直翻转后的图像", resultImage3);
cv::imshow("垂直和水平翻转后的图像", resultImage4);
cv::waitKey(0);
return 0;
}