-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
210 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#' Check x-ray lung orientation. | ||
#' | ||
#' Check the correctness of image orientation, i.e., flipped left-right, up-down, | ||
#' or both. If True, attempts to correct before returning corrected image. Otherwise | ||
#' it returns NULL. | ||
#' | ||
#' @param image input 3-D lung image. | ||
#' @param antsxnetCacheDirectory destination directory for storing the downloaded | ||
#' template and model weights. Since these can be resused, if | ||
#' \code{is.null(antsxnetCacheDirectory)}, these data will be downloaded to the | ||
#' subdirectory ~/.keras/ANTsXNet/. | ||
#' @param verbose print progress. | ||
#' @return segmentation and probability images | ||
#' @author Tustison NJ | ||
#' @examples | ||
#' \dontrun{ | ||
#' library( ANTsRNet ) | ||
#' } | ||
#' @import keras | ||
#' @export | ||
checkXrayLungOrientation <- function( image, | ||
antsxnetCacheDirectory = NULL, | ||
verbose = FALSE ) | ||
{ | ||
if( image@dimension != 2 ) | ||
{ | ||
stop( "ImageDimension must be equal to 2.") | ||
} | ||
|
||
resampledImageSize <- c( 224, 224 ) | ||
if( any( dim( image ) != resampledImageSize ) ) | ||
{ | ||
if( verbose ) | ||
{ | ||
cat( "Resampling image to ", resampledImageSize, "\n" ) | ||
} | ||
resampledImage <- resampleImage( image, resampledImageSize, useVoxels = TRUE ) | ||
} | ||
else | ||
{ | ||
resampledImage <- antsImageClone( image ) | ||
} | ||
|
||
model <- createResNetModel2D( c( resampledImageSize, 1 ), | ||
numberOfClassificationLabels = 3, | ||
mode = "classification", | ||
layers = c( 1, 2, 3, 4 ), | ||
residualBlockSchedule = c( 2, 2, 2, 2 ), | ||
lowestResolution = 64, | ||
cardinality = 1, | ||
squeezeAndExcite = FALSE ) | ||
|
||
weightsFileName <- getPretrainedNetwork( "xrayLungOrientation", | ||
antsxnetCacheDirectory = antsxnetCacheDirectory ) | ||
model$load_weights( weightsFileName ) | ||
|
||
imageMin <- min( resampledImage ) | ||
imageMax <- max( resampledImage ) | ||
normalizedImage <- antsImageClone( resampledImage ) | ||
normalizedImage <- ( normalizedImage - imageMin ) / ( imageMax - imageMin ) | ||
|
||
batchX <- array( data = as.array( normalizedImage ), dim = c( 1, resampledImageSize, 1 ) ) | ||
batchY <- model %>% predict( batchX, verbose = verbose ) | ||
|
||
# batchY is a 3-element array: | ||
# batchY[0] = Pr(image is correctly oriented) | ||
# batchY[1] = Pr(image is flipped up/down) | ||
# batchY[2] = Pr(image is flipped left/right) | ||
|
||
if( batchY[1, 1] > 0.5 ) | ||
{ | ||
return( NULL ) | ||
} else { | ||
if( verbose ) | ||
{ | ||
message( "Possible incorrect orientation. Attempting to correct." ) | ||
} | ||
normalizedImageArray <- as.array( normalizedImage ) | ||
imageUpDown <- as.antsImage( normalizedImageArray[,dim( normalizedImageArray )[2]:1], | ||
origin = antsGetOrigin( resampledImage ), | ||
spacing = antsGetSpacing( resampledImage ), | ||
direction = antsGetDirection( resampledImage ) ) | ||
imageLeftRight <- as.antsImage( normalizedImageArray[dim( normalizedImageArray )[1]:1,], | ||
origin = antsGetOrigin( resampledImage ), | ||
spacing = antsGetSpacing( resampledImage ), | ||
direction = antsGetDirection( resampledImage ) ) | ||
imageBoth <- as.antsImage( ( normalizedImageArray[,dim( normalizedImageArray )[2]:1] )[dim( normalizedImageArray )[1]:1,], | ||
origin = antsGetOrigin( resampledImage ), | ||
spacing = antsGetSpacing( resampledImage ), | ||
direction = antsGetDirection( resampledImage ) ) | ||
|
||
batchX <- array( data = 0, dim = c( 3, resampledImageSize, 1 ) ) | ||
batchX[1,,,1] <- as.array( imageUpDown ) | ||
batchX[2,,,1] <- as.array( imageLeftRight ) | ||
batchX[3,,,1] <- as.array( imageBoth ) | ||
|
||
batchY <- model %>% predict( batchX, verbose = verbose ) | ||
|
||
imageArray <- as.array( image ) | ||
orientedImage <- NULL | ||
if( batchY[1, 1] > batchY[2, 1] && batchY[1, 1] > batchY[3, 1] ) | ||
{ | ||
if( verbose ) | ||
{ | ||
message( "Image is flipped up-down." ) | ||
} | ||
orientedImage <- as.antsImage( imageArray[,dim( imageArray )[2]:1], | ||
origin = antsGetOrigin( image ), | ||
spacing = antsGetSpacing( image ), | ||
direction = antsGetDirection( image ) ) | ||
} else if( batchY[2, 1] > batchY[1, 1] && batchY[2, 1] > batchY[3, 1] ) { | ||
if( verbose ) | ||
{ | ||
message( "Image is flipped right-left." ) | ||
} | ||
orientedImage <- as.antsImage( imageArray[dim( imageArray )[1]:1,], | ||
origin = antsGetOrigin( image ), | ||
spacing = antsGetSpacing( image ), | ||
direction = antsGetDirection( image ) ) | ||
} else { | ||
if( verbose ) | ||
{ | ||
message( "Image is flipped up-down and right-left." ) | ||
} | ||
orientedImage <- as.antsImage( ( imageArray[,dim( imageArray )[2]:1] )[dim( imageArray )[1]:1,], | ||
origin = antsGetOrigin( image ), | ||
spacing = antsGetSpacing( image ), | ||
direction = antsGetDirection( image ) ) | ||
} | ||
|
||
return( orientedImage ) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
pandoc: '0' | ||
pkgdown: 2.0.6 | ||
pkgdown: 1.6.1 | ||
pkgdown_sha: ~ | ||
articles: {} | ||
last_built: 2023-09-09T00:57Z | ||
last_built: 2023-09-25T22:03Z | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.