Opencv读取图片(三通道和单通道)像素并保存(像素极操作,使用at函数)

三通道:

Mat Class主要包括两部个数据部分:一个是matrix header(包括matrix的大小尺寸,储存方法,储存地址等等..),另一个是指向存储像素值的矩阵的指针。

 

#include

#include
#include

using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
  const char* imagename = "1.png";

  //从文件中读入图像
  Mat img = imread(imagename);
  ofstream outfile("3chn.txt");

  //如果读入图像失败
  if (img.empty())
  {
    fprintf(stderr, "Can not load image %s\n", imagename);
    return -1;
  }


  int i, j;
  int cPointR, cPointG, cPointB, cPoint;//currentPoint;
  for (i = 1; i < img.rows; i++) 
  {
    for (j = 1; j     {
      cPointB = img.at(i, j)[0];
      cPointG = img.at(i, j)[1];
      cPointR = img.at(i, j)[2];
      cout << "R:"<       outfile << i<<"," << j << "," <       if (cPointB>100 & cPointR<100 & cPointG<100)
      {
        img.at(i, j)[0] = 0; 
        img.at(i, j)[1] = 0;
        img.at(i, j)[2] = 0;
      }
    }
    outfile << endl;
  }

  //显示图像
  imshow("image", img);

  //此函数等待按键,按键盘任意键就返回
  waitKey();

  return 0;
}

单通道:

#include
#include
#include


using namespace std;
using namespace cv;


int main(int argc, char* argv[])
{
const char* imagename = "8.png";


//从文件中读入图像
Mat img = imread(imagename,IMREAD_UNCHANGED);
ofstream outfile("1chn.txt");


//如果读入图像失败
if (img.empty())
{
fprintf(stderr, "Can not load image %s\n", imagename);
return -1;
}


int i, j;
int cPointB, cPoint;//currentPoint;
for (i = 1; i < 80; i++)
{
for (j = 1; j<400; j++)
{
cPointB = img.at(i, j);
cout << " B:" << cPointB << endl;
outfile << i << "," << j << ",("  << cPointB << ") ";
//if (cPointB>100)
//{
// img.at(i, j) = 0; //单通道是uchar,没有[0][1][2]

//}
}
outfile << endl;
}
//显示图像
imshow("image", img);


//此函数等待按键,按键盘任意键就返回
waitKey();


return 0;
}

你可能感兴趣的:(opencv,at函数)