• opencv 3.1.0 访问像素值的三种方法(C++)


    三种方法分别问:

    指针访问:void colorReduce_ptr(cv::Mat &inputImage, cv::Mat &outputImage, int div);

    迭代器访问:void colorReduce_iterator(cv::Mat &inputImage, cv::Mat &outputImage, int div);

    动态地址计算:void colorReduce_at(cv::Mat &inputImage, cv::Mat &outputImage, int div);

    #include <opencv.hpp>
    #include <iostream>
    
    using namespace std;
    
    void colorReduce_ptr(cv::Mat &inputImage, cv::Mat &outputImage, int div);
    void colorReduce_iterator(cv::Mat &inputImage, cv::Mat &outputImage, int div);
    void colorReduce_at(cv::Mat &inputImage, cv::Mat &outputImage, int div);
    
    void colorReduce_ptr(cv::Mat &inputImage, cv::Mat &outputImage, int div)
    {
        outputImage = inputImage.clone();
        int rowNumber = outputImage.rows;
        int colNumber = outputImage.cols*outputImage.channels();
        for (size_t i = 0; i < rowNumber; i++)
        {
            uchar* data = outputImage.ptr<uchar>(i);
            for (size_t j = 0; j < colNumber; j++)
            {
                data[j] = data[j] / div*div + div / 2;
            }
        }
    }
    
    void colorReduce_iterator(cv::Mat &inputImage, cv::Mat &outputImage, int div)
    {
        outputImage = inputImage.clone();
        cv::Mat_<cv::Vec3b>::iterator it = outputImage.begin<cv::Vec3b>();
        cv::Mat_<cv::Vec3b>::iterator it_end = outputImage.end<cv::Vec3b>();
    
        for (;it != it_end; ++it)
        {
            for (size_t px = 0; px < 3; px++)
            {
                (*it)[px] = (*it)[px] / div*div + div / 2;
            }
        }
    }
    
    void colorReduce_at(cv::Mat &inputImage, cv::Mat &outputImage, int div)
    {
        outputImage = inputImage.clone();
        int rowNumber = outputImage.rows;
        int colNumber = outputImage.cols;
    
        for (size_t i = 0; i < rowNumber; i++)
        {
            for (size_t j = 0; j < colNumber; j++)
            {
                for (size_t px = 0; px < 3; px++)
                {
                    outputImage.at<cv::Vec3b>(i, j)[px] = outputImage.at<cv::Vec3b>(i, j)[px] / div * div + div / 2;
                }
            }
        }
    }
    
    int main(int argc, char** argv[])
    {
        cv::Mat srcImg = cv::imread("E:/data/lena.jpg");
        cv::imshow("srcImg", srcImg);
    
        cv::Mat dstImg;
        dstImg.create(srcImg.rows, srcImg.cols, srcImg.type());
        
        double timeStart;
        timeStart = static_cast<double>(cv::getTickCount());
        colorReduce_ptr(srcImg, dstImg, 32);
        timeStart = ((double)cv::getTickCount() - timeStart) / cv::getTickFrequency();
        cout << "ptr方法运行时间为:" << timeStart << "" << endl;
    
        timeStart = static_cast<double>(cv::getTickCount());
        colorReduce_iterator(srcImg, dstImg, 32);
        timeStart = ((double)cv::getTickCount() - timeStart) / cv::getTickFrequency();
        cout << "iterator方法运行时间为:" << timeStart << "" << endl;
    
        timeStart = static_cast<double>(cv::getTickCount());
        colorReduce_at(srcImg, dstImg, 32);
        timeStart = ((double)cv::getTickCount() - timeStart) / cv::getTickFrequency();
        cout << "at方法运行时间为:" << timeStart << "" << endl;
        
        cv::imshow("dstImg", dstImg);
        cv::waitKey(0);
    }

  • 相关阅读:
    JAVA 字符处理
    android:visibility 隐藏 可见 不可见
    Android中 int 和 String 互相转换的多种方法
    日期月和日补零
    【程序】程序报错 error:-1610149839 等大负数
    【IE】将IE11改为64位
    【linux】CentOS网络配置
    【linux】CentOS虚拟机eth0 提示Device does not seem to be present
    【SUSE】关闭防火墙
    【走马观花】十月八日通州雾
  • 原文地址:https://www.cnblogs.com/asmer-stone/p/5396388.html
Copyright © 2020-2023  润新知