import os
from collections import defaultdict, OrderedDict
import json
import numpy as np
import cv2
import glob
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return super(NpEncoder, self).default(obj)
results = defaultdict(list)
mask = defaultdict(list)
volume= defaultdict(list)
stress= defaultdict(list)
parm= defaultdict(list)
line=defaultdict(list)
param=defaultdict(list)
results["mask"] =[]
results["volume"] =volume
results["stress"] =stress
results["parm"] =param
# mask["series_number"]=1
# mask["slice_index"]=1
# mask["rois"]=rois
# rois["RV"]= contours1
# rois["LV-Inner"]=contours2[0]
# rois["LV-Ext"]=contours2[1]
# rois["PPM"]=contours4
volume["line"]=[[10, 100], [20, 3000]]
volume["ED"]=10
volume["ES"]=100
stress["GLS"]=-19.5
stress["GCS"]=-17.7
stress["GRS"]=39.7
param["EDV"]=201
param["ESV"]=105
param["LVM"]=189
test_dict = {
"patientID": "0000086930",
"studyUID": "1.2.842065.95027634.776400958",
"seriesUID": "1.3.12.2.1107.5.2.43.166151.2022011508231736189313766.0.0.0",
"task": "CardiacMRI",
'results': results,
}
# files = glob.glob('./data/test/outputs/1464455/*')
files = os.listdir('./data/test/pre/1464455/')
print('files1',files)
files.sort()
print('files2',files)
# for imgp in os.listdir('./data/test/pre/1464455'):
for imgp in files:
# imbgr = cv2.imread('./data/test/pre/1464457/224.png')
id=str(imgp).replace('.png','')
print('id', id)
imgp=os.path.join('./data/test/pre/1464455', imgp)
print('imgp',imgp)
imbgr = cv2.imread(imgp)
imgray_path=imgp.replace('pre','outputs')
imgray = cv2.imread(imgray_path, 0)
# 以灰度图读取mask图片,那么不同物体对应的像素值是不同的,提取统一像素值的所有位置
img = np.array(imgray)
# mask中物体1 RV 的像素值为38,设定像素范围来获取所有坐标
upper1 = img <= 40
lower1 = img >= 30
thresh1 = (np.multiply(upper1, lower1).astype(np.float32) * 255).astype(np.uint8)
# mask中物体2 Myo 的像素值为75,设定像素范围来获取所有坐标
upper2 = imgray <= 80
lower2 = imgray >= 60
thresh2 = (np.multiply(upper2, lower2).astype(np.float32) * 255).astype(np.uint8)
# mask中物体3 LV的像素值为113,设定像素范围来获取所有坐标
upper3 = imgray <= 120
lower3 = imgray >= 110
thresh3 = (np.multiply(upper3, lower3).astype(np.float32) * 255).astype(np.uint8)
# mask中物体4 PPm的像素值为15,设定像素范围来获取所有坐标
upper4 = imgray <= 20
lower4 = imgray >= 5
thresh4 = (np.multiply(upper4, lower4).astype(np.float32) * 255).astype(np.uint8)
contours1, hierarchy1 = cv2.findContours(thresh1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours2, hierarchy2 = cv2.findContours(thresh2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours3, hierarchy3 = cv2.findContours(thresh3, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours4, hierarchy4 = cv2.findContours(thresh4, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
lst1 = []
for i, cous in enumerate(contours1):
a = cv2.contourArea(cous)
print('contours1:', i, a)
if a >= 100:
lst1.append(cous)
contours1 = lst1
lst2 = []
for i, cous in enumerate(contours2):
a = cv2.contourArea(cous)
print('contours2:', i, a)
if a >= 30:
lst2.append(cous)
contours2 = lst2
lst4 = []
for i, cous in enumerate(contours4):
a = cv2.contourArea(cous)
print('contours4:', i, a)
if a >= 5:
lst4.append(cous)
contours4 = lst4
label = defaultdict(list)
rois = defaultdict(list)
RV = defaultdict(list)
LVI = defaultdict(list)
LVE = defaultdict(list)
PPM = defaultdict(list)
label["series_number"]=1
label["slice_index"] = int(id)
print('label-id',id)
label["rois"]=rois
if contours1:
rois["RV"]=contours1
else : rois["RV"]=[]
if len(contours2) == 1:
rois["LV-Inner"] = contours2[0]
rois["LV-Ext"] = []
elif len(contours2)>=2:
rois["LV-Inner"]=contours2[0]
rois["LV-Ext"] = contours2[1]
else:
rois["LV-Inner"]=[]
rois["LV-Ext"]=[]
rois["LV-Ext"]=[]
if contours4:
rois["PPM"]=contours4
else:rois["PPM"]=[]
results["mask"].append(label)
json_str = json.dumps(test_dict, cls=NpEncoder)
with open('patient-1464455.json', 'w') as json_file:
json_file.write(json_str)
|