水平镜像
#include "pch.h"
#include <iostream>
#include <highgui.h>
using namespace std;
using namespace cv;
//水平镜像
void horizontalMirrorImage(const Mat& src, Mat& dst)
{
CV_Assert(src.depth() == CV_8U);
const int rows = src.rows;
const int cols = src.cols;
dst.create(rows, cols, src.type());
switch (src.channels())
{
case 1:
const uchar* origal;
uchar* p;
for (int i = 0; i < rows; i++)
{
origal = src.ptr<uchar>(i);
p = dst.ptr<uchar>(i);
for (int j = 0; j < cols; j++)
{
p[j] = origal[cols - 1 - j];
}
}
break;
case 3:
const Vec3b* origal3;
Vec3b* p3;
for (int m = 0; m < rows; m++)
{
origal3 = src.ptr<Vec3b>(m);
p3 = dst.ptr<Vec3b>(m);
for (int n = 0; n < cols; n++)
{
p3[n] = origal3[cols - 1 - n];
}
}
break;
default:
break;
}
}
int main()
{
Mat src = imread("1.png");
Mat dst;
horizontalMirrorImage(src, dst);
namedWindow("原图");
namedWindow("水平镜像");
imshow("水平镜像", dst);
imshow("原图", src);
waitKey(0);
}
垂直镜像
#include "pch.h"
#include <iostream>
#include <highgui.h>
using namespace std;
using namespace cv;
//垂直镜像
void verticalMirrorImage(const Mat& src, Mat& dst)
{
CV_Assert(src.depth() == CV_8U);
dst.create(src.rows, src.cols, src.type());
for (int i = 0; i < src.rows; i++)
{
src.row(src.rows - i - 1).copyTo(dst.row(i));
}
}
int main()
{
Mat src = imread("1.png");
Mat dst;
verticalMirrorImage(src, dst);
namedWindow("原图");
namedWindow("垂直镜像");
imshow("垂直镜像", dst);
imshow("原图", src);
waitKey(0);
}