openv将raw格式的图像转成jpg

5 篇文章 0 订阅
3 篇文章 0 订阅

在工业相机采集数据时,应该会出现raw格式的图像。

#include <iostream>
#include <io.h>
#include <chrono>

int main()
{ 
	std::string filePath = "I:/bf/cn1";
	std::vector<std::string> allFiles;
	getFiles(filePath, allFiles);
	auto aa = std::chrono::system_clock::now();

	for (int i = 0; i < allFiles.size(); ++i)
	{
		auto startTime = std::chrono::system_clock::now();

		FILE *fp = NULL;
		int ret = 0, width = 2448, height = 2048;
		unsigned char *pRawData = (unsigned char *)malloc(width*height * sizeof(unsigned char));

		if (NULL == pRawData)
		{
			printf("Fail to calloc buf\r\n");
			return -1;
		}

		if (NULL == (fp = fopen(allFiles[i].c_str(), "rb")))
		{
			printf("Fail to read %s.\r\n", allFiles[i].c_str());
			return -2;
		}

		ret = fread(pRawData, sizeof(unsigned char)*width*height, 1, fp);
		if (ret != 1)
		{
			printf("Fail to read raw data\r\n");
			return -3;
		}
		fclose(fp);
		fp = nullptr;

		Mat img(Size(width, height), CV_8UC1, pRawData);
		Mat img2(Size(width, height), CV_8UC3, Scalar(0));
		Mat img3(Size(width, height), CV_8UC3, Scalar(0));
		cvtColor(img, img2, CV_BayerRG2BGR);
		
		cvtColor(img2, img3, CV_BGR2RGB);
		std::string fileName = "offline" + std::to_string(i) + ".jpg";
		imwrite(fileName, img3);
		auto endTime = std::chrono::system_clock::now();
		// 示例
		std::cout << "time:" << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << std::endl;
	}
	auto bb = std::chrono::system_clock::now();
	std::cout << "time:" << std::chrono::duration_cast<std::chrono::milliseconds>(bb - aa).count() << std::endl;

	return -1;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值