zoukankan      html  css  js  c++  java
  • Opencv --- 图像像素遍历的各种方法

    #include <opencv2/core.hpp>
    #include <opencv2/imgcodecs.hpp>
    #include <opencv2/highgui.hpp>
    #include <iostream>
    using namespace cv;
    using namespace std;
    
    // 记录时间信息
    void PrintMs(const char *text = "")
    {
        static long long last = 0;
        long long cur = getTickCount();
        if (last == 0)
        {
            last = cur;
            return;
        }
        long long ms = 0;
        ms = ((double)(cur - last) / getTickFrequency()) * 1000;
        if (*text != 0)
        {
            printf("%s = %dms
    ", text,ms);
        }
        last = getTickCount();
    }
    int main(int argc, char *argv[])
    {
        Mat mat(3000, 4000, CV_8UC3);
        //mat.create(3000, 4000, CV_8UC3);
        //元素大小字节数
        int es = mat.elemSize();
        int size = mat.rows*mat.cols*es;
        
        //地址遍历连续的Mat
        PrintMs();
        for (int i = 0; i < size; i += es)
        {
            mat.data[i] = 255;    //B
            mat.data[i + 1] = 100;//G
            mat.data[i + 2] = 100;//R
        }
        PrintMs("mat.data ms");
    
        //地址遍历不一定连续的Mat
        for (int row = 0; row < mat.rows; row++)
        {
            for (int col = 0; col < mat.cols; col++)
            {
                (&mat.data[row*mat.step])[col*es] = 0;//B
                (&mat.data[row*mat.step])[col*es+1] = 0;//G
                (&mat.data[row*mat.step])[col*es+2] = 255;//R
            }
        }
        PrintMs("mat.step ms");
    
    
        //使用ptr遍历Mat
        for (int row = 0; row < mat.rows; row++)
        {
            for (int col = 0; col < mat.cols; col++)
            {
                Vec3b *c = mat.ptr<Vec3b>(row, col);
                c->val[0] = 0;        //B
                c->val[1] = 255;    //G
                c->val[2] = 0;        //R
            }
        }
        PrintMs("mat.ptr ");
        try
        {
            //使用at来遍历
            for (int row = 0; row < mat.rows; row++)
            {
                for (int col = 0; col < mat.cols; col++)
                {
                    Vec3b &m = mat.at<Vec3b>(row, col);
                    m[0] = 100;
                    m[1] = 100;
                    m[2] = 100;
                }
            }
            PrintMs("mat.at");
        }
        catch (Exception &ex)
        {
            cout << ex.what() << endl;
        }
        
        //通过迭代器来遍历
        PrintMs("");
        auto it = mat.begin<Vec3b>();
        auto it_end = mat.end<Vec3b>();
        for (; it != it_end; it++)
        {
            (*it).val[0] = 0;    //B
            (*it).val[1] = 0;    //G
            (*it).val[2] = 255;    //R
        }
        PrintMs("mat.itr");
    
    
        namedWindow("mat");
        imshow("mat", mat);
        waitKey(0);
    
        return 0;
    }
  • 相关阅读:
    SVN相关
    Sublime text 2/3 中 Package Control 的安装与使用方法
    JavaScript 限制input输入类型(多种方法实现)
    yahoo的30条优化规则
    浏览器页面加载解析渲染机制(一)
    Cookies 和 Session的区别
    Post Get 区别
    Vue 学习之 关键字、关键单词
    移动H5前端性能优化指南
    『Python基础练习题』day01
  • 原文地址:https://www.cnblogs.com/hs-pingfan/p/10656854.html
Copyright © 2011-2022 走看看