|
11 | 11 |
|
12 | 12 | import numpy as np |
13 | 13 | import pandas as pd |
| 14 | +import SimpleITK as sitk |
14 | 15 | from PIL import Image |
15 | | -from skimage.color import gray2rgb, rgb2hsv, hsv2rgb, rgb2lab, lab2rgb, lch2lab, lab2lch |
| 16 | +from skimage.color import ( |
| 17 | + rgb2gray, gray2rgb, rgb2hsv, hsv2rgb, rgb2lab, lab2rgb, lch2lab, lab2lch) |
16 | 18 |
|
17 | 19 | #: landmarks coordinates, loading from CSV file |
18 | 20 | LANDMARK_COORDS = ['X', 'Y'] |
@@ -344,6 +346,83 @@ def save_image(path_image, image): |
344 | 346 | image.save(path_image) |
345 | 347 |
|
346 | 348 |
|
| 349 | +@io_image_decorate |
| 350 | +def convert_from_mhd(path_image, path_out_dir=None, img_ext='.png'): |
| 351 | + """ convert standard image to MHD format |
| 352 | +
|
| 353 | + .. ref:: https://www.programcreek.com/python/example/96382/SimpleITK.WriteImage |
| 354 | +
|
| 355 | + :param str path_image: path to the input image |
| 356 | + :param str path_out_dir: path to output directory, if None use the input dir |
| 357 | + :param str img_ext: image extension like PNG or JPEG |
| 358 | + :return str: path to exported image |
| 359 | +
|
| 360 | + >>> path_img = os.path.join(update_path('data_images'), 'images', |
| 361 | + ... 'artificial_reference.jpg') |
| 362 | + >>> path_img = convert_to_mhd(path_img) |
| 363 | + >>> convert_from_mhd(path_img) # doctest: +ELLIPSIS |
| 364 | + '...artificial_reference.png' |
| 365 | + """ |
| 366 | + path_image = update_path(path_image) |
| 367 | + assert os.path.isfile(path_image), 'missing image: %s' % path_image |
| 368 | + # Reads the image using SimpleITK |
| 369 | + itk_image = sitk.ReadImage(path_image) |
| 370 | + |
| 371 | + # Convert the image to a numpy array first and then shuffle the dimensions |
| 372 | + # to get axis in the order z,y,x |
| 373 | + img = sitk.GetArrayFromImage(itk_image) |
| 374 | + |
| 375 | + # define output/destination path |
| 376 | + img_name = os.path.splitext(os.path.basename(path_image))[0] |
| 377 | + if not path_out_dir: |
| 378 | + path_out_dir = os.path.dirname(path_image) |
| 379 | + path_image = os.path.join(path_out_dir, img_name + img_ext) |
| 380 | + save_image(path_image, img) |
| 381 | + return path_image |
| 382 | + |
| 383 | + |
| 384 | +@io_image_decorate |
| 385 | +def convert_to_mhd(path_image, path_out_dir=None, to_gray=True, overwrite=True): |
| 386 | + """ converting standard image to MHD (Nifty format) |
| 387 | +
|
| 388 | + .. ref:: https://stackoverflow.com/questions/37290631 |
| 389 | +
|
| 390 | + :param str path_image: path to the input image |
| 391 | + :param str path_out_dir: path to output directory, if None use the input dir |
| 392 | + :param bool overwrite: allow overwrite existing image |
| 393 | + :return str: path to exported image |
| 394 | +
|
| 395 | + >>> path_img = os.path.join(update_path('data_images'), 'images', |
| 396 | + ... 'artificial_moving-affine.jpg') |
| 397 | + >>> convert_to_mhd(path_img) # doctest: +ELLIPSIS |
| 398 | + '...artificial_moving-affine.mhd' |
| 399 | + """ |
| 400 | + path_image = update_path(path_image) |
| 401 | + # define output/destination path |
| 402 | + img_name = os.path.splitext(os.path.basename(path_image))[0] |
| 403 | + if not path_out_dir: |
| 404 | + path_out_dir = os.path.dirname(path_image) |
| 405 | + path_image_new = os.path.join(path_out_dir, img_name + '.mhd') |
| 406 | + # in case the image exists and you are not allowed to overwrite it |
| 407 | + if os.path.isfile(path_image_new) and not overwrite: |
| 408 | + return path_image_new |
| 409 | + |
| 410 | + img = load_image(path_image) |
| 411 | + # if required and RGB on input convert to gray-scale |
| 412 | + if to_gray and img.ndim == 3 and img.shape[2] in (3, 4): |
| 413 | + img = rgb2gray(img) |
| 414 | + # the MHD usually require pixel value range (0, 255) |
| 415 | + if np.max(img) <= 1.5: |
| 416 | + img = np.round(img * 255) |
| 417 | + np.clip(img, a_min=0, a_max=255, out=img) |
| 418 | + |
| 419 | + image = sitk.GetImageFromArray(img.astype(np.uint8), isVector=False) |
| 420 | + |
| 421 | + # do not use text in MHD, othwerwise it crash DROP method |
| 422 | + sitk.WriteImage(image, path_image_new, False) |
| 423 | + return path_image_new |
| 424 | + |
| 425 | + |
347 | 426 | def image_histogram_matching(source, reference, use_color='hsv'): |
348 | 427 | """ adjust image histogram between two images |
349 | 428 |
|
|
0 commit comments