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

推荐订阅源

L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
B
Blog
WordPress大学
WordPress大学
Project Zero
Project Zero
P
Palo Alto Networks Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
T
Tailwind CSS Blog
Forbes - Security
Forbes - Security
F
Full Disclosure
SecWiki News
SecWiki News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News: Ask HN
Hacker News: Ask HN
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
Cisco Talos Blog
Cisco Talos Blog
G
Google Developers Blog
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
O
OpenAI News
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Cybersecurity and Infrastructure Security Agency CISA
S
Securelist
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
IT之家
IT之家
U
Unit 42
腾讯CDC
S
Security Affairs
C
Cisco Blogs
Schneier on Security
Schneier on Security
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss

OpenCV

目标检测,计算出旋转速度和加速度,有人精通吗?急 - V2EX opencv 图像动态融合问题 - V2EX 请问一个关于 OpenCV 手眼标定(cv::calibrateHandEye)获取相机安装参数的问题 - V2EX [求助]C++ 用到 OpenCV 库 怎么静态编译呢? - V2EX opencv 去除视频中移动的文字 - V2EX Opencv 遇到一个很奇怪的问题, mac 上获取摄像头图片,必须需要 sleep 一下, 0.1 都可以,但是不给就是黑图,代码如下,有没有大佬指点一下 - V2EX 请教一个 opencv 算法,图片中检测圆形对象 - V2EX 用 C++调用 OpenCV 的 matchTemplate 反而比 Python 更慢 - V2EX mov 视频的图片如何转换? - V2EX 大家有用过类似 PS Content-Aware Fill 抠图的库或源码吗(不限语言) - V2EX [求助] 如何确定点在曲线中的位置 - V2EX 哇 期待很久的 计算机视觉 - V2EX
nii 文件用 opencv 转换为 mp4 视频画面失真,求教 - V2EX
sugarkeek · 2023-02-12 · via OpenCV

nii 是一个 4d 的医学 ct 图像,尺寸为(高,宽,深度,时间)。

我的预期是, 取时间和另外两个维度,转换为一个 mp4 格式的视频。

我的思路,先将 nii 图片转换一个 2 维的数组,然后顺序拼成一个视频。

但是实际, 转换出来的视频画面失真,win11 系统播放器打开画面失真,qq 影音播放器打开提示视频失效。

源文件和转换后的文件预览地址

https://1drv.ms/u/s!Ah4q2HtKB2AWg4NnYXaMslribNVlWw?e=D8ScIs

代码


import os
import cv2
import numpy as np


def nii2imgs_dim(nii, dim) -> list:
    """
    将 nii 文件转换为多张图片,返回图片数组,不用中间保存,方便转换为视频
    :param nii: nii 对象
    :param dim: 要转换的维度
    :return: 图片数组
    """
    imgs = []
    data = nii
    for d3i in range(data.shape[3]):
        # 读取 3d 文件
        d3img = data[:, :, :, d3i]
        # 获取 nii 文件的维度
        d3img_shape = d3img.shape
        # 遍历每一张图片
        for d2j in range(d3img_shape[2]):
            # 获取第 i 张图片
            img = data[:, :, d2j, d3i]
            imgs.append(img)

    return imgs

def imgs2mp4(img_list, save_path):
    """
    将图片数组转换为 mp4
    :param img_list: 图片数组
    :param save_path: 保存地址
    :return: 空
    """
    # 判断 save_path 路径是否存在,如果不存在则创建
    if not os.path.exists(save_path):
        os.makedirs(save_path)

    # 图片数组转换为 mp4
    img, *imgs = img_list # *imgs 表示剩下的所有元素,img 表示第一个元素,目的是为了获取图片尺寸
    print(img.shape)
    # img 转换为 float32
    img = img.astype(np.float32)
    img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    height, width, layers = img.shape
    video = cv2.VideoWriter(save_path + "video.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 1, (width, height))
    video.write(img)
    for img in imgs:
        img = img.astype(np.float32)

        img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

        video.write(img)
    video.release()





img = nib.load(file_path)
data = img.get_fdata()
# nii 转换为多张图片
imgs = nii2imgs_dim(data, 3)
# 将图片数组转换为 mp4
imgs2mp4(imgs, "./tmp/")