一 转换方法

黑色是R,G,B都是0,因此Y通道是0,UV通道都是128

二 方法

// Correction of abscissa according to width
void fix_x(int &x, int width) {if (x < 0) {x = 0;}else if (x >= width) {x = width - 1;}
}
void fix_y(int &y, int height) {if (y < 0) {y = 0;}else if (y >= height) {y = height - 1;}
}
// Correct rectangular area according to image width and height
bool fix(int &left, int &top, int &right, int &bottom, int width, int height) {fix_x(left, width);fix_x(right, width);fix_y(top, height);fix_y(bottom, height);int w = right - left;int h = bottom - top;if (w <= 0 || h <= 0) {return false;}return true;
}
// blacken the designated area of nv12 image
// left:Abscissa of upper left corner of rectangular area
// top:Vertical coordinate of upper left corner of rectangular area
// right:Abscissa of lower right corner of rectangular area
// bottom:Vertical coordinate of the lower right corner of the rectangular area
// width:Original image width
// height:Original image height
// ppu8Plane_y:Y channel address
// ppu8Plane_uv:UV channel address
void blackening_nv12(int left, int top,int right, int bottom,int width,int height,unsigned char *ppu8Plane_y,unsigned char *ppu8Plane_uv) {if (!ppu8Plane_y || !ppu8Plane_uv) {return;}if (!fix(left, top, right, bottom, width, height)) {return;}int w = right - left;for (int j = top;j <= bottom;j++) {memset(ppu8Plane_y + left + j * width, 0, w);memset(ppu8Plane_uv + left + j / 2 * width, 128, w);}
}

测试:

#include <iostream>
#include <string>
#include <fstream>
enum FILE_WRITE_MODE {OVER_WRITE,APPEND_WRITE
};
inline bool get_file_content(const char *path, std::string &str) {std::ifstream ifs(path, std::ios::in);if (!ifs || !ifs.good()) {return false;}str.assign((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());ifs.close();return !str.empty();
}
bool write_file_content(const char *path, const char *buf, size_t size, unsigned mode) {if (!path || !buf) {return false;}std::ofstream  ofs;switch (mode){case OVER_WRITE:ofs.open(path, std::ios::in | std::ios::trunc);if (!ofs.is_open()) {return false;}break;case APPEND_WRITE:ofs.open(path, std::ios::in | std::ios::app);if (!ofs.is_open()) {return false;}break;default:return false;break;}ofs.write(buf, size);ofs.close();return true;
}
int main() {std::string str;if (get_file_content("./1653210331_1920x1080_56029_33_1920x1080.NV12", str)) {size_t size = str.size();std::cout << "load success! size:" << size << std::endl;unsigned char *ppu8Plane0 = (unsigned char *)malloc(1920 * 1080 * 3 / 2);unsigned char *ppu8Plane1 = ppu8Plane0 + 1920 * 1080;memcpy(ppu8Plane0, str.c_str(), size);blackening_nv12(0, 0, 1920 / 2 - 1, 1080 - 1, 1920, 1080, ppu8Plane0, ppu8Plane1);size = 1920 * 1080 * 3 / 2;if (write_file_content("./11_1920x1080.NV12", (char *)ppu8Plane0, size, OVER_WRITE)) {std::cout << "rewrite success! size:" << size << std::endl;}}return 0;
}

将NV21图像某一区域变黑色相关推荐

  1. 【Android Camera2】玩转图像数据 -- NV21图像旋转,镜像,转rgba代码分析,性能优化

    [Android Camera2]玩转图像数据 业务场景介绍 NV21数据旋转 逐像素遍历法 NV21数据镜像 逐像素遍历法 中心翻转法 NV21转RGB/RGBA数据 逐像素遍历法 NV21组合操作 ...

  2. OpenCV实战(3)——图像感兴趣区域

    OpenCV实战(3)--图像感兴趣区域 0. 前言 1. 感兴趣区域 1.1 ROI 实例 1.2 定义 ROI 2. 使用图像掩码 3. 完整代码示例 小结 系列链接 0. 前言 在实际应用场景下 ...

  3. 提取图像感兴趣区域_从图像中提取感兴趣区域

    提取图像感兴趣区域 Welcome to the second post in this series where we talk about extracting regions of intere ...

  4. Halcon学习之六:获取Image图像中Region区域的特征参数

    area_center_gray ( Regions, Image : : : Area, Row, Column )    计算Image图像中Region区域的面积Area和重心(Row,Colu ...

  5. R语言ggplot2可视化左对齐两个可视化图像的画图区域(边缘)实战

    R语言ggplot2可视化左对齐两个可视化图像的画图区域(边缘)实战 目录 R语言ggplot2可视化左对齐两个可视化图像的画图区域(边缘)实战

  6. 图像低频高频区域分离 小波变换

    图像低频高频区域分离 简介 本篇整理记录利用小波分离图像的高频.低频部分信息. 具体实现 实现代码参考资料:小波变换 C++ opencv 实现. 小波变换 小波生成和参考资料中一致.小波变换中,首先 ...

  7. 【Android RTMP】NV21 图像旋转处理 ( 快速搭建 RTMP 服务器 Shell 脚本 | 创建 RTMP 服务器镜像 | 浏览器观看直播 | 前置 / 后置摄像头图像旋转效果展示 )

    文章目录 安卓直播推流专栏博客总结 一. 编写快速搭建 RTMP 服务器 Shell 脚本 二. RTMP 快速搭建方法 三.创建阿里云 RTMP 服务器镜像 四.浏览器查看直播内容 五.前置 / 后 ...

  8. 【Android RTMP】NV21 图像旋转处理 ( 图像旋转算法 | 后置摄像头顺时针旋转 90 度 | 前置摄像头顺时针旋转 90 度 )

    文章目录 安卓直播推流专栏博客总结 一. 后置摄像头顺时针旋转 90 度 二. 前置摄像头顺时针旋转 90 度 三. NV21 格式图像旋转代码 安卓直播推流专栏博客总结 Android RTMP 直 ...

  9. 【Android RTMP】NV21 图像旋转处理 ( 问题描述 | 图像顺时针旋转 90 度方案 | YUV 图像旋转细节 | 手机屏幕旋转方向 )

    文章目录 安卓直播推流专栏博客总结 一. NV21 图像格式与 Camera图像传感器方向问题 二. NV21 图像格式视频旋转 1. 图像旋转问题及解决方案 ( 顺时针旋转 90 度 ) 2. NV ...

最新文章

  1. YOLOv3 Darknet安装编译与训练自己的数据集
  2. window.atob()与window.btoa()方法实现编码与解码
  3. 串口输出5v电压_为什么RS485比串口速度快距离远?--谈单端信号与差分信号之差异...
  4. 注释,今晚我不关心代码,我只想你
  5. C#LeetCode刷题之#55-跳跃游戏(Jump Game)
  6. TensorFlow2.0(七)--基础API使用
  7. ORACLE——ROWNUM解析(使用ROWNUM大于条件,无法得到任何查询结果)
  8. 电流电压曲线 vc源码_电瓶修复—充电曲线你知道多少?
  9. 第三章 平稳时间序列模型
  10. termux配置python安装kali_利用termux安装kali
  11. javaWeb通过servlet实现注册登陆
  12. 删除文件时提示需要administrator 权限
  13. AUTOSAR架构中的配置文件
  14. 计算机硬盘计入哪个会计科目,电脑加装固态硬盘如何做分录
  15. 英文网页批量翻译导出本地教程
  16. 微信template模板
  17. oracle 12 pan.baidu.com,Oracle 数据库和补丁下载地址和百度云盘地址 12.1.0.2 11.2.0.4 11.2.0.1...
  18. TiDB 实战优化之 SQL 常见问题与优化案例
  19. 不是你需要中台,而是一名合格的架构师(附各中台建设PPT)
  20. Hibernate5的学习笔记(二)

热门文章

  1. OpenCVSharp 相机棋盘格校正
  2. ajax传递多参数类型,Ajax传递不同类型的参数
  3. 给自己的android扫盲文 - 1
  4. [小样本医学图像]Generalized Organ Segmentation by Imitating One-shot Reasoning using Anatomical Correlation
  5. PTA 单链表分段逆转 (12 分)
  6. 数学中奇妙的“金蝉脱壳”(转)
  7. 两分钟内教会你如何给视频加配音,快速掌握配音技巧!
  8. 企业微信如何通过红包活动引流?
  9. 【OpenCV】人脸旋转角度计算
  10. 根据E-R图设计数据库表