OpenCV中遍历图像
2017-03-06 14:54
190 查看
iterator
LUT
完整程序
实验结果
最快的方法是LUT,因为利用了多线程。平时推荐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 &l
4000
t;< "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方法,比较安全。
相关文章推荐
- opencv学习(5) 使用迭代器遍历图像的像素
- OpenCV 图像的翻转 flip实现 与遍历像素的方式实现
- opencv像素基本操作及图像遍历at
- Opencv-遍历图像的几种方法
- opencv学习之遍历图像
- opencv像素基本操作及图像遍历at
- opencv2遍历图像程序
- opencv遍历图像像素
- opencv中Mat存储图像和遍历图像像素
- OpenCV中图像遍历与像素操作
- opencv中遍历图像每个像素点
- opencv从零开始——6. 图像的读取和像素遍历
- OpenCV图像元素遍历四种方法的源码及性能对比
- opencv中遍历图像(Mat格式)
- opencv学习笔记之对灰度图像遍历的三种方法
- Opencv2系列学习笔记2(图像的遍历)
- opencv数字图像基础,提取图像像素,遍历图像
- opencv2-遍历图像像素的14种方法
- Opencv2系列学习笔记2(图像的遍历)
- 【学习opencv】opencv中遍历图像以及Mat类变量解释