zoukankan      html  css  js  c++  java
  • opencv学习笔记(五)镜像对称

      

     

    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 }

      运行结果:

      原图像:

      

      镜像对称图像:

      

     

  • 相关阅读:
    简单工厂
    Java鲁棒性(健壮性)
    外部类,成员内部类,局部内部类能被哪些修饰符修饰
    Java枚举类的7种常用的方法
    同步,异步,阻塞,非阻塞
    对于面向对象的基本理解
    对于数组的一点理解
    类加载机制-双亲委派机制(三)
    架构- 数据库的优化
    python调用jenkinsapi
  • 原文地址:https://www.cnblogs.com/codingmengmeng/p/5608930.html
Copyright © 2011-2022 走看看