赞
踩
[先在循环外定义static int save_count]
save_count++;
std::string ssave_cnt = std::to_string(save_count);
std::string file_name = ssave_cnt+".bin";
//保存rgba数据--------------------------
FILE *fpw = fopen(file_name.c_str(), "wb");
if ( fpw == NULL )
{
std::cout << "Open error!" << std::endl;
fclose(fpw);
return;
}
if ( !img_rgba.data || img_rgba.channels() != 4 )
{
std::cout << "Image read failed or image channels isn‘t equal to 3."
<< std::endl;
return;
}
int labelw = 1;
int rows = img_rgba.rows;
int cols = img_rgba.cols;
fwrite( &labelw, sizeof(char), 1, fpw );
char* dp = (char*)img_rgba.data;
for ( int i=0; i<rows*cols; i++ )
{
fwrite( &dp[i*4], sizeof(char), 1, fpw );
fwrite( &dp[i*4+1], sizeof(char), 1, fpw );
fwrite( &dp[i*4+2], sizeof(char), 1, fpw );
fwrite( &dp[i*4+3], sizeof(char), 1, fpw );
}
fclose(fpw);
save_count++;
std::string ssave_cnt = std::to_string(save_count);
std::string file_name = "/Users/gd-npc-701/Desktop/bin_files/"+ssave_cnt+".bin";
int rows = 1920;
int cols = 1080;
FILE *fpr = fopen( file_name.c_str(), "rb" );
if ( fpr == NULL )
{
std::cout << "Open error!" << std::endl;
fclose(fpr);
return;
}
int labelr(0);
fread( &labelr, sizeof(char), 1, fpr );
std::cout << "label: " << labelr << std::endl;
cv::Mat img_rgba_from_bin( rows, cols, CV_8UC4, cv::Scalar::all(0) );
unsigned char* pData = (unsigned char*)img_rgba_from_bin.data;
for ( int i=0; i<rows*cols; i++ )
{
fread( &pData[i*4], sizeof(char), 1, fpr );
fread( &pData[i*4+1], sizeof(char), 1, fpr );
fread( &pData[i*4+2], sizeof(char), 1, fpr );
fread( &pData[i*4+3], sizeof(char), 1, fpr );
}
fclose(fpr);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。