opencv学习笔记(五)镜像对称
设图像的宽度为width,长度为height。(x,y)为变换后的坐标,(x0,y0)为原图像的坐标。
水平镜像变换:
代码实现:
1 #include <iostream> 2 #include <cv.h> 3 #include <highgui.h> 4 using namespace std; 5 using namespace cv; 6 7 void hMirrorTrans(const Mat &src, Mat &dst) 8 { 9 CV_Assert(src.depth() == CV_8U); 10 dst.create(src.rows, src.cols, src.type()); 11 12 int rows = src.rows; 13 int cols = src.cols; 14 15 switch (src.channels()) 16 { 17 case 1: 18 const uchar *origal; 19 uchar *p; 20 for (int i = 0; i < rows; i++){ 21 origal = src.ptr<uchar>(i);//ptr<>函数得到一行的指针,并用[]操作符访问某一列的像素值 22 p = dst.ptr<uchar>(i); 23 for (int j = 0; j < cols; j++){ 24 p[j] = origal[cols - 1 - j]; 25 } 26 } 27 break; 28 case 3: 29 const Vec3b *origal3; 30 Vec3b *p3; 31 for (int i = 0; i < rows; i++) { 32 origal3 = src.ptr<Vec3b>(i); 33 p3 = dst.ptr<Vec3b>(i); 34 for (int j = 0; j < cols; j++){ 35 p3[j] = origal3[cols - 1 - j]; 36 } 37 } 38 break; 39 default: 40 break; 41 } 42 43 } 44 45 int main(void) 46 { 47 Mat src = imread("Rise&Shine.jpg"); 48 Mat dst; 49 dst = Mat::zeros(src.size(), src.type()); 50 hMirrorTrans(src, dst); 51 imshow("原图像", src); 52 imshow("镜像对称图像", dst); 53 waitKey(0); 54 return 0; 55 }
运行结果:
原图像:
镜像对称图像: