opencv学习笔记(五)镜像对称

2023-03-14,,

  

opencv学习笔记(五)镜像对称

  设图像的宽度为width,长度为height。(x,y)为变换后的坐标,(x0,y0)为原图像的坐标。

水平镜像变换:

  

  代码实现:

  

 #include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
using namespace cv; void hMirrorTrans(const Mat &src, Mat &dst)
{
CV_Assert(src.depth() == CV_8U);
dst.create(src.rows, src.cols, src.type()); int rows = src.rows;
int cols = src.cols; switch (src.channels())
{
case :
const uchar *origal;
uchar *p;
for (int i = ; i < rows; i++){
origal = src.ptr<uchar>(i);//ptr<>函数得到一行的指针,并用[]操作符访问某一列的像素值
p = dst.ptr<uchar>(i);
for (int j = ; j < cols; j++){
p[j] = origal[cols - - j];
}
}
break;
case :
const Vec3b *origal3;
Vec3b *p3;
for (int i = ; i < rows; i++) {
origal3 = src.ptr<Vec3b>(i);
p3 = dst.ptr<Vec3b>(i);
for (int j = ; j < cols; j++){
p3[j] = origal3[cols - - j];
}
}
break;
default:
break;
} } int main(void)
{
Mat src = imread("Rise&Shine.jpg");
Mat dst;
dst = Mat::zeros(src.size(), src.type());
hMirrorTrans(src, dst);
imshow("原图像", src);
imshow("镜像对称图像", dst);
waitKey();
return ;
}

  运行结果:

  原图像:

  

  镜像对称图像:

  

 

opencv学习笔记(五)镜像对称的相关教程结束。

《opencv学习笔记(五)镜像对称.doc》

下载本文的Word格式文档,以方便收藏与打印。