方法大概有4中,这里只介绍其中3种:
一、基于c风格运算符[]访问
二、迭代法
三、核心函数LUT
这里第三种方法速度最快。如果使用指针的话,迭代法也不错。
以下为代码:
// b4.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <opencv245.h>
using namespace std;
using namespace cv;
//c风格运算符【】访问
void ScanP(Mat &img)
{
CV_Assert(img.depth() != sizeof(uchar));
int channels = img.channels();
int nRows = img.rows;
int nCols = img.cols * channels;
if (img.isContinuous())
{
nCols *= nRows;
nRows = 1;
}
for (int j = 0; j < nRows; j++)
{
uchar* p = img.ptr<uchar>(j);
for (int i = 0; i < nCols; i++)
{
if (p[i] > 133)
{
p[i] = 255;
}
}
}
}
//运用Iterator指针来遍历
void scanIterator(Mat &img)
{
CV_Assert(img.depth() != sizeof(uchar));
const int channels = img.channels();
switch (channels)
{
case 1:
{
MatIterator_<uch