Opencv读取图片(三通道和单通道)像素并保存

三通道:

#include <opencv2/opencv.hpp>

#include<vector>
#include <fstream>

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<img.cols; j++)
    {
      cPointB = img.at<Vec3b>(i, j)[0];
      cPointG = img.at<Vec3b>(i, j)[1];
      cPointR = img.at<Vec3b>(i, j)[2];
      cout << "R:"<<cPointR<<" G:"<<cPointG <<" B:"<<cPointB<< endl;
      outfile << i<<"," << j << "," <<cPointR << "," << cPointG << "," << cPointB << " ";
      if (cPointB>100 & cPointR<100 & cPointG<100)
      {
        img.at<Vec3b>(i, j)[0] = 0; 
        img.at<Vec3b>(i, j)[1] = 0;
        img.at<Vec3b>(i, j)[2] = 0;
      }
    }
    outfile << endl;
  }

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

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

  return 0;
}

单通道:

#include <opencv2/opencv.hpp>
#include<vector>
#include <fstream>


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<ushort>(i, j);
cout << " B:" << cPointB << endl;
outfile << i << "," << j << ",("  << cPointB << ") ";
//if (cPointB>100)
//{
// img.at<ushort>(i, j) = 0; //单通道是uchar,没有[0][1][2]

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


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


return 0;
}


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值