void ReadRaw8(){
	std::string strFilename = "raw8.raw";
	int nWidth = 1456;
	int nHeight = 1096;

	uint8_t* pRawbuf = new uint8_t[(size_t)nWidth * nHeight];
	if (!pRawbuf) {
		std::cout << "ERROR: 开辟内存失败!" << std::endl;
		return;
	}
	FILE* pfile = nullptr;
	errno_t err_code = fopen_s(&pfile, strFilename.c_str(), "rb");
	if (!pfile)
	{
		std::cout << "ERROR: 打开文件失败!" << std::endl;
		return;
	}
	fread(pRawbuf, 1, ((size_t)nWidth * nHeight), pfile);
	fclose(pfile);
	pfile = nullptr;

	cv::Mat img(nHeight, nWidth, CV_8UC1, pRawbuf);
	cv::namedWindow(strFilename, cv::WINDOW_NORMAL);
	cv::imshow(strFilename, img);
	cv::waitKey(0);
	cv::Mat rgb;
	cv::Mat gray;
	cv::cvtColor(img, rgb, cv::COLOR_BayerRG2RGB);
	cv::imshow(strFilename, rgb);
	cv::waitKey(0);

	cv::cvtColor(rgb, gray, cv::COLOR_RGB2GRAY);
	cv::imshow(strFilename, gray);
	cv::waitKey(0);
}

void ReadRaw10(){
	// 读取raw10图片
	std::string strFilename = "raw10.raw";
	int nWidth = 4208;
	int nHeight = 3120;
	short* pRaw10buf = new short[(size_t)nWidth * nHeight];
	if (!pRaw10buf)
	{
		std::cout << "ERROR: 开辟内存失败!" << std::endl;
		return;
	}
	FILE* pfile = nullptr;
	errno_t err_code = fopen_s(&pfile, strFilename.c_str(), "rb");
	if (!pfile) {
		std::cout << "ERROR: 打开文件失败!" << std::endl;
		return;
	}
	fread(pRaw10buf, sizeof(pRaw10buf[0]), (size_t)nWidth * nHeight, pfile);
	fclose(pfile);
	pfile = nullptr;

	cv::Mat raw10Img(nHeight, nWidth, CV_16SC1, pRaw10buf);
	cv::namedWindow(strFilename, cv::WINDOW_NORMAL);
	cv::imshow(strFilename, raw10Img);
	cv::waitKey(0);
    //  以下运行会崩溃,因为rgb和gray都是8U类型的数据,而raw10Img是16S,数据溢出,因此需要转换位raw8之后再转成RGB或BGR图像
    cv::Mat img_raw8;
	cv::convertScaleAbs(img, img_raw8, 0.25);
	cv::imshow(strFilename, img_raw8);
	cv::waitKey(0);

	cv::Mat rgb;
	cv::Mat gray;
	cv::cvtColor(img_raw8, rgb, cv::COLOR_BayerRG2RGB);
	cv::imshow(strFilename, rgb);
	cv::waitKey(0);

	cv::cvtColor(rgb, gray, cv::COLOR_RGB2GRAY);
	cv::imshow(strFilename, gray);
	cv::waitKey(0);
}

需要注意的是:

1. 在debug下运行可能会崩溃,改成release可以正常运行;
2. raw转rgb24时应该选择*2RGB,否则转换后的图片会偏色;
3. 读取raw10图片后如果要转成RGB或BGR图像,需要先转成raw8格式,即保证每个像素的值在0~255之间,否则会抛出异常。
 

GitHub 加速计划 / opencv31 / opencv
77.36 K
55.71 K
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:1 个月前 )
3919f33e G-API: Introduce level optimization flag for ONNXRT backend #26293 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake 21 小时前
489df18a Use border value in ipp version of warp affine #26313 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 23 小时前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐