import pydicom.uid import sys import pydicom from pylab import * import cv2 import json def contours_in(contours): p = np.zeros(shape=(2000,2000)) cv2.drawContours(p, contours, -1, 255, -1) a = np.where(p==255)[0].reshape(-1,1) b = np.where(p==255)[1].reshape(-1,1) coordinate = np.concatenate([a,b], axis=1).tolist() inside = [tuple(x) for x in coordinate] return inside if __name__ == '__main__': part = 'belly' # image_path = '/Users/haoyanbin/Desktop/0e9df40b-2dd1022a-c9fe09f4-c337c767-34502cca_cropped_image.png' image_path = '/Users/haoyanbin/Desktop/db0d2877-16a5787c-4c8d67c4-071097fc-deee3b7e.png' # image = Image.open(image_path) # pixel_array = np.array(image) # dcm_path = '/Users/haoyanbin/Desktop/a4f9c3a5-4ba2-4adb-b408-3d3a0df1d912.dcm' dcm_path = '/Users/haoyanbin/Desktop/response3.dcm' dicom_image = pydicom.dcmread(dcm_path) dic_img = dicom_image.pixel_array img = cv2.bitwise_not(dic_img) # img = cv2.cvtColor(pixel_array) # img = cv2.imread(image_path) height, width = img.shape[:2] image = np.zeros((height, width, 1), np.uint8) # image2 = np.zeros((height, width, 3), np.uint8) # data_json = r'{"visible":true,"active":false,"invalidated":false,"handles":{"points":[{"x":1075.0994182303004,"y":1325.3899033176901,"highlight":true,"active":true,"lines":[{"x":1206.9569189047063,"y":1259.4611529804872}]},{"x":1206.9569189047063,"y":1259.4611529804872,"highlight":true,"active":true,"lines":[{"x":1206.9569189047063,"y":1256.7141217164371}]},{"x":1206.9569189047063,"y":1256.7141217164371,"highlight":true,"active":true,"lines":[{"x":1209.7039501687564,"y":1256.7141217164371}]},{"x":1209.7039501687564,"y":1256.7141217164371,"highlight":true,"active":true,"lines":[{"x":1308.5970756745608,"y":1179.7972463230337}]},{"x":1308.5970756745608,"y":1179.7972463230337,"highlight":true,"active":true,"lines":[{"x":1347.0555133712626,"y":1163.315058738733}]},{"x":1347.0555133712626,"y":1163.315058738733,"highlight":true,"active":true,"lines":[{"x":1341.5614508431622,"y":1017.7224017440765}]},{"x":1341.5614508431622,"y":1017.7224017440765,"highlight":true,"active":true,"lines":[{"x":1336.067388315062,"y":844.6594321089187}]},{"x":1336.067388315062,"y":844.6594321089187,"highlight":true,"active":true,"lines":[{"x":1336.067388315062,"y":732.0311502828636}]},{"x":1336.067388315062,"y":732.0311502828636,"highlight":true,"active":true,"lines":[{"x":1267.3916067138089,"y":721.0430252266631}]},{"x":1267.3916067138089,"y":721.0430252266631,"highlight":true,"active":true,"lines":[{"x":1264.6445754497588,"y":721.0430252266631}]},{"x":1264.6445754497588,"y":721.0430252266631,"highlight":true,"active":true,"lines":[{"x":1264.6445754497588,"y":723.7900564907131}]},{"x":1264.6445754497588,"y":723.7900564907131,"highlight":true,"active":true,"lines":[{"x":1209.7039501687564,"y":803.4539631481668}]},{"x":1209.7039501687564,"y":803.4539631481668,"highlight":true,"active":true,"lines":[{"x":1209.7039501687564,"y":806.2009944122169}]},{"x":1209.7039501687564,"y":806.2009944122169,"highlight":true,"active":true,"lines":[{"x":1206.9569189047063,"y":808.948025676267}]},{"x":1206.9569189047063,"y":808.948025676267,"highlight":true,"active":true,"lines":[{"x":1176.739575000155,"y":902.3470886539712}]},{"x":1176.739575000155,"y":902.3470886539712,"highlight":true,"active":true,"lines":[{"x":1176.739575000155,"y":905.0941199180213}]},{"x":1176.739575000155,"y":905.0941199180213,"highlight":true,"active":true,"lines":[{"x":1176.739575000155,"y":907.8411511820714}]},{"x":1176.739575000155,"y":907.8411511820714,"highlight":true,"active":true,"lines":[{"x":1176.739575000155,"y":910.5881824461217}]},{"x":1176.739575000155,"y":910.5881824461217,"highlight":true,"active":true,"lines":[{"x":1165.7514499439544,"y":1006.734276687876}]},{"x":1165.7514499439544,"y":1006.734276687876,"highlight":true,"active":true,"lines":[{"x":1132.787074775353,"y":1124.856621042031}]},{"x":1132.787074775353,"y":1124.856621042031,"highlight":true,"active":true,"lines":[{"x":1105.3167621348516,"y":1218.2556840197353}]},{"x":1105.3167621348516,"y":1218.2556840197353,"highlight":true,"active":true,"lines":[{"x":1105.3167621348516,"y":1221.0027152837854}]},{"x":1105.3167621348516,"y":1221.0027152837854,"highlight":true,"active":true,"lines":[{"x":1102.5697308708015,"y":1221.0027152837854}]},{"x":1102.5697308708015,"y":1221.0027152837854,"highlight":true,"active":true,"lines":[{"x":1102.5697308708015,"y":1223.7497465478357}]},{"x":1102.5697308708015,"y":1223.7497465478357,"highlight":true,"active":true,"lines":[{"x":1102.5697308708015,"y":1226.4967778118857}]},{"x":1102.5697308708015,"y":1226.4967778118857,"highlight":true,"active":true,"lines":[{"x":1077.8464494943505,"y":1297.9195906771888}]},{"x":1077.8464494943505,"y":1297.9195906771888,"highlight":true,"active":true,"lines":[{"x":1075.0994182303004,"y":1325.3899033176901,"highlight":true,"active":true,"lines":[{"x":1206.9569189047063,"y":1259.4611529804872}]}]}],"textBox":{"active":false,"hasMoved":true,"movesIndependently":false,"drawnIndependently":true,"allowedOutsideImage":true,"hasBoundingBox":true,"x":739.9616040161853,"y":1207.2675589635348,"boundingBox":{"width":132.6845703125,"height":24,"left":530.1999969482422,"top":87.20000076293945},"moving":false},"invalidHandlePlacement":false},"uuid":"4d21ceae-710e-49ab-90c5-6a97db7870f6","canComplete":false,"highlight":false,"polyBoundingBox":{"left":1075.0994182303004,"top":721.0430252266631,"width":271.95609514096213,"height":604.3468780910271},"area":2611.8905008669453,"info":"{\"_type\":2,\"de_position_id\":{\"label\":\"胸部\",\"value\":4,\"key\":\"0\"},\"de_system_id\":{\"label\":\"肺\",\"value\":27,\"key\":\"5\"},\"de_organ_id\":{\"label\":\"椎隔三角区\",\"value\":209,\"key\":\"0\"},\"de_disease_id\":{\"label\":\"胸腔积液\",\"value\":129,\"key\":\"4\"},\"de_symptom_id\":{\"label\":\"侧位-肺叶边缘回缩\",\"value\":154,\"key\":\"6\"}}"}' data_json = r'{"body_examined":"head","body_examined_property":0.4,"conclusion":"分割成功","instance_id":"a97bbe0a-92563041-fed5e016-eac0e5f6-0f72d4af","points":{"belly":[[[3057,1347]],[[3020,1330]],[[2734,1275]],[[2549,1264]],[[2433,1291]],[[2305,1352]],[[2241,1426]],[[2206,1686]],[[2141,1772]],[[2063,1802]],[[2032,1859]],[[1970,1917]],[[1895,2034]],[[1822,2198]],[[1798,2275]],[[1782,2389]],[[1800,2501]],[[1826,2583]],[[1851,2615]],[[1902,2646]],[[1968,2662]],[[2237,2664]],[[2401,2627]],[[2769,2510]],[[2981,2457]],[[3057,2421]]],"chest":[[[2267,1332]],[[2250,1325]],[[2080,1346]],[[1613,1437]],[[1424,1490]],[[1345,1487]],[[1232,1515]],[[1034,1538]],[[918,1572]],[[846,1573]],[[752,1611]],[[712,1635]],[[666,1709]],[[626,1949]],[[593,2065]],[[600,2135]],[[652,2182]],[[712,2261]],[[839,2361]],[[1069,2473]],[[1159,2500]],[[1233,2508]],[[1395,2558]],[[1807,2635]],[[1827,2618]],[[1815,2592]],[[1813,2541]],[[1783,2468]],[[1771,2364]],[[1819,2181]],[[1888,2027]],[[1958,1911]],[[2058,1797]],[[2135,1764]],[[2189,1704]],[[2207,1661]],[[2211,1552]],[[2240,1423]],[[2266,1371]]],"forelimb":[[[175,1431]],[[133,1429]],[[110,1442]],[[60,1448]],[[35,1469]],[[23,1506]],[[0,1517]],[[0,2460]],[[10,2462]],[[25,2539]],[[38,2562]],[[82,2566]],[[126,2548]],[[303,2431]],[[439,2421]],[[444,2399]],[[400,2349]],[[382,2306]],[[381,2277]],[[401,2214]],[[392,2128]],[[433,2064]],[[488,2055]],[[503,2036]],[[505,1990]],[[465,1896]],[[449,1777]],[[393,1659]],[[389,1612]],[[431,1589]],[[429,1565]],[[373,1506]]],"head":[],"heart":[[[1052,1945]],[[1028,1996]],[[1010,2072]],[[1021,2138]],[[1106,2326]],[[1202,2447]],[[1256,2481]],[[1376,2531]],[[1508,2565]],[[1673,2582]],[[1718,2563]],[[1734,2540]],[[1736,2461]],[[1710,2237]],[[1609,2014]],[[1587,1946]],[[1548,1897]],[[1469,1846]],[[1280,1803]],[[1215,1821]],[[1160,1872]],[[1105,1901]]],"hindlimb":[],"sternum":[[[1110,2539]],[[1133,2571]],[[1208,2612]],[[1354,2665]],[[1475,2675]],[[1660,2721]],[[1772,2714]],[[1912,2690]],[[1872,2658]],[[1779,2660]],[[1683,2633]],[[1595,2628]],[[1497,2589]],[[1394,2575]],[[1213,2517]],[[1117,2523]]],"vertebra":[[[3057,1122]],[[3019,1101]],[[2918,1107]],[[2756,1081]],[[2691,1082]],[[2613,1051]],[[2534,1047]],[[2447,1073]],[[2310,1079]],[[2233,1116]],[[2184,1120]],[[1976,1187]],[[1820,1219]],[[1715,1225]],[[1656,1247]],[[1576,1255]],[[1499,1276]],[[1401,1278]],[[1164,1309]],[[681,1351]],[[637,1374]],[[608,1374]],[[564,1394]],[[553,1422]],[[579,1469]],[[679,1497]],[[924,1518]],[[1326,1470]],[[1473,1463]],[[2189,1317]],[[2374,1288]],[[2568,1281]],[[2721,1294]],[[3020,1340]],[[3057,1335]]]},"show_img":"https://ai-bianque.oss-cn-beijing.aliyuncs.com/a97bbe0a-92563041-fed5e016-eac0e5f6-0f72d4af_organ_seg.jpg"}' data = json.loads(data_json) points = data['points'][part] i = 0 c = len(points) area = [] while i < c: area.append([points[i][0][0],points[i][0][1]]) # area.append([int(points[i][0]),int(points[i][0])]) i+=1 if i == c: area.append([points[0][0][0],points[0][0][1]]) # pts = np.array(area, np.int32) # pts = pts.reshape((-1,1,2)) cv2.fillPoly(image, np.array([area]), color=(255, 255, 255)) if part == 'chest': points2 = data['points']['heart'] i = 0 c = len(points2) area2 = [] while i < c: area2.append([points2[i][0][0],points2[i][0][1]]) # area.append([int(points[i][0]),int(points[i][0])]) i+=1 if i == c: area2.append([points2[0][0][0],points2[0][0][1]]) cv2.fillPoly(image, np.array([area2]), color=(0, 0, 0)) # cv2.fillPoly(image, pixel_array, color=(255, 255, 255)) # cv2.imshow('image', image) # cv2.waitKey() # cv2.destroyAllWindows() ret, binary_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(binary_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) image3 = np.zeros((height, width, 1), np.uint8) cv2.drawContours(image3, contours, -1, 255, -1) a = np.where(image3==255)[0].reshape(-1,1) b = np.where(image3==255)[1].reshape(-1,1) coordinate = np.concatenate([a,b], axis=1).tolist() gray_kv = {} gray_value = [] gray_num = 0 gn = 0 for n in coordinate: gray_kv[(n[0],n[1])] = img[n[0],n[1]] gray_value.append(img[n[0],n[1]]) gray_num += img[n[0],n[1]] gn += 1 max_gray_value = max(gray_value) min_gray_value = min(gray_value) for k,v in gray_kv.items(): if v == max_gray_value: print(k) if v == min_gray_value: print(k) print(max(gray_value)) print(min(gray_value)) print(gray_num) print(gn) print(gray_num/gn) # for n in coordinate: # image2[n[0],n[1]] = image2[n[0],n[1]] + [255,255,255] # cv2.imshow('image', image2) # cv2.waitKey() # cv2.destroyAllWindows() # z = [] # for x in range(height): # for y in range(width): # if image[x,y] == [255, 255, 255]: # z.append[x,y] # 设置颜色阈值 # lower_green = np.array([254, 254, 254]) # upper_green = np.array([255, 255, 255]) # # 获取特定颜色范围内的点位 # mask = cv2.inRange(image, lower_green, upper_green) # # 使用cv2.findNonZero()来找到非零像素点的坐标 # non_zero_points = cv2.findNonZero(mask) # # 打印点位数量 # print(f"Number of green points: {len(non_zero_points)}") # # 如果需要可视化点位 # # 创建一个和原图一样大小的图片,并用点位标记 # green_points_image = cv2.cvtColor(np.zeros_like(image), cv2.COLOR_GRAY2BGR) # for point in non_zero_points: # x, y = point[0][0], point[0][1] # cv2.circle(green_points_image, (x, y), 5, (0, 255, 0), -1) # # cv2.polylines(image,[pts],True,(255,255,255)) # cv2.imshow('image', green_points_image) # cv2.waitKey() # cv2.destroyAllWindows() # ret, binary_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY) # cv2.imshow('image', binary_image) # cv2.waitKey() # cv2.destroyAllWindows() # contours, hierarchy = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) # contour_image = cv2.cvtColor(image2, cv2.COLOR_GRAY2BGR) # contours_in(contours) # image3 = np.zeros((height, width, 1), np.uint8) # cv2.drawContours(image3, contours, -1, 255, -1) # a = np.where(image3==255)[0].reshape(-1,1) # b = np.where(image3==255)[1].reshape(-1,1) # coordinate = np.concatenate([a,b], axis=1).tolist() # ab = [] # inside = [ for x in coordinate] # contour_image = cv2.cvtColor(image2, cv2.COLOR_GRAY2BGR) # cv2.drawContours(contour_image, inside, -1, (255,255,255), 2) # for n in coordinate: # image2[n[0],n[1]] = image2[n[0],n[1]] + [255,255,255] # cv2.imshow('image', image2) # cv2.waitKey() # cv2.destroyAllWindows() # cv2.fillPoly(pixel_array, np.array([area]), color=(255, 255, 255)) # cv2.imwrite(output_path+filename, image) # lower_threshold = 100 # 设置下限阈值 # upper_threshold = 200 # 设置上限阈值 # tissue_mask = (pixel_array > lower_threshold) & (pixel_array < upper_threshold) # tissue_density = np.mean(pixel_array) # print(tissue_density) # plt.imshow(pixel_array, cmap='gray') # # plt.contour(tissue_mask, colors='r') # 在图像上叠加感兴趣区域的轮廓 # plt.title(f'Tissue Density: {tissue_density:.2f}') # plt.show()