OpenCV将BGR转换为NV12
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv
免费下载资源
·
NV12的格式为YYYYYYYY..UVUV..,OpenCV使用imread读出来的图像是BGR格式,但没有提供BGR直接转NV12的方法,只能先转成YUV_I420,再间接得到YUV_NV12。
cv::Mat BGR2YUV_NV12(const cv::Mat &src) {
auto src_h = src.rows;
auto src_w = src.cols;
cv::Mat dst(src_h * 1.5, src_w, CV_8UC1);
cv::cvtColor(src, dst, cv::COLOR_BGR2YUV_I420); // I420: YYYY...UU...VV...
auto n_y = src_h * src_w;
auto n_uv = n_y / 2;
auto n_u = n_y / 4;
std::vector<uint8_t> uv(n_uv);
std::copy(dst.data+n_y, dst.data+n_y+n_uv, uv.data());
for (auto i = 0; i < n_u; i++) {
dst.data[n_y + 2*i] = uv[i]; // U
dst.data[n_y + 2*i + 1] = uv[n_u + i]; // V
}
return dst;
}
可以使用如下代码将YUV_I420进行保存:
FILE* outfile = fopen("img.yuv", "wb+");
fwrite(dst.data, 1, dst.rows*dst.cols*3/2, outfile);
fclose(outfile);
播放yuv图像,想得到正确结果,必须I420格式:
ffplay -f rawvideo -video_size 1920x1080 img.yuv
关于yuv的介绍可以参考这两篇文章:
图解YU12、I420、YV12、NV12、NV21、YUV420P、YUV420SP、YUV422P、YUV444P的区别
https://blog.csdn.net/byhook/article/details/84037338
视音频数据处理入门:RGB、YUV像素数据处理
https://blog.csdn.net/leixiaohua1020/article/details/50534150
作者简介
我是阿德,一名五年工作经验的软件开发工程师,985高校非科班程序员,热爱编程与开源,
此公众号【程序员阿德】专注于分享各种计算机干货,包括但不限于计算机组成原理、数据结构与算法、计算机网络、操作系统、数据库、Linux等。
相信你能够在这里有所收获,精进为一名优秀程序员,我们一起加油~
现在关注公众号 [程序员阿德] ,回复【书籍】可以获取我精心挑选的10本经典计算机电子书,加速你的成长。
GitHub 加速计划 / opencv31 / opencv
142
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:3 个月前 )
d9a139f9
Animated WebP Support #25608
related issues #24855 #22569
### 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
- [x] 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
1 天前
09030615
V4l default image size #25500
Added ability to set default image width and height for V4L capture. This is required for cameras that does not support 640x480 resolution because otherwise V4L capture cannot be opened and failed with "Pixel format of incoming image is unsupported by OpenCV" and then with "can't open camera by index" message. Because of the videoio architecture it is not possible to insert actions between CvCaptureCAM_V4L::CvCaptureCAM_V4L and CvCaptureCAM_V4L::open so the only way I found is to use environment variables to preselect the resolution.
Related bug report is [#25499](https://github.com/opencv/opencv/issues/25499)
Maybe (but not confirmed) this is also related to [#24551](https://github.com/opencv/opencv/issues/24551)
This fix was made and verified in my local environment: capture board AVMATRIX VC42, Ubuntu 20, NVidia Jetson Orin.
### 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
- [X] 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
1 天前
更多推荐
已为社区贡献3条内容
所有评论(0)