在opencv中读取像素值

时间:2019-07-15 14:28:32

标签: c++ opencv

我已读取图像的像素值并将其保存在.txt文件中,但我想在txt文件中仅包含0和255

我做了一些编码,代码成功编译,但是txt文件没有打开。

plzzz帮帮我。

#include <opencv2/opencv.hpp>
using namespace cv;

#include <fstream>
using namespace std;


int main()
{ 
    try
  {
    throw 20;
  }
  catch (int e)
  {
    cout << "An exception occurred. Exception Nr. " << e << '\n';
  }


    Mat colorImage = imread(path);

    // Open the file in write mode.
    ofstream outputFile;
    outputFile.open("name.txt");

    // Iterate through pixels.
    for (int r = 0; colorImage.rows < r ; r++)
    {
        for (int c = 0; colorImage.cols < c ; c++)
          {

            int pixel = colorImage.at<uchar>(r,c);


            if ( pixel < 70)
            { 
                pixel = 0;
            }
            else pixel = 255;

            outputFile << pixel << '\t';
        }
        outputFile << endl;
    }

    // Close the file.
    outputFile.close();
    return 0;
}

0 个答案:

没有答案