Dat文件转为AVI格式(不存成图片)

#include<opencv2\opencv.hpp>
#include"LoadData.h"

using namespace cv;
using namespace std;

Mat  img14to8(ushort *in, int rows, int cols);

//红外文件名
string LoadFileName = "D:\\UAVS\\1200_1.5-1km\\ir_data_file_20210419_121553001.dat";



#define IMG_WIDTH           640
#define IMG_HEIGHT          512
#define IMG_BITWIDTH        16384 //2^14
//#define SAVEIMG2VIDEO      
//#define SAVEIRIMG
#define IMG_TYPE            ".jpg"
#define  IMG_DATA

unsigned short(imagedata[IMG_WIDTH*IMG_HEIGHT]);
unsigned short Hist[IMG_BITWIDTH];


int main()
{

	Mat outputImage(IMG_HEIGHT, IMG_WIDTH, CV_8UC1, Scalar(0));

	FILE *pFile;
	pFile = fopen(LoadFileName.c_str(), "rb");

	//=============== 计算Dat文件长度 ===============//	
	struct _stati64 statbuf;
	_stati64(LoadFileName.c_str(), &statbuf);
	printf("文件大小为%dB\n", statbuf.st_size);

	//=============== 查看文件是否存在字节头 ===============//
	float fByteTitle = ((((double)statbuf.st_size)) / (sizeof(unsigned short) * IMG_WIDTH * IMG_HEIGHT));
	long long DatNumberSize;
	if ((int)fByteTitle == fByteTitle)
	{
		DatNumberSize = (long long)(((statbuf.st_size)) / (sizeof(unsigned short) * IMG_WIDTH * IMG_HEIGHT ));
		printf("图片数量为%ld张\n", DatNumberSize);
	}
	else
	{
		DatNumberSize = (long long)(((statbuf.st_size)) / (sizeof(unsigned short) * IMG_WIDTH * IMG_HEIGHT + 640));
		printf("图片数量为%ld张\n", DatNumberSize);
	}

	//=============== 保存视频 ===============//	
	string LoadDat = LoadFileName;
	VideoWriter videoWriter;
	String Dat2Video = LoadDat.erase(LoadDat.size() - 4, 4).append(".avi");
	videoWriter.open(Dat2Video, CV_FOURCC('M', 'J', 'P', 'G'), 60, Size(IMG_WIDTH, IMG_HEIGHT));

	//=============== 从指定位置读取 ===============//
	//fseek(pFile, (640 * 512 + 320) * sizeof(unsigned short) * frameNumber, 0);
	long long frameNumber = 0;
	if ((int)fByteTitle == fByteTitle)
	{
		_fseeki64(pFile, (IMG_WIDTH*IMG_HEIGHT) * sizeof(unsigned short) * frameNumber, 0);
	}
	else
	{
		_fseeki64(pFile, (IMG_WIDTH*IMG_HEIGHT + 320) * sizeof(unsigned short) * frameNumber, 0);
	}
	while (frameNumber < DatNumberSize)
	{
			
		//=============== 输出文件指针位置 ===============//
		long long CurrPtrPosition;
		CurrPtrPosition = _ftelli64(pFile);
		//cout << "CurrPtrPosition:" << CurrPtrPosition << endl;
		if ((int)fByteTitle== fByteTitle)
		{
			fread(imagedata, sizeof(unsigned short), IMG_HEIGHT*IMG_WIDTH, pFile);
		}
		else
		{
			unsigned short usIrImgData1[IMG_WIDTH*IMG_HEIGHT + 320] = { 0 };   	//红外原图图像存储
			fread(usIrImgData1, sizeof(short), IMG_WIDTH*IMG_HEIGHT + 320, pFile);
			memcpy(imagedata, usIrImgData1 + 320, IMG_WIDTH*IMG_HEIGHT * 2);
		}

		outputImage = img14to8(imagedata, IMG_HEIGHT, IMG_WIDTH);
		cvtColor(outputImage, outputImage, CV_GRAY2BGR);
		//flip(outputImage, outputImage, 0);
		imshow("outputImage", outputImage);
		cout << "正在处理第" << frameNumber << "帧" << endl;
		waitKey(1);
		videoWriter.write(outputImage);

		frameNumber++;
	}
	videoWriter.release();
	destroyWindow("outputImage");

	return true;
}
	



Mat img14to8(ushort *in, int rows, int cols)
{
	Mat out(rows, cols, CV_8UC1);
	int inc = 0, minValue = 0, maxValue = 0;
	double factor = 0.0, middle = 0.0;
	unsigned char hist[640 * 512];
	memset(hist, 0, sizeof(hist));
	for (size_t i = 0; i < rows; i++)
	{
		for (size_t j = 0; j < cols; j++)
		{
			if (in[i*cols + j] > 0 && in[i*cols + j] < 16384)
				hist[in[i*cols + j]] = hist[in[i*cols + j]] + 1;
		}
	}

	for (size_t i = 0; i < 16384; i++)
	{
		if ((inc < 2000) && ((inc + hist[i]) > 1999))
		{
			minValue = i;
			break;
		}
		inc = inc + hist[i];
	}

	inc = 0;
	for (size_t i = 16383; i > 0; i--)
	{
		if ((inc < 2000) && ((inc + hist[i]) > 1999))
		{
			maxValue = i;
			break;
		}
		inc = inc + hist[i];
	}

	maxValue = maxValue + 32;
	minValue = minValue - 32;
	factor = 1.0 / (maxValue - minValue) * 255;

	for (size_t i = 0; i < rows; i++)
	{
		for (size_t j = 0; j < cols; j++)
		{
			if (in[i*cols + j] == 0)
				out.ptr<uchar>(i)[j] = 0;
			else
			{
				middle = (in[i*cols + j] - minValue)*factor;
				if (middle>255)
					out.ptr<uchar>(i)[j] = 255;
				else if (middle<0)
					out.ptr<uchar>(i)[j] = 0;
				else
					out.ptr<uchar>(i)[j] = uchar(middle);
			}
		}
	}

	return out;
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值