【文件属性】:
文件名称:opencv中对图像像素的访问
文件大小:979B
文件格式:SLN
更新时间:2021-12-11 09:10:32
opencv2
首次接触图像处理,通过次来记录自己的学习记录,以方便回忆。
//指针访问像素
void colorReduce(Mat& temImage, int div)
{
//行数
int rowNumber = temImage.rows;
cout << "图像通道数:" << temImage.channels() << endl;
//列数*通道数=每一行的元素个数
int colNumber = temImage.cols * temImage.channels();
for (int row = 0; row < rowNumber;row++)
{
uchar* data = temImage.ptr(row);
for (int col = 0; col < colNumber;col++)
{
data[col] = data[col] / div*div + div / 2;
}
}
}
//迭代器iterator操作像素
void iterColorReduce(Mat& temImage,int div)
{
Mat_::iterator it = temImage.begin();
Mat_::iterator itend = temImage.end();
//存取彩色图像的像素
while (it != itend)
{
//开始处理每个像素
(*it)[0] = (*it)[0] / div*div + div / 2;
(*it)[1] = (*it)[1] / div*div + div / 2;
(*it)[2] = (*it)[2] / div*div + div / 2;
++it;
}
}
//动态地址计算像素
void atColorReduce(Mat& temImage, int div)
{
int rowNumber = temImage.rows;
int colNumber = temImage.cols;
//存取彩色图像
for (int row = 0; row < rowNumber; row++)
{
for (int col = 0; col < colNumber; col++)
{
//开始处理每个图像
//蓝色通道
temImage.at(row, col)[0] = temImage.at(row, col)[0] / div*div + div / 2;
//绿色通道
temImage.at(row, col)[1] = temImage.at(row, col)[1] / div*div + div / 2;
//红色通道
temImage.at(row, col)[2] = temImage.at(row, col)[2] / div*div + div / 2;
}
}
}