码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • python转yuyv422到jpg


    1. import cv2 as cv
    2. import yuvio
    3. import os
    4. import cv2 as cv
    5. import numpy as np
    6. #参考:https://pypi.org/project/yuvio/
    7. yuv_frame = yuvio.imread("my.yuv", 720, 640, 'yuyv422')
    8. y = yuv_frame.y
    9. u = yuv_frame.u
    10. v = yuv_frame.v
    11. cv.imshow('y',y)
    12. reshaped_y = y.reshape(720,640)
    13. reshaped_u = u.reshape(720,320)
    14. reshaped_v = v.reshape(720,320)
    15. img = "my.jpg"
    16. outImg = "out.jpg"
    17. cv.imwrite(img, y)
    18. im = cv.imread(img)
    19. ##截取图像高从25到479,宽从0到720
    20. im = im[25:479,0:720]
    21. cv.imwrite(outImg, im)
    22. cv.waitkey(0)

    参考:

    YUV Colorspace

    https://github.com/labradon/yuvio/blob/main/README.md

    另外下面代码yuv420p的转换,来自于网上。

    1. import os
    2. import cv2 as cv
    3. import numpy as np
    4. # 读取yuv420p的一帧文件,并转化为png图片
    5. if __name__ == '__main__':
    6. filepath = 'test.yuv'
    7. binfile = open(filepath, 'rb')
    8. size = os.path.getsize(filepath)
    9. image_width = 720
    10. image_hight = 640
    11. image_y = [[0] * image_width for i in range(image_hight)]
    12. image_u = [[0] * image_width for i in range(image_hight)]
    13. image_v = [[0] * image_width for i in range(image_hight)]
    14. for r in range(image_hight):
    15. for c in range(image_width):
    16. image_y[r][c] = binfile.read(1)[0]
    17. Image_Y = np.array(image_y)
    18. for r in range(int(image_hight / 2)):
    19. for c in range(int(image_width / 2)):
    20. pixel = binfile.read(1)[0]
    21. image_u[2 * r + 0][2 * c + 0] = pixel
    22. image_u[2 * r + 1][2 * c + 0] = pixel
    23. image_u[2 * r + 0][2 * c + 1] = pixel
    24. image_u[2 * r + 1][2 * c + 1] = pixel
    25. Image_U = np.array(image_u)
    26. for r in range(int(image_hight / 2)):
    27. for c in range(int(image_width / 2)):
    28. pixel = binfile.read(1)[0]
    29. image_v[2 * r + 0][2 * c + 0] = pixel
    30. image_v[2 * r + 0][2 * c + 1] = pixel
    31. image_v[2 * r + 1][2 * c + 0] = pixel
    32. image_v[2 * r + 1][2 * c + 1] = pixel
    33. Image_V = np.array(image_v)
    34. binfile.close()
    35. compose = np.array([Image_Y, Image_V, Image_U]).transpose([1, 2, 0]).astype(np.uint8)
    36. #compose = np.array([Image_Y, Image_U, Image_V]).transpose([1, 2, 0]).astype(np.uint8)
    37. Image = cv.cvtColor(compose, cv.COLOR_YUV2RGB)
    38. #Image = cv.cvtColor(compose, cv.COLOR_YUV2BGR)
    39. cv.imwrite("one_frame_of_highway.yuv.png", Image)

  • 相关阅读:
    2亿用户,B站API网关如何架构?
    【论文翻译】分布式数据库系统中的并发控制
    解决方案|法大大电子签:3招击破汽车销售效率及成本难题!
    计算机毕业设计Java易医就医购药交互平台(源码+系统+mysql数据库+Lw文档)
    树莓派——4、交叉编译
    【菜鸟教程】 C++学习笔记
    视觉特效,图片转成漫画功能
    Hive执行计划之什么是hiveSQL向量化模式及优化详解
    讲一讲VS Code配置GoLang语言开发环境
    【Python基础】4. 基本语句
  • 原文地址:https://blog.csdn.net/tengfeidx/article/details/134041883
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号