百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

[图像处理] YUV图像处理入门5

bigegpt 2024-09-12 11:27 10 浏览

12 yuv420转换为rgb(opencv mat)

yuv格式具有亮度信息和色彩信息分离的特点,但大多数图像处理操作都是基于RGB格式,而且自己造轮子工作量太大。因此通常都会将yuv转换为rgb,再用opencv等视觉库进行图像处理。

yuv转换为rgb有多种方法,比如公式法。但是推荐使用第三方库进行转换,比如ffmpeg,libyuv,opencv。其中ffmpeg是专门的视频音频处理软件,libyuv是谷歌开发的专门用于yuv基本图像处理(如旋转,缩放,格式转换)的视频库,libyuv主要用于android端。

ffmpeg,libyuv,opencv都是开源的。可以在网上查找资料。

本文简单介绍ffmpeg和libyuv的安装,opencv的安装教程很多就不介绍了。具体见文章:

https://blog.csdn.net/weixin_39393712/article/details/79583274

ffmpeg和libyuv的安装:

下载最新的ffmpeg的dev版和share版,ffmpeg严格区分x64和x86。下载网站为:

http://ffmpeg.zeranoe.com/builds/

Libyuv需要编译源文件,源文件地址:

https://chromium.googlesource.com/libyuv/libyuv/

https://github.com/seungrye/libyuv 。

编译步骤见:

https://blog.csdn.net/aabcd123456/article/details/78982528

获得源文件后先建立vs工程,然后将ffmpegdev版本文件夹中的include和lib整个目录复制到vs工程目录下。如图所示:

添加图片注释,不超过 140 字(可选)

对于libyuv的libyuv文件和lib文件,将其分别复制到vs工程目录下的include目录和lib目录。如图所示:

添加图片注释,不超过 140 字(可选)

添加图片注释,不超过 140 字(可选)

通常include中包含的是所调用库头文件,lib包含的是静态链接库,当然ffmpeg需要将其动态链接库复制到vs工程目录下,即将ffmpeg,share版本文件夹中bin目录下对应的所有dll复制到项目路径下如图所示::

添加图片注释,不超过 140 字(可选)

Dll和lib是windows系统下的动态链接库和静态链接库,linux系统下的静态链接库以.a结尾,linux系统下的动态链接库以.so或.so.y结尾。具体可以见文章:

https://www.cnblogs.com/general001/articles/3567446.html

对于ffmpeg,libyuv在linux系统下的编译使用,通过编译下载相关源代码,通过cmake或者make命令进行项目构建。推荐使用cmake软件,cmake非常有用,应用十分广泛。入门教程见:

http://www.cnblogs.com/52php/p/5681745.html

在windows平台下,通过vs就能够减少大量工作。vs平台链接ffmpeg和libyuv的头文件和lib文件,先在项目工程属性>C/C++>常规>附加包含目录,添加include目录,但是ffmpeg有许多错误,vs通常会开启SDL检查后,某些警告会成为错误。所以将sdl检查置为否。如下图所示:

添加图片注释,不超过 140 字(可选)

接着在在项目工程属性>链接器>常规>附加库目录下,添加lib文件夹,如下图所示:

添加图片注释,不超过 140 字(可选)

最后如果使用ffmpeg和libyuv库,需要添加头文件完成整个工作的配置。代码如下:


extern "C"

{

#include "include\libavcodec\avcodec.h"

#include "include\libavformat\avformat.h"

#include "include\libavutil\channel_layout.h"

#include "include\libavutil\common.h"

#include "include\libavutil\imgutils.h"

#include "include\libswscale\swscale.h"

#include "include\libavutil\imgutils.h"

#include "include\libavutil\opt.h"

#include "include\libavutil\mathematics.h"

#include "include\libavutil\samplefmt.h"

//libyuv

#include "include\libyuv\libyuv.h"

};

#pragma comment(lib, "avcodec.lib")

#pragma comment(lib, "avformat.lib")

#pragma comment(lib, "avdevice.lib")

#pragma comment(lib, "avfilter.lib")

#pragma comment(lib, "avutil.lib")

#pragma comment(lib, "postproc.lib")

#pragma comment(lib, "swresample.lib")

#pragma comment(lib, "swscale.lib")

//libyuv

#pragma comment(lib, "yuv.lib")

yuv420转rgb

接下来通过ffmpeg,libyuv,opencv实现yuv420转rgb,并进行性能分析。函数的代码如下所示:


/**
 * @file 12 yuv_transform.cpp
 * @author luohen
 * @brief YUV image transform to opencv rgb image
 * @date 2018-12-11
 * 
 */

#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <time.h>

extern "C"
{
#include "include\libavcodec\avcodec.h"
#include "include\libavformat\avformat.h"
#include "include\libavutil\channel_layout.h"
#include "include\libavutil\common.h"
#include "include\libavutil\imgutils.h"
#include "include\libswscale\swscale.h"
#include "include\libavutil\imgutils.h"
#include "include\libavutil\opt.h"
#include "include\libavutil\mathematics.h"
#include "include\libavutil\samplefmt.h"
//libyuv
#include "include\libyuv\libyuv.h"
};
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "swscale.lib")
//libyuv
#pragma comment(lib, "yuv.lib")

using namespace std;
using namespace cv;

/**
 * @brief
 *
 * @param pYUV        input yuv420 image
 * @param pBGR24     output bgr24 image
 * @param width        width of input yuv420p image
 * @param height    height of input yuv420p image
 * @return
 */
bool ffmpeg_yuv2bgr(unsigned char *pYUV, unsigned char *pBGR24, int width, int height)
{
    AVPicture pFrameYUV, pFrameBGR;

    avpicture_fill(&pFrameYUV, pYUV, AV_PIX_FMT_YUV420P, width, height);
    avpicture_fill(&pFrameBGR, pBGR24, AV_PIX_FMT_BGR24, width, height);

    struct SwsContext *imgCtx = NULL;
    //初始化函数
    //原图高,宽,图像类型;输出图高,宽,图像类型;算法种类;其他
    imgCtx = sws_getContext(width, height, AV_PIX_FMT_YUV420P, width, height, AV_PIX_FMT_BGR24, SWS_BILINEAR, 0, 0, 0);

    if (imgCtx != NULL)
    {
        //执行函数
        //函数返回值;输入图像指针数组,图像颜色通道数组;扫描起点;扫描行数;输出图像指针数组,图像颜色通道数组;
        sws_scale(imgCtx, pFrameYUV.data, pFrameYUV.linesize, 0, height, pFrameBGR.data, pFrameBGR.linesize);
        //end
        if (imgCtx)
        {
            sws_freeContext(imgCtx);
            imgCtx = NULL;
        }
        return true;
    }
    else
    {
        sws_freeContext(imgCtx);
        imgCtx = NULL;
        return false;
    }
}

/**
 * @brief        transform function of ffmpeg
 *
 * @param w        width of input yuv420p image
 * @param h        height of input yuv420p image
 * @param pic    input yuv image
 * @return Mat    output rgb image(opencv mat)
 */
Mat yuv420_ffmpeg(int w, int h, unsigned char *pic)
{
    Mat bgrImg(h, w, CV_8UC3);
    unsigned char *pBGR24 = new unsigned char[w * h * 3];
    ffmpeg_yuv2bgr(pic, bgrImg.data, w, h);

    return bgrImg;
}

/**
 * @brief        transform function of libyuv
 *
 * @param w        width of input yuv420p image
 * @param h        height of input yuv420p image
 * @param pic    input yuv image
 * @return Mat    output rgb image(opencv mat)
 */
Mat yuv420_libyuv(int w, int h, unsigned char *pic)
{
    int size_src = w * h * 3 / 2;
    int size_dest = w * h * 4;

    //BGRA, A:Alpha(transparency,透明度)
    Mat matI420 = cv::Mat(h, w, CV_8UC4);

    libyuv::I420ToARGB((const uint8 *)pic, w,
                       (const uint8 *)(pic + w * h), w / 2,
                       (const uint8 *)(pic + w * h * 5 / 4), w / 2,
                       matI420.data, w * 4, w, h);
    //bgr
    Mat bgrImg;
    cvtColor(matI420, bgrImg, COLOR_BGRA2BGR);
    return bgrImg;
}

/**
 * @brief
 *
 * @param w
 * @param h
 * @param pic
 * @return Mat
 */
Mat yuv420_opencv(int w, int h, unsigned char *pic)
{
    //创建YUV mat
    cv::Mat yuvImg;
    yuvImg.create(h * 3 / 2, w, CV_8UC1);
    //数据保存为yuvImg.data
    memcpy(yuvImg.data, pic, w * h * 3 / 2 * sizeof(unsigned char));

    //转化为RGB图像
    cv::Mat bgrImg;
    cv::cvtColor(yuvImg, bgrImg, CV_YUV2BGR_I420);

    return bgrImg;
}

/**
 * @brief main
 *
 * @return int
 */
int main()
{
    clock_t start, end;
    double endtime;
    //Frequency of reading image
    int count_frame = 300;
    //视频路径
    char *url = (char *)"video/akiyo.yuv";
    int w = 352, h = 288;
    FILE *input_fp;
    if ((input_fp = fopen(url, "rb")) == NULL)
    {
        printf("%s open error!\n", url);
        return -1;
    }
    else
    {
        printf("%s open.\n", url);
    }

    unsigned char *pYuvBuf = new unsigned char[w * h * 3 / 2];

    fseek(input_fp, 0, SEEK_SET);
    //Timing starts
    start = clock();
    Mat ffmpeg_mat;
    for (int i = 0; i < count_frame; i++)
    {
        fread(pYuvBuf, sizeof(unsigned char), w * h * 3 / 2, input_fp);
        ffmpeg_mat = yuv420_ffmpeg(w, h, pYuvBuf);
    }

    //Timing end
    end = clock();
    endtime = (double)(end - start) / CLOCKS_PER_SEC;
    cout << "ffmpeg Total time:" << endtime << "s" << endl;
    cout << "ffmpeg Total time:" << endtime * 1000 << "ms" << endl;

    fseek(input_fp, 0, SEEK_SET);
    start = clock();
    Mat libyuv_mat;
    for (int i = 0; i < count_frame; i++)
    {
        fread(pYuvBuf, sizeof(unsigned char), w * h * 3 / 2, input_fp);
        libyuv_mat = yuv420_libyuv(w, h, pYuvBuf);
    }
    end = clock();
    endtime = (double)(end - start) / CLOCKS_PER_SEC;
    cout << "libyuv Total time:" << endtime << "s" << endl;            //s为单位
    cout << "libyuv Total time:" << endtime * 1000 << "ms" << endl; //ms为单位

    fseek(input_fp, 0, SEEK_SET);
    start = clock();
    Mat opencv_mat;
    for (int i = 0; i < count_frame; i++)
    {
        fread(pYuvBuf, sizeof(unsigned char), w * h * 3 / 2, input_fp);
        opencv_mat = yuv420_opencv(w, h, pYuvBuf);
    }
    end = clock();
    endtime = (double)(end - start) / CLOCKS_PER_SEC;
    cout << "opencv Total time:" << endtime << "s" << endl;
    cout << "opencv Total time:" << endtime * 1000 << "ms" << endl;

    system("pause");
    return 0;
}

调用函数为:


Mat yuv420_ffmpeg(int w, int h, unsigned char *pic);

Mat yuv420_libyuv(int w, int h, unsigned char *pic);

Mat yuv420_opencv(int w, int h, unsigned char *pic);

这段代码主要是分别用ffmpeg,libyuv,opencv实现yuv420转换为rgb,每种方法转换300张yuv420图像。对比三种方法转换所用时间,结果如下:

添加图片注释,不超过 140 字(可选)

综合三种方法来说,ffmpeg速度最快,且ffmpeg最常用,因此推荐使用ffmpeg。如果仅仅对yuv图像进行处理或者android端,libyuv最为推荐。如果是安装ffmpeg或者libyuv较为麻烦,仅限于研究项目,建议使用opencv。


引用链接

[1] https://blog.csdn.net/weixin_39393712/article/details/79583274: https://blog.csdn.net/weixin_39393712/article/details/79583274

[2] http://ffmpeg.zeranoe.com/builds/ : http://ffmpeg.zeranoe.com/builds/

[3] https://chromium.googlesource.com/libyuv/libyuv/ : https://chromium.googlesource.com/libyuv/libyuv/

[4] https://github.com/seungrye/libyuv : https://github.com/seungrye/libyuv

[5] https://blog.csdn.net/aabcd123456/article/details/78982528 : https://blog.csdn.net/aabcd123456/article/details/78982528

[6] https://www.cnblogs.com/general001/articles/3567446.html : https://www.cnblogs.com/general001/articles/3567446.html

[7] http://www.cnblogs.com/52php/p/5681745.html : http://www.cnblogs.com/52php/p/5681745.html

相关推荐

当Frida来“敲”门(frida是什么)

0x1渗透测试瓶颈目前,碰到越来越多的大客户都会将核心资产业务集中在统一的APP上,或者对自己比较重要的APP,如自己的主业务,办公APP进行加壳,流量加密,投入了很多精力在移动端的防护上。而现在挖...

服务端性能测试实战3-性能测试脚本开发

前言在前面的两篇文章中,我们分别介绍了性能测试的理论知识以及性能测试计划制定,本篇文章将重点介绍性能测试脚本开发。脚本开发将分为两个阶段:阶段一:了解各个接口的入参、出参,使用Python代码模拟前端...

Springboot整合Apache Ftpserver拓展功能及业务讲解(三)

今日分享每天分享技术实战干货,技术在于积累和收藏,希望可以帮助到您,同时也希望获得您的支持和关注。架构开源地址:https://gitee.com/msxyspringboot整合Ftpserver参...

Linux和Windows下:Python Crypto模块安装方式区别

一、Linux环境下:fromCrypto.SignatureimportPKCS1_v1_5如果导包报错:ImportError:Nomodulenamed'Crypt...

Python 3 加密简介(python des加密解密)

Python3的标准库中是没多少用来解决加密的,不过却有用于处理哈希的库。在这里我们会对其进行一个简单的介绍,但重点会放在两个第三方的软件包:PyCrypto和cryptography上,我...

怎样从零开始编译一个魔兽世界开源服务端Windows

第二章:编译和安装我是艾西,上期我们讲述到编译一个魔兽世界开源服务端环境准备,那么今天跟大家聊聊怎么编译和安装我们直接进入正题(上一章没有看到的小伙伴可以点我主页查看)编译服务端:在D盘新建一个文件夹...

附1-Conda部署安装及基本使用(conda安装教程)

Windows环境安装安装介质下载下载地址:https://www.anaconda.com/products/individual安装Anaconda安装时,选择自定义安装,选择自定义安装路径:配置...

如何配置全世界最小的 MySQL 服务器

配置全世界最小的MySQL服务器——如何在一块IntelEdison为控制板上安装一个MySQL服务器。介绍在我最近的一篇博文中,物联网,消息以及MySQL,我展示了如果Partic...

如何使用Github Action来自动化编译PolarDB-PG数据库

随着PolarDB在国产数据库领域荣膺桂冠并持续获得广泛认可,越来越多的学生和技术爱好者开始关注并涉足这款由阿里巴巴集团倾力打造且性能卓越的关系型云原生数据库。有很多同学想要上手尝试,却卡在了编译数据...

面向NDK开发者的Android 7.0变更(ndk android.mk)

订阅Google官方微信公众号:谷歌开发者。与谷歌一起创造未来!受Android平台其他改进的影响,为了方便加载本机代码,AndroidM和N中的动态链接器对编写整洁且跨平台兼容的本机...

信创改造--人大金仓(Kingbase)数据库安装、备份恢复的问题纪要

问题一:在安装KingbaseES时,安装用户对于安装路径需有“读”、“写”、“执行”的权限。在Linux系统中,需要以非root用户执行安装程序,且该用户要有标准的home目录,您可...

OpenSSH 安全漏洞,修补操作一手掌握

1.漏洞概述近日,国家信息安全漏洞库(CNNVD)收到关于OpenSSH安全漏洞(CNNVD-202407-017、CVE-2024-6387)情况的报送。攻击者可以利用该漏洞在无需认证的情况下,通...

Linux:lsof命令详解(linux lsof命令详解)

介绍欢迎来到这篇博客。在这篇博客中,我们将学习Unix/Linux系统上的lsof命令行工具。命令行工具是您使用CLI(命令行界面)而不是GUI(图形用户界面)运行的程序或工具。lsoflsof代表&...

幻隐说固态第一期:固态硬盘接口类别

前排声明所有信息来源于网络收集,如有错误请评论区指出更正。废话不多说,目前固态硬盘接口按速度由慢到快分有这几类:SATA、mSATA、SATAExpress、PCI-E、m.2、u.2。下面我们来...

新品轰炸 影驰SSD多款产品登Computex

分享泡泡网SSD固态硬盘频道6月6日台北电脑展作为全球第二、亚洲最大的3C/IT产业链专业展,吸引了众多IT厂商和全球各地媒体的热烈关注,全球存储新势力—影驰,也积极参与其中,为广大玩家朋友带来了...