4040import requests
4141import orjson
4242import pydicom
43-
44-
45- def get_windowed_image (ds ):
46- # Apply Rescale Slope and Rescale Intercept if they exist
47- pixel_array = ds .pixel_array .astype (float )
48- rescale_slope = ds .get ("RescaleSlope" , 1 )
49- rescale_intercept = ds .get ("RescaleIntercept" , 0 )
50- if rescale_slope != 1 or rescale_intercept != 0 :
51- pixel_array = pixel_array * rescale_slope + rescale_intercept
52-
53- # Check for windowing information in the DICOM metadata
54- window_center = ds .get ("WindowCenter" , None )
55- window_width = ds .get ("WindowWidth" , None )
56-
57- if window_center is None or window_width is None :
58- # If no windowing info, use the full dynamic range
59- window_min = pixel_array .min ()
60- window_max = pixel_array .max ()
61- else :
62- # Handle possible multi-valued tags by taking the first value
63- if isinstance (window_center , pydicom .multival .MultiValue ):
64- window_center = window_center [0 ]
65- if isinstance (window_width , pydicom .multival .MultiValue ):
66- window_width = window_width [0 ]
67-
68- window_min = window_center - window_width / 2
69- window_max = window_center + window_width / 2
70-
71- # Apply windowing
72- pixel_array = np .clip (pixel_array , window_min , window_max )
73-
74- # Normalize to 0-255
75- if window_max > window_min :
76- pixel_array = ((pixel_array - window_min ) / (window_max - window_min )) * 255.0
77- else : # Handle case where all pixels are the same
78- pixel_array .fill (128 )
79-
80- # Handle Photometric Interpretation
81- photometric_interpretation = ds .get ("PhotometricInterpretation" , "MONOCHROME2" )
82- if photometric_interpretation == "MONOCHROME1" :
83- pixel_array = 255.0 - pixel_array
84-
85- # Convert to 8-bit unsigned integer
86- image_8bit = pixel_array .astype (np .uint8 )
87-
88- return image_8bit
43+ from ISAT .annotation import get_windowed_image
8944
9045
9146class QtBoxStyleProgressBar (QtWidgets .QProgressBar ):
@@ -254,7 +209,7 @@ def run(self):
254209
255210 image_path = os .path .join (self .mainwindow .image_root , self .mainwindow .files_list [index ])
256211
257- image_data = np . array ( Image . open ( image_path ). convert ( 'RGB' ) )
212+ image_data = self . mainwindow . current_label . get_img_data ( to_rgb = True )
258213 try :
259214 features , original_size , input_size = self .sam_encoder (image_data )
260215 except Exception as e :
@@ -1227,17 +1182,31 @@ def show_image(self, index: int, zoomfit: bool=True):
12271182 self .plugin_manager_dialog .trigger_before_image_open (file_path )
12281183
12291184 if file_path .lower ().endswith ('.dcm' ):
1230- ds = pydicom .dcmread (file_path )
1231- image_data = Image .fromarray (get_windowed_image (ds ))
12321185 self .can_be_annotated = True
12331186 else :
1234- image_data = Image .open (file_path )
1235- png_palette = image_data .getpalette ()
1236- if png_palette is not None and file_path .endswith ('.png' ):
1237- self .statusbar .showMessage ('This image might be a label image in VOC format.' )
1238- self .can_be_annotated = False
1239- else :
1240- self .can_be_annotated = True
1187+ try :
1188+ image = Image .open (file_path )
1189+ png_palette = image .getpalette ()
1190+ if png_palette is not None and file_path .endswith ('.png' ):
1191+ self .statusbar .showMessage ('This image might be a label image in VOC format.' )
1192+ self .can_be_annotated = False
1193+ else :
1194+ self .can_be_annotated = True
1195+ except Exception as e :
1196+ # stausbar show error
1197+ self .statusbar .showMessage (str (e ), 5000 )
1198+ return
1199+
1200+ # load label
1201+ if self .can_be_annotated :
1202+ self .current_group = 1
1203+ _ , name = os .path .split (file_path )
1204+ label_path = os .path .join (self .label_root , '.' .join (name .split ('.' )[:- 1 ]) + '.json' )
1205+ self .current_label = Annotation (file_path , label_path )
1206+ # 载入数据
1207+ self .current_label .load_annotation ()
1208+ # get image data and info
1209+ self .current_label .get_img_data ()
12411210
12421211 if self .can_be_annotated :
12431212 self .actionPolygon .setEnabled (True )
@@ -1262,28 +1231,22 @@ def show_image(self, index: int, zoomfit: bool=True):
12621231 self .view .zoomfit ()
12631232
12641233 # 判断图像是否旋转
1265- exif_info = image_data .getexif ()
1266- if exif_info and exif_info .get (274 , 1 ) != 1 :
1267- warning_info = '这幅图像包含EXIF元数据,且图像的方向已被旋转.\n 建议去除EXIF信息后再进行标注\n 你可以使用[菜单栏]-[工具]-[处理exif标签]功能处理图像的旋转问题。' \
1268- if self .cfg ['software' ]['language' ] == 'zh' \
1269- else 'This image has EXIF metadata, and the image orientation is rotated.\n Suggest labeling after removing the EXIF metadata.\n You can use the function of [Process EXIF tag] in [Tools] in [Menu bar] to deal with the problem of images.'
1270- QtWidgets .QMessageBox .warning (self , 'Warning' , warning_info , QtWidgets .QMessageBox .Ok )
1234+ if not file_path .lower ().endswith ('.dcm' ):
1235+ image_data = Image .open (file_path )
1236+ exif_info = image_data .getexif ()
1237+ if exif_info and exif_info .get (274 , 1 ) != 1 :
1238+ warning_info = '这幅图像包含EXIF元数据,且图像的方向已被旋转.\n 建议去除EXIF信息后再进行标注\n 你可以使用[菜单栏]-[工具]-[处理exif标签]功能处理图像的旋转问题。' \
1239+ if self .cfg ['software' ]['language' ] == 'zh' \
1240+ else 'This image has EXIF metadata, and the image orientation is rotated.\n Suggest labeling after removing the EXIF metadata.\n You can use the function of [Process EXIF tag] in [Tools] in [Menu bar] to deal with the problem of images.'
1241+ QtWidgets .QMessageBox .warning (self , 'Warning' , warning_info , QtWidgets .QMessageBox .Ok )
12711242
12721243 if self .use_segment_anything and self .can_be_annotated :
12731244 self .segany .reset_image ()
12741245 self .seganythread .index = index
12751246 self .seganythread .start ()
12761247 self .SeganyEnabled ()
12771248
1278- # load label
12791249 if self .can_be_annotated :
1280- self .current_group = 1
1281- _ , name = os .path .split (file_path )
1282- label_path = os .path .join (self .label_root , '.' .join (name .split ('.' )[:- 1 ]) + '.json' )
1283- self .current_label = Annotation (file_path , label_path )
1284- # 载入数据
1285- self .current_label .load_annotation ()
1286-
12871250 for object in self .current_label .objects :
12881251 try :
12891252 group = int (object .group )
0 commit comments