64,646
社区成员
发帖
与我相关
我的任务
分享
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
void write_pixel_to_txt(std::string const &name, ofstream &stream)
{
cv::Mat const image = cv::imread(name);
if(image.empty()){
std::cerr<<"input is empty"<<std::endl;
return;
}
for(int row = 0; row != image.rows; ++row){
auto ptr = image.ptr<uchar>(row);
for(int col = 0; col != image.cols; ++col){
//the channels of openCV is BGR but not RGB
stream<<row<<", "<<col<<", "<<static_cast<int>(ptr[2])<<", "<<
static_cast<int>(ptr[1])<<", "<<static_cast<int>(ptr[0])<<std::endl;
ptr += 3;
}
}
}
int main()
{
ofstream stream("church00.txt");
write_pixel_to_txt("church00.png", stream);
return 0;
}
忘了一些细节,补充