1. 获取图像
1.1指针访问,查找表赋值
Mat& ScanImageAndReduceC(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 * channels; int nCols = I.cols; 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; }
1.2迭代法
Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table) { // accept only char type matrices CV_Assert(I.depth() != sizeof(uchar)); 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; }
需要指出的是,OpenCV的迭代在扫描过一行中所有列后会自动跳至下一行,所以说如果在彩色图像中如果只使用一个简单的 uchar 而不是Vec3b 迭代的话就只能获得蓝色通道(B)里的值。
1.3 核心函数LUT
这是最被推荐的用于实现批量图像元素查找和更该操作图像方法。在图像处理中,对于一个给定的值,将其替换成其他的值是一个很常见的操作,OpenCV 提供里一个函数直接实现该操作,并不需要你自己扫描图像,就是:operationsOnArrays:LUT() <lut> ,一个包含于core module的函数. 首先我们建立一个mat型用于查表:
Mat lookUpTable(1, 256, CV_8U);
uchar* p = lookUpTable.data;
for( int i = 0; i < 256; ++i)
p[i] = table[i];
然后我们调用函数 (I 是输入 J 是输出):
LUT(I, lookUpTable, J);
2. 计时
double t = (double)getTickCount(); // 做点什么 ... t = ((double)getTickCount() - t)/getTickFrequency(); cout << "Times passed in seconds: " << t << endl;
3.图像修复
inpaint函数
inpaint(srcImage, maskImage, dstImage, 3, INPAINT_TELEA);
请注意,掩码网格的类型一定一定要是8比特一通道的,也就是CV_8UC1,否则很容易error。
蛮好玩儿的一点是,这个可以用来去水印,哈哈哈哈哈哈哈
4.阈值操作
阈值函数threshold
double threshold( InputArray src, OutputArray dst,
double thresh, double maxval, int type );
thresh 就是阈值;
maxval 如果满足条件,就设置成的最大值;
type:
5.与、或、取反、异或操作
bitwise_not , bitwise_xor , bitwise_or , bitwise_and
格式: bitwise_not(InputArray src, OutputArray dst, InputArray mask = noArray());