zoukankan      html  css  js  c++  java
  • opencv 访问图像像素的三种方式

    访问图像中的像素

    访问图像像素有三种可行的方法
    方法一:指针访问
    指针访问访问的速度最快,Mat类可以通过ptr函数得到图像任意一行的首地址,
    同时,Mat类的一些属性也可以用到公有属性 rows和cols 表示行和列
    通道数可以通过channels()函数获得;
    void visitPix1()
    {
    Mat srcImg = imread("jpg/1.jpeg");
    Mat dstImg;
    srcImg.copyTo(dstImg);
    int rowNum = dstImg.rows;
    int colNum = dstImg.cols*dstImg.channels();
    for(int row = 0;row<rowNum;row++)
    {
    uchar* data = srcImg.ptr<uchar>(row);
    for(int col = 0;col<colNum;col++)
    {
    data[col]+=23;
    }
    }
    imshow("src",srcImg);
    imshow("dst",dstImg);
    waitKey();
    }
    方法二:迭代器访问
    通过迭代器访问像素。可以获得begin和end 然后只需要从begin迭代到end就可以 用×操作符取得地址内容

    void visitPix1()
    {
    Mat srcImg = imread("jpg/1.jpeg");
    Mat dstImg;
    srcImg.copyTo(dstImg);
    Mat_<Vec3b>::iterator begin = dstImg.begin<Vec3b>();
    Mat_<Vec3b>::iterator end = dstImg.end<Vec3b>();
    for(Mat_<Vec3b>::iterator it = begin;it!=end;it++)
    {
    (*it)[0] += 12;
    (*it)[1] +=13;
    (*it)[2] +=14;
    }
    imshow("src",srcImg);
    imshow("dst",dstImg);
    waitKey();
    }
    方法三:动态地址计算
    这里可以介绍一个at方法,at(int y,int x)可以用来存取图像,但是需要数据类型的转换
    img.at<Vec3b>(j,i)[channel] = value;
    void visitPix3()
    { Mat srcImg = imread("jpg/1.jpeg");
    Mat dstImg;
    srcImg.copyTo(dstImg);
    int rowNum = dstImg.rows;
    int colNum = dstImg.cols*dstImg.channels();
    for(int row = 0;row<rowNum;row++)
    {
    for(int col = 0;col<colNum;col++)
    {
    dstImg.at<Vec3b>(row,col)[0] +=23;
    dstImg.at<Vec3b>(row,col)[1] +=23;
    dstImg.at<Vec3b>(row,col)[2] +=23;
    }

    }
    imshow("src",srcImg);
    imshow("dst",dstImg);
    waitKey();

    }

    另外添加一个函数

    计时函数

    opencv 中有两个可以计时的函数 getTickCount 和 getTickFrequency
    getTickCount 函数返回cpu自某个事件以来走过的时钟周期数
    getTickFrequency 函数返回cpu 一秒钟所走的时钟周期数,

    例子
    void timeTest()
    {
    double time0 = static_cast<double>(getTickCount());
    for(int i =0;i<100000;i++){}
    time0 = (double)(getTickCount()-time0)/getTickFrequency();
    cout<<"run time"<<time0<<"sec"<<endl;
    }

  • 相关阅读:
    网页性能优化,缓存优化、加载时优化、动画优化--摘抄
    display的32种写法--摘抄
    transform与position:fixed的那些恩怨--摘抄
    float 常见用法与问题--摘抄
    10个JavaScript难点--摘抄
    CSS3 动画卡顿性能优化解决方案--摘抄
    CSS 盒模型、解决方案、BFC 原理讲解--摘抄
    web实时长图实践--摘抄
    移动端H5多平台分享实践--摘抄
    canvas绘制视频封面--摘抄
  • 原文地址:https://www.cnblogs.com/techdreaming/p/5196541.html
Copyright © 2011-2022 走看看