OpenCV中遍历图像
来源:互联网 发布:mysql数据库导入导出 编辑:程序博客网 时间:2024/04/28 22:25
- iterator
Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table){ // accept only char type matrices CV_Assert(I.depth() == CV_8U); const int channels = I.channels(); switch(channels) { case 1: { MatIterator_<uchar> it, end; for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it) *it = table[*it]; break; } case 3: { MatIterator_<Vec3b> it, end; for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it) { (*it)[0] = table[(*it)[0]]; (*it)[1] = table[(*it)[1]]; (*it)[2] = table[(*it)[2]]; } } } return I;}
LUT
Mat lookUpTable(1, 256, CV_8U); uchar* p = lookUpTable.data; for( int i = 0; i < 256; ++i) p[i] = table[i]; LUT(I, lookUpTable, J);
完整程序
/* * main.cpp * * Created on: Mar 5, 2017 * Author: may */#include <opencv2/core.hpp>#include <opencv2/core/utility.hpp>#include "opencv2/imgcodecs.hpp"#include <opencv2/highgui.hpp>#include <iostream>#include <sstream>using namespace std;using namespace cv;static void help(){ cout << "\n--------------------------------------------------------------------------" << endl << "This program shows how to scan image objects in OpenCV (cv::Mat). As use case" << " we take an input image and divide the native color palette (255) with the " << endl << "input. Shows C operator[] method, iterators and at function for on-the-fly item address calculation."<< endl << "Usage:" << endl << "./how_to_scan_images <imageNameToUse> <divideWith> [G]" << endl << "if you add a G parameter the image is processed in gray scale" << endl << "--------------------------------------------------------------------------" << endl << endl;}Mat& ScanImageAndReduceIterator(Mat& I, const uchar* table);int main(){ help(); Mat I, J; I = imread("image.jpg"); if (I.empty()) { cout << "The image could not be loaded." << endl; return -1; } //! [dividewith] int divideWith = 7; // convert our input string to number - C++ style uchar table[256]; for (int i = 0; i < 256; ++i) table[i] = (uchar)(divideWith * (i/divideWith)); //! [dividewith] const int times = 100; double t; t = (double)getTickCount(); for (int i = 0; i < times; ++i) { cv::Mat clone_i = I.clone(); J = ScanImageAndReduceIterator(clone_i, table); } t = 1000*((double)getTickCount() - t)/getTickFrequency(); t /= times; cout << "Time of reducing with the iterator (averaged for " << times << " runs): " << t << " milliseconds."<< endl; //! [table-init] Mat lookUpTable(1, 256, CV_8U); uchar* p = lookUpTable.ptr(); for( int i = 0; i < 256; ++i) p[i] = table[i]; //! [table-init] t = (double)getTickCount(); for (int i = 0; i < times; ++i) //! [table-use] LUT(I, lookUpTable, J); //! [table-use] t = 1000*((double)getTickCount() - t)/getTickFrequency(); t /= times; cout << "Time of reducing with the LUT function (averaged for " << times << " runs): " << t << " milliseconds."<< endl; return 0;}//! [scan-iterator]Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table){ // accept only char type matrices CV_Assert(I.depth() == CV_8U); const int channels = I.channels(); switch(channels) { case 1: { MatIterator_<uchar> it, end; for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it) *it = table[*it]; break; } case 3: { MatIterator_<Vec3b> it, end; for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it) { (*it)[0] = table[(*it)[0]]; (*it)[1] = table[(*it)[1]]; (*it)[2] = table[(*it)[2]]; } } } return I;}//! [scan-iterator]
实验结果
最快的方法是LUT,因为利用了多线程。平时推荐Iterator方法,比较安全。
0 0
- OpenCV中遍历图像
- opencv中遍历图像每个像素点
- OpenCV遍历文件夹中所有图像
- opencv中遍历图像(Mat格式)
- opencv中遍历图像(IplImage格式)
- opencv图像遍历中出现的问题
- OpenCV中遍历图像的基本方式
- OpenCV中遍历图像与邻域操作
- OpenCV中图像遍历与像素操作
- opencv中Mat存储图像和遍历图像像素
- OPENCV图像遍历方法
- opencv遍历图像
- opencv 图像的遍历
- opencv的图像遍历
- OpenCV图像的遍历
- opencv之高效遍历图像
- opencv GPU 简单遍历图像
- 【OpenCV】图像遍历+像素压缩
- Java--File类
- 加班最勤奋的往往是失败者,他们只是在拼命杀死自己|真实故事
- CSS实现div的高度填满剩余空间
- Android消息推送,极光推送
- 重载与重写抽象类和接口
- OpenCV中遍历图像
- 【工作笔记】从零开始学ExtJs6( 四)—— 首页树状菜单和Tab页的实现
- Camera APP问题记录
- Android Studio插件之Sexy Editor
- CF 782C 贪心,DFS染色,水题
- MySQL5.6新特性之crash-safe slaves
- 大数据storm视频教程完整版
- ES6新属性笔记_0
- Disruptor