-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #537 from saalfeldlab/feat/intensityThreshold
Feat/intensity threshold
- Loading branch information
Showing
18 changed files
with
513 additions
and
82 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
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
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
225 changes: 225 additions & 0 deletions
225
src/main/kotlin/org/janelia/saalfeldlab/paintera/control/modes/RawSourceMode.kt
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,225 @@ | ||
package org.janelia.saalfeldlab.paintera.control.modes | ||
|
||
import javafx.beans.value.ChangeListener | ||
import javafx.collections.FXCollections | ||
import javafx.collections.ObservableList | ||
import javafx.scene.input.KeyCode | ||
import javafx.scene.input.KeyEvent.KEY_PRESSED | ||
import net.imglib2.RandomAccessibleInterval | ||
import net.imglib2.histogram.Histogram1d | ||
import net.imglib2.histogram.Real1dBinMapper | ||
import net.imglib2.realtransform.AffineTransform3D | ||
import net.imglib2.type.label.LabelMultisetType | ||
import net.imglib2.type.label.VolatileLabelMultisetType | ||
import net.imglib2.type.numeric.IntegerType | ||
import net.imglib2.type.numeric.RealType | ||
import net.imglib2.type.numeric.integer.IntType | ||
import net.imglib2.type.numeric.integer.UnsignedLongType | ||
import net.imglib2.type.numeric.real.DoubleType | ||
import net.imglib2.type.volatiles.AbstractVolatileRealType | ||
import net.imglib2.util.Intervals | ||
import net.imglib2.util.Util | ||
import net.imglib2.view.IntervalView | ||
import org.janelia.saalfeldlab.bdv.fx.viewer.ViewerPanelFX | ||
import org.janelia.saalfeldlab.fx.actions.ActionSet | ||
import org.janelia.saalfeldlab.fx.actions.ActionSet.Companion.installActionSet | ||
import org.janelia.saalfeldlab.fx.actions.ActionSet.Companion.removeActionSet | ||
import org.janelia.saalfeldlab.fx.actions.painteraActionSet | ||
import org.janelia.saalfeldlab.fx.ortho.OrthogonalViews | ||
import org.janelia.saalfeldlab.fx.ui.ScaleView | ||
import org.janelia.saalfeldlab.net.imglib2.converter.ARGBColorConverter | ||
import org.janelia.saalfeldlab.paintera.control.actions.AllowedActions | ||
import org.janelia.saalfeldlab.paintera.control.tools.Tool | ||
import org.janelia.saalfeldlab.paintera.paintera | ||
import org.janelia.saalfeldlab.paintera.state.SourceStateBackendN5 | ||
import org.janelia.saalfeldlab.paintera.state.raw.ConnectomicsRawState | ||
import org.janelia.saalfeldlab.util.* | ||
|
||
|
||
object RawSourceMode : AbstractToolMode() { | ||
|
||
override val defaultTool: Tool = NavigationTool | ||
|
||
override val tools: ObservableList<Tool> = FXCollections.observableArrayList() | ||
|
||
override val allowedActions = AllowedActions.NAVIGATION | ||
|
||
private val minMaxIntensityThreshold = painteraActionSet("Min/Max Intensity Threshold") { | ||
verifyAll(KEY_PRESSED, "Source State is Raw Source State ") { activeSourceStateProperty.get() is ConnectomicsRawState<*, *> } | ||
KEY_PRESSED(KeyCode.SHIFT, KeyCode.Y) { | ||
graphic = { ScaleView().apply { styleClass += "intensity-reset-min-max" } } | ||
onAction { | ||
val rawSource = activeSourceStateProperty.get() as ConnectomicsRawState<*, *> | ||
resetIntensityMinMax(rawSource) | ||
} | ||
} | ||
KEY_PRESSED(KeyCode.Y) { | ||
lateinit var viewer: ViewerPanelFX | ||
graphic = { ScaleView().apply { styleClass += "intensity-auto-min-max" } } | ||
verify("Last focused viewer found") { paintera.baseView.lastFocusHolder.value?.viewer()?.also { viewer = it } != null } | ||
onAction { | ||
val rawSource = activeSourceStateProperty.get() as ConnectomicsRawState<*, *> | ||
autoIntensityMinMax(rawSource, viewer) | ||
} | ||
} | ||
} | ||
|
||
fun autoIntensityMinMax(rawSource: ConnectomicsRawState<*, *>, viewer: ViewerPanelFX) { | ||
val globalToViewerTransform = AffineTransform3D().also { viewer.state.getViewerTransform(it) } | ||
val viewerInterval = Intervals.createMinSize(0, 0, 0, viewer.width.toLong(), viewer.height.toLong(), 1L) | ||
|
||
val scaleLevel = viewer.state.bestMipMapLevel | ||
val dataSource = rawSource.getDataSource().getDataSource(0, scaleLevel) as RandomAccessibleInterval<RealType<*>> | ||
|
||
val sourceToGlobalTransform = rawSource.getDataSource().getSourceTransformCopy(0, scaleLevel) | ||
|
||
|
||
val extension = Util.getTypeFromInterval(dataSource).createVariable().let { | ||
when (it) { | ||
is VolatileLabelMultisetType, is LabelMultisetType -> UnsignedLongType(0) | ||
else -> it | ||
} | ||
} | ||
|
||
|
||
val screenSource = dataSource | ||
.extendValue(extension) | ||
.interpolateNearestNeighbor() | ||
.affineReal(globalToViewerTransform.concatenate(sourceToGlobalTransform)) | ||
.raster() | ||
.interval(viewerInterval) | ||
|
||
val converter = rawSource.converter() | ||
val curMin = converter.minProperty().get() | ||
val curMax = converter.maxProperty().get() | ||
|
||
if ( curMin == curMax) { | ||
resetIntensityMinMax(rawSource) | ||
return | ||
} | ||
if (converterAtDefault(rawSource)) { | ||
estimateWithRange(screenSource, converter) | ||
} | ||
|
||
if (extension is IntegerType<*>) | ||
estimateWithHistogram(IntType(), screenSource, rawSource, converter) | ||
else | ||
estimateWithHistogram(DoubleType(), screenSource, rawSource, converter) | ||
} | ||
|
||
private fun estimateWithRange(screenSource: IntervalView<RealType<*>>, converter: ARGBColorConverter<out AbstractVolatileRealType<*, *>>) { | ||
var min = screenSource.cursor().get().realDouble | ||
var max = min | ||
screenSource.forEach { | ||
val value = it.realDouble | ||
if (value < min) | ||
min = value | ||
if (value > max) | ||
max = value | ||
} | ||
converter.min = min | ||
converter.max = max | ||
} | ||
|
||
private fun <T : RealType<T>> estimateWithHistogram(type: T, screenSource: IntervalView<RealType<*>>, rawSource: ConnectomicsRawState<*, *>, converter: ARGBColorConverter<out AbstractVolatileRealType<*, *>>) { | ||
val binMapper = Real1dBinMapper<T>(converter.min, converter.max, 4, false) | ||
val histogram = Histogram1d(binMapper) | ||
val img = screenSource.convert(type) { src, target -> target.setReal(src.realDouble) }.asIterable() | ||
histogram.countData(img) | ||
|
||
val numPixels = screenSource.dimensionsAsLongArray().sum() | ||
|
||
|
||
val minBinIdx = histogram.indexOfFirst { i -> i.get() > (numPixels / 5000) } | ||
val maxBinIdx = histogram.indexOfLast { i -> i.get() > (numPixels / 5000) } | ||
|
||
val updateOrResetConverter = { min : Double, max : Double -> | ||
if (converter.minProperty().value == min && converter.maxProperty().value == max) | ||
resetIntensityMinMax(rawSource) | ||
else { | ||
converter.min = min | ||
converter.max = max | ||
} | ||
} | ||
|
||
when { | ||
minBinIdx == -1 && maxBinIdx == -1 -> resetIntensityMinMax(rawSource) | ||
minBinIdx == maxBinIdx -> { | ||
updateOrResetConverter( | ||
histogram.getLowerBound(minBinIdx.toLong(), type).let { type.realDouble }, | ||
histogram.getUpperBound(maxBinIdx.toLong(), type).let { type.realDouble } | ||
) | ||
} | ||
|
||
else -> { | ||
updateOrResetConverter( | ||
histogram.getCenterValue(minBinIdx.toLong(), type).let { type.realDouble }, | ||
histogram.getCenterValue(maxBinIdx.toLong(), type).let { type.realDouble } | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun converterAtDefault(rawSource: ConnectomicsRawState<*, *>) : Boolean { | ||
val converter = rawSource.converter() | ||
(rawSource.backend as? SourceStateBackendN5<*, *>)?.getMetadataState()?.let { | ||
return converter.min == it.minIntensity && converter.max == it.maxIntensity | ||
} | ||
|
||
val dataSource = rawSource.getDataSource().getDataSource(0, 0) as RandomAccessibleInterval<RealType<*>> | ||
val extension = Util.getTypeFromInterval(dataSource).createVariable().let { | ||
when (it) { | ||
is VolatileLabelMultisetType, is LabelMultisetType -> UnsignedLongType(0) | ||
else -> it | ||
} | ||
} | ||
|
||
return converter.minProperty().get() == extension.minValue && converter.maxProperty().get() == extension.maxValue | ||
} | ||
|
||
fun resetIntensityMinMax(rawSource: ConnectomicsRawState<*, *>) { | ||
|
||
(rawSource.backend as? SourceStateBackendN5<*, *>)?.getMetadataState()?.let { | ||
rawSource.converter().min = it.minIntensity | ||
rawSource.converter().max = it.maxIntensity | ||
return | ||
} | ||
|
||
val dataSource = rawSource.getDataSource().getDataSource(0, 0) as RandomAccessibleInterval<RealType<*>> | ||
val extension = Util.getTypeFromInterval(dataSource).createVariable().let { | ||
when (it) { | ||
is VolatileLabelMultisetType, is LabelMultisetType -> UnsignedLongType(0) | ||
else -> it | ||
} | ||
} | ||
|
||
rawSource.converter().minProperty().set(extension.minValue) | ||
rawSource.converter().maxProperty().set(extension.maxValue) | ||
} | ||
|
||
override val modeActions: List<ActionSet> = listOf(minMaxIntensityThreshold) | ||
|
||
private val moveToolTriggersToActiveViewer = ChangeListener<OrthogonalViews.ViewerAndTransforms?> { _, old, new -> | ||
/* remove the tool triggers from old, add to new */ | ||
modeActions.forEach { actionSet -> | ||
old?.viewer()?.removeActionSet(actionSet) | ||
new?.viewer()?.installActionSet(actionSet) | ||
} | ||
|
||
/* set the currently activeTool for this viewer */ | ||
switchTool(activeTool ?: NavigationTool) | ||
} | ||
|
||
override fun enter() { | ||
activeViewerProperty.addListener(moveToolTriggersToActiveViewer) | ||
super.enter() | ||
} | ||
|
||
override fun exit() { | ||
activeViewerProperty.removeListener(moveToolTriggersToActiveViewer) | ||
super.exit() | ||
} | ||
|
||
} | ||
|
||
|
Oops, something went wrong.