Commit 42876a2514e4ecae9aa9c0a50c1a43c4207c2c63
1 parent
259b8d63
Exists in
orientation
Verifying if sagittal case if stacking from left->right or right->left
Created an function to know if the sagittal case is stacked from left->right or right->left. It considers the sign of distance between two consecutive slices. If positive it is righ->left, otherwise is left->right.
Showing
1 changed file
with
45 additions
and
4 deletions
Show diff stats
invesalius/data/imagedata_utils.py
| ... | ... | @@ -416,6 +416,40 @@ class ImageCreator: |
| 416 | 416 | |
| 417 | 417 | return imagedata |
| 418 | 418 | |
| 419 | + | |
| 420 | +def get_stacking_direction(files, orientation): | |
| 421 | + """ | |
| 422 | + Returns the direction of stacking in the given orientation: | |
| 423 | + | |
| 424 | + AXIAL: | |
| 425 | + | |
| 426 | + CORONAL: | |
| 427 | + | |
| 428 | + SAGITAL: +1 - Right -> Left | |
| 429 | + -1 - Left -> Right | |
| 430 | + """ | |
| 431 | + r1 = vtkgdcm.vtkGDCMImageReader() | |
| 432 | + r1.SetFileName(files[0]) | |
| 433 | + r1.Update() | |
| 434 | + p1 = r1.GetImagePositionPatient() | |
| 435 | + | |
| 436 | + r2 = vtkgdcm.vtkGDCMImageReader() | |
| 437 | + r2.SetFileName(files[1]) | |
| 438 | + r2.Update() | |
| 439 | + p2 = r2.GetImagePositionPatient() | |
| 440 | + | |
| 441 | + if orientation == 'SAGITTAL': | |
| 442 | + d = (p2[0] - p1[0]) | |
| 443 | + | |
| 444 | + elif orientation == 'CORONAL': | |
| 445 | + d = (p2[1] - p1[1]) | |
| 446 | + | |
| 447 | + else: | |
| 448 | + d = (p2[2] - p1[2]) | |
| 449 | + | |
| 450 | + return int(d / abs(d)) | |
| 451 | + | |
| 452 | + | |
| 419 | 453 | def dcm2memmap(files, slice_size, orientation, resolution_percentage): |
| 420 | 454 | """ |
| 421 | 455 | From a list of dicom files it creates memmap file in the temp folder and |
| ... | ... | @@ -452,6 +486,8 @@ def dcm2memmap(files, slice_size, orientation, resolution_percentage): |
| 452 | 486 | max_scalar = None |
| 453 | 487 | min_scalar = None |
| 454 | 488 | |
| 489 | + d = get_stacking_direction(files, orientation) | |
| 490 | + | |
| 455 | 491 | for n, f in enumerate(files): |
| 456 | 492 | dcm_reader.SetFileName(f) |
| 457 | 493 | dcm_reader.Update() |
| ... | ... | @@ -477,10 +513,15 @@ def dcm2memmap(files, slice_size, orientation, resolution_percentage): |
| 477 | 513 | matrix[:, -n-1, :] = array |
| 478 | 514 | elif orientation == 'SAGITTAL': |
| 479 | 515 | array.shape = matrix.shape[0], matrix.shape[1] |
| 480 | - # TODO: Verify if it's necessary to add the slices swapped only in | |
| 481 | - # sagittal rmi or only in # Rasiane's case or is necessary in all | |
| 482 | - # sagittal cases. | |
| 483 | - matrix[:, :,-n-1] = array[:, ::-1] | |
| 516 | + | |
| 517 | + print ">>>", d, dcm_reader.GetImagePositionPatient(), dcm_reader.GetDirectionCosines() | |
| 518 | + | |
| 519 | + # stacking from right to left | |
| 520 | + if d == 1: | |
| 521 | + matrix[:, :,n] = array[:, ::-1] | |
| 522 | + # stacking from left to right | |
| 523 | + else: | |
| 524 | + matrix[:, :,-n-1] = array[:, ::-1] | |
| 484 | 525 | else: |
| 485 | 526 | print array.shape, matrix.shape |
| 486 | 527 | array.shape = matrix.shape[1], matrix.shape[2] | ... | ... |