首页 > 编程语言 >使用opencv进行RGB--YUV转换 c++版本

使用opencv进行RGB--YUV转换 c++版本

时间:2022-11-09 23:23:56浏览次数:59  
标签:src Mat -- yuv420sp c++ YUV nv12 yuv cv

//
// Created by DangXS on 2022/4/27.
//

#ifndef CPLUSPLUS_PROJECT1_YUV_HELPER_H
#define CPLUSPLUS_PROJECT1_YUV_HELPER_H

#include "opencv2/opencv.hpp"

static void YUVI420_to_YUV_NV12(cv::Mat &src, uchar *dst, int width, int height) {
    /*
    * src: YYYYYYYY UU VV
    * dst: YYYYYYYY UVUV
    * */
    int _size = width * height;
    uchar *src_y = src.data;
    uchar *src_u = src_y + _size;
    uchar *src_v = src_u + _size / 4;

    memcpy(dst, src_y, _size);
    for (int i = 0; i < _size / 2; i += 2) {
        dst[_size + i] = src_u[i / 2];
        dst[_size + i + 1] = src_v[i / 2];
    }
}


static void BGR2YUV420P(const cv::Mat& img, uint8_t *yuv_data, int w, int h) {
    assert(w % 2 == 0 && h % 2 == 0);
    cv::Mat yuv;
    cvtColor(img, yuv, cv::COLOR_BGR2YUV_I420); //CV_BGR2YUV_I420

    cv::Mat Y = yuv(cv::Rect(0, 0, w, h));
    cv::Mat U = yuv(cv::Rect(0, h, w / 2, h / 2));
    cv::Mat V = yuv(cv::Rect(w / 2, h, w / 2, h / 2));

    memcpy_s(yuv_data, w * h, Y.data, h * w);
    for (size_t i = 0; i < h / 2; i++)
    {
        for (size_t j = 0; j < w; j++)
        {
            int index = i * w + j;
            if (j < 2 / w) yuv_data[w*h + index] = U.data[index];
            else yuv_data[w*h + index] = V.data[index - w / 2];
        }
    }
    yuv.release();

}

int TEST_YUVI420_to_YUV_NV12() {
    printf("hello wrold");

    std::string _path = "../1.jpg";
    cv::Mat img = cv::imread(_path);
    int width = img.cols;
    int height = img.rows;
    cv::Mat _yuv420;
    cv::cvtColor(img, _yuv420, CV_BGR2YUV_I420);

    cv::imwrite("../1_yuv420.jpg", _yuv420);

    auto *ptr__yuv420sp_nv12 = new uchar[width * height * 3 / 2];
    YUVI420_to_YUV_NV12(_yuv420, ptr__yuv420sp_nv12, width, height);
    cv::Mat _yuv420sp_nv12 = _yuv420.clone();
    _yuv420sp_nv12.setTo(cv::Scalar(0));
    memcpy(_yuv420sp_nv12.data, ptr__yuv420sp_nv12, width * height * 3 / 2);
    printf("w:%d, h:%d ", _yuv420sp_nv12.cols, _yuv420sp_nv12.rows);
    cv::imwrite("../1_yuv_nv12.jpg", _yuv420sp_nv12);

    cv::Mat _gray;
    cv::cvtColor(_yuv420sp_nv12, _gray, CV_YUV2GRAY_NV12);
    cv::imwrite("../gray.jpg", _gray);

    cv::Mat _img2;
    cv::cvtColor(_yuv420sp_nv12, _img2, CV_YUV2BGR_NV12);
    cv::imwrite("../img2.jpg", _img2);

    delete[] ptr__yuv420sp_nv12;
    return 0;
}

#endif //CPLUSPLUS_PROJECT1_YUV_HELPER_H

 

标签:src,Mat,--,yuv420sp,c++,YUV,nv12,yuv,cv
From: https://www.cnblogs.com/dxscode/p/16196396.html

相关文章

  • CSharp: Singleton Pattern in donet 6
     ///<summary>///单例模式单件模式、SingletonPattern///</summary>publicabstractclassBaseGreeter{publicvirtualvoid......
  • Kafka问题收集
    Kafka问题收集kafka分区分配策略干趴面试官系列|请你简述一下Kafka中的分区分配生产者发送消息分区分配策略kafka里的partitioner(分区器)来负责客户端生产层面的负......
  • mac 查看已经安装jdk路径,以及配置jdk环境变量
    JAVA_HOME干嘛的?JAVA_HOME是个变量名这样一来,配置JAVA_HOME的作用就很清楚了吧。JAVA_HOME就是索引java文件地址的。某些应用软件需要用到java,然后就默认规约(算是默......
  • c++ 函数-遍历文件夹
    #pragmaonce#include<iostream>#include<string.h>#include<unistd.h>#include<sys/io.h>#include<sys/types.h>#include<sys/stat.h>#include"dirent.h"......
  • Git - 错误集
    $gitpull20221109testmasterFromhttps://gitee.com/chaung_sun/20221109test*branchmaster->FETCH_HEADfatal:refusingtomergeunrelated......
  • 部署owncloud连接ladp迁移数据
    定期清理日志echo''>/var/www/html/data/owncloud.log查询用户的ldap语句(|(objectclass=inetOrgPerson)(objectclass=posixAccount)(objectclass=top))开......
  • [Bug0049]SwitchHosts报错:没有写入 Hosts 文件的权限
    问题SwitchHosts报错:没有写入Hosts文件的权限解决方案1、打开如下目录C:\Windows\System32\drivers\etc2、右键hosts文件->点击安全->点击编辑->找到User......
  • Morris遍历 介绍+前中后序遍历
    前言Morris遍历是通过对原二叉树增加虚拟连接(后面会复原)来节约递归或队列的额外空间消耗,通过常数空间即可实现对二叉树的遍历。本文主要是通过MorrisInorderTraversal......
  • Atcoder Grand Contest 004(A~F)
    这场半VP做的,就不分赛时赛后写了,直接放每道题的解法。A-DivideaCuboid当某一维的长度为偶数的时候,显然可以在这一维的中间切,两部分方块的最小差为\(0\)。当每一......
  • 爱心代码Html
    1<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">2<HTML>3<HEAD>4<TITLE>Love</TITLE>5<METANAME="Generator"CONTENT="Ed......