PNG16_MAX = pow(2, 16) - 1 # here if thinks the heaviest weight bit is for transparency or something not in use with dicom imgs
PNG8_MAX = pow(2, 8+1) - 1 # heaviest weight bit is 8 => 2**8, but dont forget the others: the reason of +1
-INPUT_DIR = '../../Data/Images_anonymous/Case_0449/'
+INPUT_DIR = '../../Data/Images_anonymous/Case_0350/'
OUT_DIR = './generated/'
#os.mkdir(outdir)
+def ftrim(Mat):
+ # private func to trim the Matrix, but in one direction, vertically | horizontally
+ # return the slice, don't affect the Matrix
+ # y | : for column, x -> : for rows
+ # v
+ # not my fault, numpy architecture
+ y1 = y2 = i = 0
+ while i < len(Mat):# search by top
+ if max(Mat[i]) > 0:
+ y1 = i
+ break
+ i += 1
+ i = len(Mat) - 1
+ while i >= 0:# search by bottom
+ if max(Mat[i]) > 0:
+ y2 = i
+ break
+ i -= 1
+ # print('y1, y2', y1, y2)
+ return slice(y1, y2+1)# +1 to stop at y2
+
+def trim(Mat):
+ # most use who make implicit call of ftrim twice
+ horizontal = ftrim(Mat)
+ vertical = ftrim(Mat.T)
+ # print('horizontal:vertical', horizontal, vertical)
+ return Mat[horizontal, vertical]
+
def map16(array):# can be useful in future
return array * 16
return '/'.join(filepath.split('/')[:-1]) + '/'
def topng(inputfile, outfile=None, overwrite=True):
+ """
+ return (64, 64) : the width and height
+ """
try:
dicimg = pydicom.read_file(inputfile) # read dicom image
except pydicom.errors.InvalidDicomError as e:
# @TODO: log, i can't read this file
return
- img = dicimg.pixel_array# get image array (12bits)
+ img = trim(dicimg.pixel_array)# get image array (12bits)
# test <<
- print('img', img)
+ # return img.shape # $$ COMMENT OR REMOVE THIS LINE
+ # print('img', img)
# print('img', type(img))
# print('min', img.min(), 'max', img.max())
# dicimg.convert_pixel_data() # same as using dicimg.pixel_array
# test >>
# affine transfo to png 16 bits, func affects img variable
- maxdepth = img.max()
- # print('maxdepth, PNG8_MAX', maxdepth, PNG8_MAX) # testing..
+ maxdepth = pow(2, dicimg.BitsStored) - 1
+ print('dicimg.BitsStored, PNG8_MAX', dicimg.BitsStored, PNG8_MAX) # testing..
affine(img,
- (img.min(), maxdepth),
+ (0, maxdepth), # img.min() replace 0 may be, but not sure it would be good choice
(0, PNG16_MAX if maxdepth > PNG8_MAX else PNG8_MAX)
)
cv2.imwrite(savepath, img, [cv2.IMWRITE_PNG_COMPRESSION, 0]) # write png image
+ # test
+ return img.shape
def topngs(inputdir, outdir):
"""
topng( inputdir + f, join(outdir, f) )
if __name__ == '__main__':
- # topngs( INPUT_DIR, join(OUT_DIR, INPUT_DIR.split('/')[-2]) )
- topng(INPUT_DIR+'Image00001', OUT_DIR + INPUT_DIR.split('/')[-2] +'-Image00001')
+ topngs( INPUT_DIR, join(OUT_DIR, INPUT_DIR.split('/')[-2]) )
+ # topng(INPUT_DIR+'Image00001', OUT_DIR + INPUT_DIR.split('/')[-2] +'-Image00001')