文章选自:
一、像素访问
一张图片由许多个点组成,每个点就是一个像素,每个像素包含不同的值,对图像像素操作是图像处理过程中常使用的
二、访问像素
void Samples::AccessPixels1(Mat &image, int div = 64) {
int nl = image.rows; //行数
int nc = image.cols * image.channels(); //每行元素的总元素数量
for (int j = 0; j < nl; j++) {
uchar *data = image.ptr<uchar>(j);
for (int i = 0; i < nc; i++) {
//-------------开始处理每个像素-------------------
data[i] = data[i] / div * div + div / 2;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
//-----------------------------------【方法二】-------------------------------------------------
// 说明:利用 .ptr 和 * ++
//-------------------------------------------------------------------------------------------------
void Samples::AccessPixels2(Mat &image, int div = 64) {
int nl = image.rows; //行数
int nc = image.cols * image.channels(); //每行元素的总元素数量
for (int j = 0; j < nl; j++) {
uchar *data = image.ptr<uchar>(j);
for (int i = 0; i < nc; i++) {
//-------------开始处理每个像素-------------------
*data++ = *data / div * div + div / 2;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
//-----------------------------------------【方法三】-------------------------------------------
// 说明:利用.ptr 和 * ++ 以及模操作
//-------------------------------------------------------------------------------------------------
void Samples::AccessPixels3(Mat &image, int div = 64) {
int nl = image.rows; //行数
int nc = image.cols * image.channels(); //每行元素的总元素数量
for (int j = 0; j < nl; j++) {
uchar *data = image.ptr<uchar>(j);
for (int i = 0; i < nc; i++) {
//-------------开始处理每个像素-------------------
int v = *data;
*data++ = v - v % div + div / 2;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
//----------------------------------------【方法四】---------------------------------------------
// 说明:利用.ptr 和 * ++ 以及位操作
//----------------------------------------------------------------------------------------------------
void Samples::AccessPixels4(Mat &image, int div = 64) {
int nl = image.rows; //行数
int nc = image.cols * image.channels(); //每行元素的总元素数量
int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));
//掩码值
uchar mask = 0xFF << n; // e.g. 对于 div=16, mask= 0xF0
for (int j = 0; j < nl; j++) {
uchar *data = image.ptr<uchar>(j);
for (int i = 0; i < nc; i++) {
//------------开始处理每个像素-------------------
*data++ = *data & mask + div / 2;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
//----------------------------------------【方法五】----------------------------------------------
// 说明:利用指针算术运算
//---------------------------------------------------------------------------------------------------
void Samples::AccessPixels5(Mat &image, int div = 64) {
int nl = image.rows; //行数
int nc = image.cols * image.channels(); //每行元素的总元素数量
int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));
int step = image.step; //有效宽度
//掩码值
uchar mask = 0xFF << n; // e.g. 对于 div=16, mask= 0xF0
//获取指向图像缓冲区的指针
uchar *data = image.data;
for (int j = 0; j < nl; j++) {
for (int i = 0; i < nc; i++) {
//-------------开始处理每个像素-------------------
*(data + i) = *data & mask + div / 2;
//-------------结束像素处理------------------------
} //单行处理结束
data += step; // next line
}
}
//---------------------------------------【方法六】----------------------------------------------
// 说明:利用 .ptr 和 * ++以及位运算、image.cols * image.channels()
//-------------------------------------------------------------------------------------------------
void Samples::AccessPixels6(Mat &image, int div = 64) {
int nl = image.rows; //行数
int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));
//掩码值
uchar mask = 0xFF << n; // e.g. 例如div=16, mask= 0xF0
for (int j = 0; j < nl; j++) {
uchar *data = image.ptr<uchar>(j);
for (int i = 0; i < image.cols * image.channels(); i++) {
//-------------开始处理每个像素-------------------
*data++ = *data & mask + div / 2;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
// -------------------------------------【方法七】----------------------------------------------
// 说明:利用.ptr 和 * ++ 以及位运算(continuous)
//-------------------------------------------------------------------------------------------------
void Samples::AccessPixels7(Mat &image, int div = 64) {
int nl = image.rows; //行数
int nc = image.cols * image.channels(); //每行元素的总元素数量
if (image.isContinuous()) {
//无填充像素
nc = nc * nl;
nl = 1; // 为一维数列
}
int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));
//掩码值
uchar mask = 0xFF << n; // e.g. 比如div=16, mask= 0xF0
for (int j = 0; j < nl; j++) {
uchar *data = image.ptr<uchar>(j);
for (int i = 0; i < nc; i++) {
//-------------开始处理每个像素-------------------
*data++ = *data & mask + div / 2;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
//------------------------------------【方法八】------------------------------------------------
// 说明:利用 .ptr 和 * ++ 以及位运算 (continuous+channels)
//-------------------------------------------------------------------------------------------------
void Samples::AccessPixels8(Mat &image, int div = 64) {
int nl = image.rows; //行数
int nc = image.cols; //列数
if (image.isContinuous()) {
//无填充像素
nc = nc * nl;
nl = 1; // 为一维数组
}
int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));
//掩码值
uchar mask = 0xFF << n; // e.g. 比如div=16, mask= 0xF0
for (int j = 0; j < nl; j++) {
uchar *data = image.ptr<uchar>(j);
for (int i = 0; i < nc; i++) {
//-------------开始处理每个像素-------------------
*data++ = *data & mask + div / 2;
*data++ = *data & mask + div / 2;
*data++ = *data & mask + div / 2;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
// -----------------------------------【方法九】 ------------------------------------------------
// 说明:利用Mat_ iterator
//-------------------------------------------------------------------------------------------------
void Samples::AccessPixels9(Mat &image, int div = 64) {
//获取迭代器
Mat_<Vec3b>::iterator it = image.begin<Vec3b>();
Mat_<Vec3b>::iterator itend = image.end<Vec3b>();
for (; it != itend; ++it) {
//-------------开始处理每个像素-------------------
(*it)[0] = (*it)[0] / div * div + div / 2;
(*it)[1] = (*it)[1] / div * div + div / 2;
(*it)[2] = (*it)[2] / div * div + div / 2;
//-------------结束像素处理------------------------
} //单行处理结束
}
//-------------------------------------【方法十】-----------------------------------------------
// 说明:利用Mat_ iterator以及位运算
//-------------------------------------------------------------------------------------------------
void Samples::AccessPixels10(Mat &image, int div = 64) {
// div必须是2的幂
int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));
//掩码值
uchar mask = 0xFF << n; // e.g. 比如 div=16, mask= 0xF0
// 获取迭代器
Mat_<Vec3b>::iterator it = image.begin<Vec3b>();
Mat_<Vec3b>::iterator itend = image.end<Vec3b>();
//扫描所有元素
for (; it != itend; ++it) {
//-------------开始处理每个像素-------------------
(*it)[0] = (*it)[0] & mask + div / 2;
(*it)[1] = (*it)[1] & mask + div / 2;
(*it)[2] = (*it)[2] & mask + div / 2;
//-------------结束像素处理------------------------
} //单行处理结束
}
//------------------------------------【方法十一】---------------------------------------------
// 说明:利用Mat Iterator_
//-------------------------------------------------------------------------------------------------
void Samples::AccessPixels11(Mat &image, int div = 64) {
//获取迭代器
Mat_<Vec3b> cimage = image;
Mat_<Vec3b>::iterator it = cimage.begin();
Mat_<Vec3b>::iterator itend = cimage.end();
for (; it != itend; it++) {
//-------------开始处理每个像素-------------------
(*it)[0] = (*it)[0] / div * div + div / 2;
(*it)[1] = (*it)[1] / div * div + div / 2;
(*it)[2] = (*it)[2] / div * div + div / 2;
//-------------结束像素处理------------------------
}
}
void Samples::AccessPixels12(Mat &image, int div = 64) {
int nl = image.rows; //行数
int nc = image.cols; //列数
for (int j = 0; j < nl; j++) {
for (int i = 0; i < nc; i++) {
//-------------开始处理每个像素-------------------
image.at<Vec3b>(j, i)[0] = image.at<Vec3b>(j, i)[0] / div * div + div / 2;
image.at<Vec3b>(j, i)[1] = image.at<Vec3b>(j, i)[1] / div * div + div / 2;
image.at<Vec3b>(j, i)[2] = image.at<Vec3b>(j, i)[2] / div * div + div / 2;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
//----------------------------------【方法十三】-----------------------------------------------
// 说明:利用图像的输入与输出
//-------------------------------------------------------------------------------------------------
void Samples::AccessPixels13(const Mat &image, //输入图像
Mat & result, // 输出图像
int div = 64) {
int nl = image.rows; //行数
int nc = image.cols; //列数
//准备好初始化后的Mat给输出图像
result.create(image.rows, image.cols, image.type());
//创建无像素填充的图像
nc = nc * nl;
nl = 1; //单维数组
int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));
//掩码值
uchar mask = 0xFF << n; // e.g.比如div=16, mask= 0xF0
for (int j = 0; j < nl; j++) {
uchar * data = result.ptr<uchar>(j);
const uchar *idata = image.ptr<uchar>(j);
for (int i = 0; i < nc; i++) {
//-------------开始处理每个像素-------------------
*data++ = (*idata++) & mask + div / 2;
*data++ = (*idata++) & mask + div / 2;
*data++ = (*idata++) & mask + div / 2;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
//--------------------------------------【方法十四】-------------------------------------------
// 说明:利用操作符重载
//-------------------------------------------------------------------------------------------------
void Samples::AccessPixels14(Mat &image, int div = 64) {
int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));
//掩码值
uchar mask = 0xFF << n; // e.g. 比如div=16, mask= 0xF0
//进行色彩还原
image = (image & Scalar(mask, mask, mask)) + Scalar(div / 2, div / 2, div / 2);
}