惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
博客园_首页
IT之家
IT之家
The Cloudflare Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Y
Y Combinator Blog
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
S
SegmentFault 最新的问题
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
小众软件
小众软件
有赞技术团队
有赞技术团队

博客园 - ahuo

dts删除节点方法 Ubuntu ap桥接到有线 MQTT 3.1.1 客户端如何使用共享订阅 mdns shell Ubuntu笔记本盖上不休眠 win10+ubuntu24 双系统 http代理-docker拉取 Ubuntu20 IP显示登录界面 串口启用密码或者不用密码的配置方法(/etc/inittab) mkpasswd Linux下串口的RTS控制 万用表测量MCU的GPIO trackerslist Linux串口通讯监听-jpnevulator crop_yuv420_sp Ubuntu 22 root桌面登录 Ubuntu22 向日葵黑屏 vscode ssh key登录 Samba 访问失败,提示“你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问。” Linux NAT转发
NV12数据转OpenCV的Mat
ahuo · 2024-12-20 · via 博客园 - ahuo
// 将 NV12 转换为 BGR
void nv12ToBgr(const unsigned char* yuvData, int width, int height, Mat& bgrImage) {
    // 计算每个平面的大小
    int ySize = width * height;
    int uvSize = (width / 2) * (height / 2);

    // 创建一个包含 NV12 数据的 Mat 对象
    Mat nv12(height + height / 2, width, CV_8UC1, const_cast<unsigned char*>(yuvData));

    // 直接使用 OpenCV 的颜色转换函数进行 NV12 到 BGR 的转换
    cvtColor(nv12, bgrImage, COLOR_YUV2BGR_NV12);
}
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
using namespace cv;

// 将 NV12 转换为 BGR
void nv12ToBgr(const unsigned char* yuvData, int width, int height, Mat& bgrImage) {
    // 计算每个平面的大小
    int ySize = width * height;
    int uvSize = (width / 2) * (height / 2);

    // 创建一个包含 NV12 数据的 Mat 对象
    Mat nv12(height + height / 2, width, CV_8UC1, const_cast<unsigned char*>(yuvData));

    // 直接使用 OpenCV 的颜色转换函数进行 NV12 到 BGR 的转换
    cvtColor(nv12, bgrImage, COLOR_YUV2BGR_NV12);
}

int main() {
    // OpenCV 版本号
    cout << "OpenCV_Version: " << CV_VERSION << endl;

    string filePath = "f.nv12";
    ifstream file(filePath, ios::binary | ios::ate);

    if (!file.is_open()) {
        cerr << "无法打开文件: " << filePath << endl;
        return -1;
    }

    streamsize size = file.tellg();
    file.seekg(0, ios::beg);

    vector<char> buffer(size);
    if (!file.read(buffer.data(), size)) {
        cerr << "无法读取文件: " << filePath << endl;
        return -1;
    }

    int width = 640;  // 假设宽度为 640
    int height = 480; // 假设高度为 480

    Mat bgrImage;
    nv12ToBgr(reinterpret_cast<const unsigned char*>(buffer.data()), width, height, bgrImage);

    // 显示图像
    imshow("NV12 to BGR", bgrImage);
    waitKey(0);

    return 0;
}