Raw image python medical image format conversion

Many medical images are saved in raw format, which is also the original format of photos taken by SLR cameras.

RAW image: The CMOS or CCD image sensor converts the captured light source signal into the original data of the digital signal.

Recently, when using pytorch to process ophthalmic medical images, it is necessary to convert raw to jpg. Because raw images generally cannot be processed directly, Python is used for format conversion. After searching, it is very complicated. What to get the raw data format, the length and width of the picture, etc., the result is to try it yourself, and the result can be obtained directly by opencv, so record it, and there is no in-depth research. I look forward to continuing to study the raw image in depth in the future. processing.

core code

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

Complete code: 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 !!!")


Guess you like

Origin blog.csdn.net/weixin_42748604/article/details/119539168