raw图片python医学影像的格式转换

很多医学图像是raw格式保存的,raw同时也是单反相机的拍摄照片的原格式。

RAW图像:CMOS或者CCD图像感应器将捕捉到的光源信号转化为数字信号的原始数据。

最近使用pytorch处理眼科医学影像时,需要把raw转换为jpg。因为raw图像一般不能直接处理,所以使用Python进行格式转换。经过搜索都说的很复杂,什么要获得raw的数据格式,图片的长宽之类的,结果自己尝试,直接opencv就可以结果,所以记录一下,也没有深入研究,期待以后继续深度研究raw图像的处理。

核心代码

img = cv2.imread(filePath)
cv2.imwrite(filename, img)

完整代码:jpg.py

import numpy as np
import os
import cv2
import shutil
from PIL import Image

def searchDirFile(rootDir,saveDir):
    for dir_or_file in os.listdir(rootDir):
        try:
            filePath = os.path.join(rootDir, dir_or_file)
            # 判断是否为文件
            if os.path.isfile(filePath):
                # 如果是文件再判断是否以.jpg结尾,不是则跳过本次循环
                if os.path.basename(filePath).endswith('.raw'):
                    print('imgBox fileName is: '+os.path.basename(filePath))
                    # 拷贝jpg文件到自己想要保存的目录下
                    # shutil.copyfile(filePath,os.path.join(saveDir,os.path.basename(filePath)))

                    img = cv2.imread(filePath)

                    path2 = filePath.split('/')[2]
                    path = f'{saveDir}/{path2}'
                    # print(path)
                    if not os.path.exists(path):
                        os.makedirs(path)

                    filename = os.path.splitext(os.path.basename(filePath))[0] + ".jpg"
                    filename = os.path.join(path, filename)
                    print('filename is: '+filename)
                    cv2.imwrite(filename, img)

                else:
                    continue
            # 如果是个dir,则再次调用此函数,传入当前目录,递归处理。
            elif os.path.isdir(filePath):
                searchDirFile(filePath, saveDir)
            else: print('not file and dir '+os.path.basename(filePath))
        except:
            continue



if __name__ == '__main__':
    rootDir = './Images'
    saveDir = './jpg'
    searchDirFile(rootDir, saveDir)
    print("the end !!!")


猜你喜欢

转载自blog.csdn.net/weixin_42748604/article/details/119539168