OpenCV遍历16位图像(Updating)

OpenCV遍历16位图像

记录一下对于十六位图像的操作

使用Mat.ptr

这里使用ptr的方式和8位是一致的

1
double data21 = thermal16.ptr<ushort>(i)[j];

i 代表rows, j代表cols

使用Mat.data

对于原始的.data函数而言,其指针是面对uchar的,所以需要对其做一个强制转换

1
2
ushort *ppptr = reinterpret_cast<ushort*>(thermal16.data);
double data22 = (double)(ppptr[i*thermal16.step1() + j]);

使用Mat.at

这里使用和8位是一致的

1
double data23 = (double)(thermal16.at<ushort>(i,j));

三种方式时间上的对比

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// test0.5 测试三种遍历图像的时间
double start = (double)cv::getTickCount();
for(int i = 0; i < thermal16.rows ; i ++)
for(int j = 0; j < thermal16.cols ; j ++)
{
double data21 = thermal16.ptr<ushort>(i)[j];
}
double time = (double)cv::getTickCount() - start;
cout << "Method 1 cost " << time <<endl;

double start2 = (double)cv::getTickCount();
ushort *ppptr = reinterpret_cast<ushort*>(thermal16.data);
for(int i = 0; i < thermal16.rows ; i ++)
for(int j = 0; j < thermal16.cols ; j ++)
{
double data22 = (double)(ppptr[i*thermal16.step1() + j]);
}
double time2 = (double)cv::getTickCount() - start2;
cout << "Method 2 cost " << time2 <<endl;

double start3 = (double)cv::getTickCount();
for(int i = 0; i < thermal16.rows ; i ++)
for(int j = 0; j < thermal16.cols ; j ++)
{
double data23 = (double)(thermal16.at<ushort>(i,j));
}
double time3 = (double)cv::getTickCount() - start3;
cout << "Method 3 cost " << time3 <<endl;
1
2
3
Method 1 cost 100
Method 2 cost 40
Method 3 cost 40

所以后面两种计算时间上是差不多的