本文首发于个人博客https://kezunlin.me/post/61d55ab4/,欢迎阅读!
opencv mat for loop
Series
- Part 1: compile opencv on ubuntu 16.04
- Part 2: compile opencv with CUDA support on windows 10
- Part 3: opencv mat for loop
- Part 4: speed up opencv image processing with openmp
Guide
Mat
- for gray image, use type
- for RGB color image,use type
gray format storage
color format storage: BGR
we can use method
isContinuous()
to judge whether the memory buffer is continuous or not.
color space reduction
uchar color_space_reduction(uchar pixel)
{
/*
0-9 ===>0
10-19===>10
20-29===>20
...
240-249===>24
250-255===>25
map from 256*256*256===>26*26*26
*/
int divideWith = 10;
uchar new_pixel = (pixel / divideWith)*divideWith;
return new_pixel;
}
color table
void get_color_table()
{
// cache color value in table[256]
int divideWith = 10;
uchar table[256];
for (int i = 0; i < 256; i)
table[i] = divideWith* (i / divideWith);
}
C
ptr []
// C ptr []: faster but not safe
Mat& ScanImageAndReduce_Cptr(Mat& I, const uchar* const table)
{
// accept only char type matrices
CV_Assert(I.depth() != sizeof(uchar));
int channels = I.channels();
int nRows = I.rows;
int nCols = I.cols* channels;
if (I.isContinuous())
{
nCols *= nRows;
nRows = 1;
}
int i, j;
uchar* p;
for (i = 0; i < nRows; i)
{
p = I.ptr<uchar>(i);
for (j = 0; j < nCols; j)
{
p[j] = table[p[j]];
}
}
return I;
}
ptr
// C ptr : faster but no