0
我使用opencv讀取單色位圖圖像並將其像素值保存到另一個文本文件。據我所知,單色位圖的值爲0和1,而不是0和255之間。當我試圖將值保存在文本文件中時,則會存儲0和255。如果我用255分割像素值,那麼我得到0和1,但輸出不可接受,因爲它沒有形成任何字符(單色位圖圖像是帶有字符的掃描文本文件)。 我認爲深度,類型或通道數量有問題,但無法解決。請幫忙。提前致謝。在opencv文本文件中存儲單色位圖圖像像素值(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;
}