在opencv中的文本文件中存储单色位图图像像素值(0,1)

时间:2017-04-16 19:30:53

标签: c++ opencv bitmap

我使用opencv读取单色位图图像并将其像素值保存到另一个文本文件中。根据我的知识,单色位图的值为0和1,而不是0到255之间。当我尝试将值保存在文本文件中时,则会存储0和255。如果我将像素值除以255然后我得到0和1但输出是不可接受的,因为它没有形成任何字符(单色位图图像是带有字符的扫描文本文件)。 我认为渠道的深度,类型或数量存在问题,但无法解决。请帮忙。提前谢谢。

这是我的代码:

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include "math.h"
#include <fstream>
using namespace cv;

using namespace std;

int main(int argc,char **argv)
{
ofstream fout("monochrome_file.txt");
Mat img=imread("1_mono.bmp",CV_THRESH_BINARY);
uchar val;int x;
 if(img.empty())
{
    cout<<"File Not Opened"<<endl;  
}
for(int i=0;i<img.rows;i++)
{
    for(int j=0;j<img.cols;j++)
    {

        val=img.at<uchar>(i,j);
        x=(int)val;
                    x=x/255;
                    fout<<x;
    }

}

waitKey();
return 0;
}

1 个答案:

答案 0 :(得分:0)

在visual studio中打开monochrome_file.txt而不是记事本。所需的模式将可见。

相关问题