...
 
Commits (2)
# org.bioimageanalysis.icy.surf
# SURF - Speeded Up Robust Features
Surf feature matching
\ No newline at end of file
This repository contains the code used for the plugin Match SURF Features and Extract SURF Features.
This two plugins are available as both Interface plugins and Protocols blocks.
The project has been prepared using gradle and eclipse to simplify project configuration. See below to setup this project on your machine.
## Installation
### Requirements
In order to be able to work with this project you must have installed the following software:
- **Icy**, version 1.9.5.1 or above. ( [Available here](http://icy.bioimageanalysis.org) )
- The following plugins should be already installed in order to use the SURF project:
- EzPlug SDK
- Protocols SDK
- **Eclipse**, version _Neon_ or above. Make sure to have the _Buildship_ plugin installed. ([Available here](http://www.eclipse.org/downloads/))
- **Icy4Eclipse** plugin for Eclipse, the latest version available. Follow [these](http://icy.bioimageanalysis.org/index.php?display=startDevWithIcy) instructions.
### Setup
1. Use your *Git* repository manager of preference to download this repository (even Eclipse can do this). The repository URL is [https://gitlab.pasteur.fr/bia/org.bioimageanalysis.icy.surf.git](https://gitlab.pasteur.fr/bia/org.bioimageanalysis.icy.surf.git).
2. Make sure the environment variable **ICY_HOME** is set to the location of your Icy installation. _**Note**: This could be tricky on Mac so make sure to follow [these](https://stackoverflow.com/questions/829749/launch-mac-eclipse-with-environment-variables-set) instructions._
3. Open Eclipse and select the menu *File > Import...* Then select *Gradle > Existing Gradle Project*. Click *Next* the project root directory is demanded select the folder **org.bioimageanalysis.icy.surf** inside the folder you downloaded the at. Finally, click *Finish* to create the project in eclipse.
Eclipse will download the dependencies specified in the *gradle.build* file. When it finishes you should see the project without any problem on the project explorer of Eclipse. *If this is not the case, check that the environment variable ICY_HOME is correctly defined.*
\ No newline at end of file
/.settings/
/.gradle/
/bin/
/build/
.classpath
.project
println "ICY_HOME=${System.env.ICY_HOME}"
apply plugin: 'java-library'
apply plugin: 'eclipse'
version = '1.0.0'
configurations {
extraLibs // configuration that holds jars to include in the jar
}
// In this section you declare where to find the dependencies of your project
repositories {
jcenter()
}
dependencies {
extraLibs 'org.apache.commons:commons-math3:3.0' // Apache Math
configurations.implementation.extendsFrom(configurations.extraLibs)
implementation files("${System.env.ICY_HOME}/icy.jar") // Icy
implementation files("${System.env.ICY_HOME}/plugins/adufour/ezplug/EzPlug.jar") // EzPlug
implementation files("${System.env.ICY_HOME}/plugins/adufour/blocks/Blocks.jar") // Blocks
implementation files("${System.env.ICY_HOME}/plugins/adufour/protocols/Protocols.jar") // Protocols
implementation files("${System.env.ICY_HOME}/lib/bioformats.jar") // Bioformats
implementation files("${System.env.ICY_HOME}/lib/jai_core.jar") // JAI
testImplementation 'junit:junit:4.12'
testImplementation 'org.hamcrest:hamcrest-all:1.3' // Hamcrest
}
eclipse {
classpath.downloadJavadoc = true
project {
natures += ['icy.icy4eclipse.core.icynature']
}
}
jar {
from {
configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) }
}
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.output
from sourceSets.main.java
from configurations.extraLibs.collect { it.isDirectory() ? it : zipTree(it) }
}
artifacts {
archives sourcesJar
}
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.3-bin.zip
#!/usr/bin/env sh
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn () {
echo "$*"
}
die () {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Escape application args
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=$(save "$@")
# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi
exec "$JAVACMD" "$@"
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windows variants
if not "%OS%" == "Windows_NT" goto win9xME_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
/*
* This settings file was generated by the Gradle 'init' task.
*
* The settings file is used to specify which projects to include in your build.
* In a single project build this file can be empty or even removed.
*
* Detailed information about configuring a multi-project build in Gradle can be found
* in the user guide at https://docs.gradle.org/4.3/userguide/multi_project_builds.html
*/
/*
// To declare projects as part of a multi-project build use the 'include' method
include 'shared'
include 'api'
include 'services:webservice'
*/
rootProject.name = 'org.bioimageanalysis.icy.surf'
package org.bioimageanalysis.icy.surf;
public class KeyPoint {
private double x, y;
private double scale;
private double orientation;
private boolean laplacianSignPositive;
public KeyPoint(double x, double y, double scale, double orientation, boolean laplacianSign) {
this.x = x;
this.y = y;
this.scale = scale;
this.orientation = orientation;
this.laplacianSignPositive = laplacianSign;
}
public KeyPoint(KeyPoint keyPoint) {
this.x = keyPoint.getX();
this.y = keyPoint.getY();
this.scale = keyPoint.getScale();
this.orientation = keyPoint.getOrientation();
this.laplacianSignPositive = keyPoint.isLaplacianSignPositive();
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getScale() {
return scale;
}
public double getOrientation() {
return orientation;
}
public boolean isLaplacianSignPositive() {
return laplacianSignPositive;
}
@Override
public String toString() {
return String.format("Key point [x=%f, y=%f, scale=%f, orientation=%f, laplacianSignPositive=%b]", x, y, scale, orientation,
laplacianSignPositive);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (laplacianSignPositive ? 1231 : 1237);
long temp;
temp = Double.doubleToLongBits(orientation);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(scale);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof KeyPoint)) {
return false;
}
KeyPoint other = (KeyPoint) obj;
if (laplacianSignPositive != other.laplacianSignPositive) {
return false;
}
if (Double.doubleToLongBits(orientation) != Double.doubleToLongBits(other.orientation)) {
return false;
}
if (Double.doubleToLongBits(scale) != Double.doubleToLongBits(other.scale)) {
return false;
}
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x)) {
return false;
}
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y)) {
return false;
}
return true;
}
}
package org.bioimageanalysis.icy.surf;
import java.util.ArrayList;
import java.util.List;
import org.bioimageanalysis.icy.surf.image.ArrayImageIntegrator;
public class KeyPointDescriptor {
private static final int DESCRIPTOR_SECTOR_SIZE_1D = 4;
private static final int DESCRIPTOR_SECTOR_GRID_SIZE_2D = DESCRIPTOR_SECTOR_SIZE_1D * DESCRIPTOR_SECTOR_SIZE_1D;
public static List<KeyPointDescriptor> createDescriptors(ArrayImageIntegrator integrator,
List<KeyPoint> keyPoints) {
List<KeyPointDescriptor> descriptors = new ArrayList<>(keyPoints.size());
for (KeyPoint keyPoint : keyPoints) {
descriptors.add(createKeyPointDescriptor(integrator, keyPoint));
}
return descriptors;
}
private static KeyPointDescriptor createKeyPointDescriptor(ArrayImageIntegrator integrator, KeyPoint keyPoint) {
KeyPointDescriptor descriptor = new KeyPointDescriptor();
// Divide in a 4x4 zone the space around the interest point
// First compute the orientation.
double cosP = Math.cos(keyPoint.getOrientation());
double sinP = Math.sin(keyPoint.getOrientation());
double norm = 0;
// Divide in 16 sectors the space around the interest point.
for (int i = 0; i < DESCRIPTOR_SECTOR_SIZE_1D; i++) {
for (int j = 0; j < DESCRIPTOR_SECTOR_SIZE_1D; j++) {
computeSectorDescriptorInDescriptor(integrator, keyPoint, cosP, sinP, descriptor, i, j);
// Compute the norm of the vector
norm += computeSectorDescriptorsNorm(descriptor, i, j);
}
}
// Normalization of the descriptors in order to improve invariance to
// contrast change and whitening the descriptors.
norm = Math.sqrt(norm);
if (norm != 0)
normalizeSectorDescriptorsBy(descriptor, norm);
descriptor.setKeyPoint(keyPoint);
return descriptor;
}
private static void computeSectorDescriptorInDescriptor(ArrayImageIntegrator integrator, KeyPoint keyPoint,
double cosP, double sinP, KeyPointDescriptor descriptor, int i, int j) {
double u, v, gauss, responseU, responseV, responseX, responseY;
descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).setSumDx(0);
descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).setSumAbsDx(0);
descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).setSumDy(0);
descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).setSumAbsDy(0);
// Then each 4x4 is subsampled into a 5x5 zone
for (int k = 0; k < 5; k++) {
for (int l = 0; l < 5; l++) {
// We pre compute Haar answers
u = (keyPoint.getX() + keyPoint.getScale() * (cosP * ((i - 2) * 5 + k + 0.5) - sinP * ((j - 2) * 5 + l + 0.5)));
v = (keyPoint.getY() + keyPoint.getScale() * (sinP * ((i - 2) * 5 + k + 0.5) + cosP * ((j - 2) * 5 + l + 0.5)));
responseX = integrator.getHaarX((int) u, (int) v, (int) Math.round(keyPoint.getScale()));
// (u,v) are already translated of 0.5, which means
// that there is no round-off to perform: one takes
// the integer part of the coordinates.
responseY = integrator.getHaarY((int) u, (int) v, (int) Math.round(keyPoint.getScale()));
// Gaussian weight
gauss = getGaussian(((i - 2) * 5 + k + 0.5), ((j - 2) * 5 + l + 0.5), 3.3);
// Rotation of the axis
// responseU = gauss*( -responseX*sinP + responseY*cosP);
// responseV = gauss*(responseX*cosP + responseY*sinP);
responseU = gauss * (responseX * cosP + responseY * sinP);
responseV = gauss * (-responseX * sinP + responseY * cosP);
// The descriptors.
descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).addToSumDx(responseU);
descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).addToSumAbsDx(Math.abs(responseU));
descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).addToSumDy(responseV);
descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).addToSumAbsDy(Math.abs(responseV));
}
}
}
public static double getGaussian(double x, double y, double sigma) {
return 1 / (2 * Math.PI * sigma * sigma) * Math.exp(-(x * x + y * y) / (2 * sigma * sigma));
}
private static double computeSectorDescriptorsNorm(KeyPointDescriptor descriptor, int i, int j) {
return descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).getSumAbsDx()
* descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).getSumAbsDx()
+ descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).getSumAbsDy()
* descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).getSumAbsDy()
+ descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).getSumDx()
* descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).getSumDx()
+ descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).getSumDy()
* descriptor.getSectorDescriptor(DESCRIPTOR_SECTOR_SIZE_1D * i + j).getSumDy();
}
private static void normalizeSectorDescriptorsBy(KeyPointDescriptor descriptor, double norm) {
for (int i = 0; i < descriptor.getSectorDescriptors().length; i++) {
(descriptor.getSectorDescriptors()[i]).divideSumDxBy(norm);
(descriptor.getSectorDescriptors()[i]).divideSumAbsDxBy(norm);
(descriptor.getSectorDescriptors()[i]).divideSumDyBy(norm);
(descriptor.getSectorDescriptors()[i]).divideSumAbsDyBy(norm);
}
}
private KeyPoint keyPoint;
private KeyPointSectorDescriptor[] sectorDescriptors;
public KeyPointDescriptor() {
this.sectorDescriptors = new KeyPointSectorDescriptor[DESCRIPTOR_SECTOR_GRID_SIZE_2D];
for (int i = 0; i < sectorDescriptors.length; i++) {
sectorDescriptors[i] = new KeyPointSectorDescriptor();
}
}
private void setKeyPoint(KeyPoint keyPoint) {
this.keyPoint = keyPoint;
}
public KeyPoint getKeyPoint() {
return keyPoint;
}
private KeyPointSectorDescriptor[] getSectorDescriptors() {
return sectorDescriptors;
}
public KeyPointSectorDescriptor getSectorDescriptor(int index) {
return sectorDescriptors[index];
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("Descriptor[%s]", keyPoint);
}
}
package org.bioimageanalysis.icy.surf;
public class KeyPointSectorDescriptor {
private double sumDx;
private double sumDy;
private double sumAbsDx;
private double sumAbsDy;
public KeyPointSectorDescriptor() {}
public KeyPointSectorDescriptor(double sumDx, double sumDy, double sumAbsDx, double sumAbsDy) {
this.sumDx = sumDx;
this.sumDy = sumDy;
this.sumAbsDx = sumAbsDx;
this.sumAbsDy = sumAbsDy;
}
public double getSumDx() {
return sumDx;
}
public void setSumDx(double sumDx) {
this.sumDx = sumDx;
}
public void addToSumDx(double value) {
sumDx += value;
}
public void divideSumDxBy(double value) {
sumDx /= value;
}
public double getSumDy() {
return sumDy;
}
public void setSumDy(double sumDy) {
this.sumDy = sumDy;
}
public void addToSumDy(double value) {
sumDy += value;
}
public void divideSumDyBy(double value) {
sumDy /= value;
}
public double getSumAbsDx() {
return sumAbsDx;
}
public void setSumAbsDx(double sumAbsDx) {
this.sumAbsDx = sumAbsDx;
}
public void addToSumAbsDx(double value) {
sumAbsDx += value;
}
public void divideSumAbsDxBy(double value) {
sumAbsDx /= value;
}
public double getSumAbsDy() {
return sumAbsDy;
}
public void setSumAbsDy(double sumAbsDy) {
this.sumAbsDy = sumAbsDy;
}
public void addToSumAbsDy(double value) {
sumAbsDy += value;
}
public void divideSumAbsDyBy(double value) {
sumAbsDy /= value;
}
}
package org.bioimageanalysis.icy.surf.image;
public class ArrayImage {
private int width;
private int height;
private double[][] dataArray;
public ArrayImage(int width, int height) {
this.width = width;
this.height = height;
this.dataArray = new double[height][width];
}
public double[][] getDataArray() {
return dataArray;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public double getValueAt(int x, int y) {
return dataArray[y][x];
}
public void setValue(int x, int y, double value) {
getDataArray()[y][x] = value;
}
}
package org.bioimageanalysis.icy.surf.image;
import icy.image.IcyBufferedImage;
import icy.type.DataType;
public abstract class ArrayImageHelper {
/**
* @param bufferedImage
* Single channel BufferedImage.
* @return Single channel image.
*/
public static ArrayImage getArrayImageFrom(IcyBufferedImage bufferedImage) {
if (bufferedImage.getSizeC() > 1)
throw new IllegalArgumentException("Buffered image has more than 1 channels.");
ArrayImage image = new ArrayImage(bufferedImage.getWidth(), bufferedImage.getHeight());
double[] xyDataArray = bufferedImage.getDataXYAsDouble(0);
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
image.setValue(x, y, xyDataArray[y * bufferedImage.getWidth() + x]);
}
}
return image;
}
/**
* @param bufferedImage
* Multi-channel BufferedImage.
* @return Array of single channel images. One ArrayImage per buffered image
* channel/
*/
public static ArrayImage[] getArrayImagesFrom(IcyBufferedImage bufferedImage) {
ArrayImage[] images = new ArrayImage[bufferedImage.getSizeC()];
for (int c = 0; c < bufferedImage.getSizeC(); c++) {
images[c] = new ArrayImage(bufferedImage.getWidth(), bufferedImage.getHeight());
double[] xyDataArray = bufferedImage.getDataXYAsDouble(c);
for (int x = 0; x < images[c].getWidth(); x++) {
for (int y = 0; y < images[c].getHeight(); y++) {
images[c].setValue(x, y, xyDataArray[y * bufferedImage.getWidth() + x]);
}
}
}
return images;
}
/**
* @param image
* Array image to convert.
* @return Converted image.
*/
public static IcyBufferedImage getBufferedImageFrom(ArrayImage image) {
IcyBufferedImage bImage = new IcyBufferedImage(image.getWidth(), image.getHeight(), 1, DataType.DOUBLE);
bImage.beginUpdate();
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
bImage.setData(x, y, 0, image.getValueAt(x, y));
}
}
bImage.endUpdate();
return bImage;
}
/**
* @param imageArray
* Array of ArrayImages to convert.
* @return Single converted image.
*/
public static IcyBufferedImage getBufferedImageFrom(ArrayImage[] imageArray) {
IcyBufferedImage bImage = new IcyBufferedImage(imageArray[0].getWidth(), imageArray[0].getHeight(),
imageArray.length, DataType.DOUBLE);
bImage.beginUpdate();
for (int c = 0; c < imageArray.length; c++) {
for (int x = 0; x < imageArray[0].getWidth(); x++) {
for (int y = 0; y < imageArray[0].getHeight(); y++) {
bImage.setData(x, y, c, imageArray[c].getValueAt(x, y));
}
}
}
bImage.endUpdate();
return bImage;
}
public static int getImageWidthForSamplingStep(IcyBufferedImage image, int currentSamplingStep) {
return image.getWidth()/currentSamplingStep;
}
public static int getImageHeightForSamplingStep(IcyBufferedImage image, int currentSamplingStep) {
return image.getHeight()/currentSamplingStep;
}
}
package org.bioimageanalysis.icy.surf.image;
import com.google.common.util.concurrent.AtomicDouble;
public class ArrayImageIntegrator {
private ArrayImage targetImage;
private int padding = -1;
private ArrayImage paddedImage;
private int paddedWidth;
private int paddedHeight;
private ArrayImage integralImage;
public ArrayImageIntegrator() {
}
public int getPadding() {
return padding;
}
public void setPaddingSize(int padding) {
this.padding = padding;
}
private ArrayImage getTargetImage() {
return targetImage;
}
public void setTargetImage(ArrayImage image) {
this.targetImage = image;
}
private void unsetTargetImage() {
this.targetImage = null;
}
public synchronized void compute() {
if (getTargetImage() == null)
throw new RuntimeException("No target image specified");
if (getPadding() < 0)
throw new RuntimeException("No padding specified");
ArrayImage paddedImage = padImage(getTargetImage(), getPadding());
unsetTargetImage();
setPaddedImage(paddedImage);
integralImage = new ArrayImage(paddedWidth, paddedHeight);
computeIntegral();
unsetPaddedImage();
}
private ArrayImage padImage(ArrayImage image, int padding) {
ArrayImagePadder padder = new ArrayImagePadder();
padder.setTargetImage(image);
padder.setPaddingSize(padding);
padder.compute();
return padder.getPaddedImage();
}
private void setPaddedImage(ArrayImage paddedImage) {
this.paddedImage = paddedImage;
this.paddedWidth = paddedImage.getWidth();
this.paddedHeight = paddedImage.getHeight();
}
private void unsetPaddedImage() {
this.paddedImage = null;
}
private ArrayImage getPaddedImage() {
return this.paddedImage;
}
private void computeIntegral() {
integralImage.getDataArray()[0][0] = 0;
for (int x = 1; x < paddedWidth; x++) {
computeIntegralFirstRowPixelValues(x);
}
for (int y = 1; y < paddedHeight; y++) {
computeLineIntegralPixelValues(y);
}
}
private void computeIntegralFirstRowPixelValues(int x) {
integralImage.setValue(x, 0, integralImage.getValueAt(x - 1, 0) + getPaddedImage().getValueAt(x, 0));
}
private void computeLineIntegralPixelValues(int y) {
AtomicDouble lineSum = new AtomicDouble();
for (int x = 0; x < paddedWidth; x++) {
computeIntegralPixelValues(x, y, lineSum);
}
}
private void computeIntegralPixelValues(int x, int y, AtomicDouble lineSum) {
lineSum.addAndGet(getPaddedImage().getDataArray()[y][x]);
integralImage.setValue(x, y, lineSum.get() + integralImage.getValueAt(x, y - 1));
}
public ArrayImage getIntegralImage() {
return this.integralImage;
}
public double getSquareConvolutionXYAt(int x, int y, int boundX0, int boundX1, int boundY0, int boundY1) {
int a1 = x - boundX0;
int a2 = y - boundX1;
int b1 = a1 - boundY0;
int b2 = a2 - boundY1;
a1 += padding;
a2 += padding;
b1 += padding;
b2 += padding;
double result = 0;
try {
result += integralImage.getValueAt(b1, b2);
} catch (ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException(String.format("Cannot convolve at (%d, %d)", b1, b2));
}
try {
result += integralImage.getValueAt(a1, a2);
} catch (ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException(String.format("Cannot convolve at (%d, %d)", a1, a2));
}
try {
result -= integralImage.getValueAt(b1, a2);
} catch (ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException(String.format("Cannot convolve at (%d, %d)", b1, a2));
}
try {
result -= integralImage.getValueAt(a1, b2);
} catch (ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException(String.format("Cannot convolve at (%d, %d)", a1, b2));
}
return result;
}
public double getHaarX(int x, int y, int lambda) {
double result = -getSquareConvolutionXYAt(x, y, 1, -lambda - 1, -lambda - 1, 2 * lambda + 1);
result -= getSquareConvolutionXYAt(x, y, 0, -lambda - 1, lambda + 1, 2 * lambda + 1);
return result;
}
public double getHaarY(int x, int y, int lambda) {
double result = 0;
result -= getSquareConvolutionXYAt(x, y, -lambda - 1, 1, 2 * lambda + 1, -lambda - 1);
result -= getSquareConvolutionXYAt(x, y, -lambda - 1, 0, 2 * lambda + 1, lambda + 1);
return result;
}
}
package org.bioimageanalysis.icy.surf.image;
public class ArrayImagePadder {
private int targetWidth;
private int targetHeight;
private ArrayImage targetImage;
private int padding;
private int paddedWidth;
private int paddedHeight;
private ArrayImage paddedImage;
public ArrayImagePadder() {
}
public ArrayImage getTargetImage() {
return targetImage;
}
public void setTargetImage(ArrayImage image) {
this.targetImage = image;
this.targetWidth = targetImage.getWidth();
this.targetHeight = targetImage.getHeight();
}
private void unsetTargetImage() {
this.targetImage = null;
this.targetWidth = -1;
this.targetHeight = -1;
}
public int getPaddingSize() {
return this.padding;
}
public void setPaddingSize(int padding) {
this.padding = padding;
}
private void unsetPaddingSize() {
this.padding = -1;
}
public synchronized void compute() {
if (getTargetImage() == null)
throw new RuntimeException("No target image specified");
if (getPaddingSize() < 0)
throw new RuntimeException("No padding size specified");
paddedWidth = targetWidth + 2 * padding;
paddedHeight = targetHeight + 2 * padding;
paddedImage = new ArrayImage(paddedWidth, paddedHeight);
fillPaddedImage();
unsetTargetImage();
unsetPaddingSize();
}
private void fillPaddedImage() {
int targetYPosition;
for (int paddedYPosition = 0; paddedYPosition < paddedHeight; paddedYPosition++) {
targetYPosition = getYPositionAtTargetImage(paddedYPosition);
fillPaddedImageLine(paddedYPosition, targetYPosition);
}
}
private int getYPositionAtTargetImage(int paddedYPosition) {
if (paddedYPosition < padding)
return getMirroredPositionAtTargetImage(padding - paddedYPosition, targetHeight);
else if (paddedYPosition < paddedHeight - padding)
return paddedYPosition - padding;
else
return getMirroredPositionAtTargetImage((targetHeight + paddedHeight - padding - 2) - paddedYPosition,
targetHeight);
}
private int getMirroredPositionAtTargetImage(int position, int length) {
if (position < 0)
return getMirroredPositionAtTargetImage(-position, length);
else if (position < length)
return position;
else
return getMirroredPositionAtTargetImage((2 * length) - 2 - position, length);
}
private void fillPaddedImageLine(int paddedYPosition, int targetYPosition) {
int targetXPosition;
for (int paddedXPosition = 0; paddedXPosition < paddedWidth; paddedXPosition++) {
targetXPosition = getXPositionAtTargetImage(paddedXPosition);
fillPaddedImagePixel(paddedXPosition, paddedYPosition, targetXPosition, targetYPosition);
}
}
private int getXPositionAtTargetImage(int paddedXPosition) {
if (paddedXPosition < padding)
return getMirroredPositionAtTargetImage(padding - paddedXPosition, targetWidth);
else if (paddedXPosition < paddedWidth - padding)
return paddedXPosition - padding;
else
return getMirroredPositionAtTargetImage((targetWidth + paddedWidth - padding - 2) - paddedXPosition, targetWidth);
}
private void fillPaddedImagePixel(int paddedXPosition, int paddedYPosition, int targetXPosition,
int targetYPosition) {
double value;
try {
value = getTargetImage().getValueAt(targetXPosition, targetYPosition);
} catch (ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException(
String.format("Invalid target image position (%d, %d)", targetXPosition, targetYPosition));
}
try {
paddedImage.setValue(paddedXPosition, paddedYPosition, value);
} catch (ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException(
String.format("Invalid padded image position (%d, %d)", paddedXPosition, paddedYPosition));
}
}
public ArrayImage getPaddedImage() {
return paddedImage;
}
}
package org.bioimageanalysis.icy.surf.image;
import icy.image.IcyBufferedImage;
public abstract class IcyBufferedImageHelper {
public static int getImageWidthForSamplingStep(IcyBufferedImage image, int currentSamplingStep) {
return image.getWidth()/currentSamplingStep;
}
public static int getImageHeightForSamplingStep(IcyBufferedImage image, int currentSamplingStep) {
return image.getHeight()/currentSamplingStep;
}
}
package org.bioimageanalysis.icy.surf.image;
import com.google.common.util.concurrent.AtomicDouble;
import icy.image.IcyBufferedImage;
public class IcyBufferedImageIntegrator {
private IcyBufferedImage targetImage;
private int targetSizeC;
private int padding = -1;
private IcyBufferedImage paddedImage;
private int paddedWidth;
private int paddedHeight;
private IcyBufferedImage integralImage;
public IcyBufferedImageIntegrator() {
}
public int getPadding() {
return padding;
}
public void setPaddingSize(int padding) {
this.padding = padding;
}
private IcyBufferedImage getTargetImage() {
return targetImage;
}
public void setTargetImage(IcyBufferedImage image) {
this.targetImage = image;
this.targetSizeC = image.getSizeC();
}
private void unsetTargetImage() {
this.targetImage = null;
}
public synchronized void compute() {
if (getTargetImage() == null)
throw new RuntimeException("No target image specified");
if (getPadding() < 0)
throw new RuntimeException("No padding specified");
IcyBufferedImage paddedImage = padImage(getTargetImage(), getPadding());
unsetTargetImage();
setPaddedImage(paddedImage);
integralImage = new IcyBufferedImage(paddedWidth, paddedHeight, targetSizeC, getPaddedImage().getDataType_());
integralImage.beginUpdate();
for (int c = 0; c < targetSizeC; c++) {
computeChannelIntegral(c);
}
integralImage.endUpdate();
unsetPaddedImage();
}
private IcyBufferedImage padImage(IcyBufferedImage image, int padding) {
IcyBufferedImagePadder padder = new IcyBufferedImagePadder();
padder.setTargetImage(image);
padder.setPaddingSize(padding);
padder.compute();
return padder.getPaddedImage();
}
private void setPaddedImage(IcyBufferedImage paddedImage) {
this.paddedImage = paddedImage;
this.paddedWidth = paddedImage.getWidth();
this.paddedHeight = paddedImage.getHeight();
}
private void unsetPaddedImage() {
this.paddedImage = null;
}
private IcyBufferedImage getPaddedImage() {
return this.paddedImage;
}
private void computeChannelIntegral(int c) {
integralImage.setData(0, 0, c, paddedImage.getDataAsDouble(0, 0, c));
for (int x = 1; x < paddedWidth; x++) {
computeIntegralFirstRowPixelValues(c, x);
}
for (int y = 1; y < paddedHeight; y++) {
computeLineIntegralPixelValues(c, y);
}
}
private void computeIntegralFirstRowPixelValues(int c, int x) {
integralImage.setData(x, 0, c, integralImage.getData(x - 1, 0, c) + getPaddedImage().getData(x, 0, c));
}
private void computeLineIntegralPixelValues(int c, int y) {
AtomicDouble lineSum = new AtomicDouble();
for (int x = 0; x < paddedWidth; x++) {
computeIntegralPixelValues(c, x, y, lineSum);
}
}
private void computeIntegralPixelValues(int c, int x, int y, AtomicDouble lineSum) {
lineSum.addAndGet(getPaddedImage().getData(x, y, c));
integralImage.setData(x, y, c, lineSum.get() + integralImage.getData(x, y - 1, c));
}
public IcyBufferedImage getIntegralImage() {
return this.integralImage;
}
public double getSquareConvolutionXYAt(int x, int y, int c, int boundX0, int boundX1, int boundY0, int boundY1) {
int a1 = x - boundX0;
int a2 = y - boundX1;
int b1 = a1 - boundY0;
int b2 = a2 - boundY1;
a1 += padding;
a2 += padding;
b1 += padding;
b2 += padding;
double result = 0;
try {
result += integralImage.getData(b1, b2, c);
} catch (ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException(String.format("Cannot convolve at (%d, %d) channel %d", b1, b2, c));
}
try {
result += integralImage.getData(a1, a2, c);
} catch (ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException(String.format("Cannot convolve at (%d, %d) channel %d", a1, a2, c));
}
try {
result -= integralImage.getData(b1, a2, c);
} catch (ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException(String.format("Cannot convolve at (%d, %d) channel %d", b1, a2, c));
}
try {
result -= integralImage.getData(a1, b2, c);
} catch (ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException(String.format("Cannot convolve at (%d, %d) channel %d", a1, b2, c));
}
return result;
}
public double getHaarX(int x, int y, int c, int lambda) {
double result = -getSquareConvolutionXYAt(x, y, c, 1, -lambda - 1, -lambda - 1, 2 * lambda + 1);
result -= getSquareConvolutionXYAt(x, y, c, 0, -lambda - 1, lambda + 1, 2 * lambda + 1);
return result;
}
public double getHaarY(int x, int y, int c, int lambda) {
double result = 0;
result -= getSquareConvolutionXYAt(x, y, c, -lambda - 1, 1, 2 * lambda + 1, -lambda - 1);
result -= getSquareConvolutionXYAt(x, y, c, -lambda - 1, 0, 2 * lambda + 1, lambda + 1);
return result;
}
}
package org.bioimageanalysis.icy.surf.image;
import icy.image.IcyBufferedImage;
public class IcyBufferedImagePadder {
private int targetWidth;
private int targetHeight;
private int targetSizeC;
private IcyBufferedImage targetImage;
private int padding;
private int paddedWidth;
private int paddedHeight;
private IcyBufferedImage paddedImage;
public IcyBufferedImagePadder() {}
public IcyBufferedImage getTargetImage() {
return targetImage;
}
public void setTargetImage(IcyBufferedImage image) {
this.targetImage = image;
this.targetWidth = targetImage.getWidth();
this.targetHeight = targetImage.getHeight();
this.targetSizeC = targetImage.getSizeC();
}
private void unsetTargetImage() {
this.targetImage = null;
this.targetWidth = -1;
this.targetHeight = -1;
this.targetSizeC = -1;
}
public int getPaddingSize() {
return this.padding;
}
public void setPaddingSize(int padding) {
this.padding = padding;
}
private void unsetPaddingSize() {
this.padding = -1;
}
public synchronized void compute() {
if (getTargetImage() == null) throw new RuntimeException("No target image specified");
if (getPaddingSize() < 0) throw new RuntimeException("No padding size specified");
paddedWidth = targetWidth + 2 * padding;
paddedHeight = targetHeight + 2 * padding;
paddedImage = new IcyBufferedImage(paddedWidth, paddedHeight, targetSizeC, getTargetImage().getDataType_());
paddedImage.beginUpdate();
for (int c = 0; c < targetSizeC; c++) {
fillPaddedImageChannel(c);
}
paddedImage.endUpdate();
unsetTargetImage();
unsetPaddingSize();
}
private void fillPaddedImageChannel(int c) {
int targetYPosition;
for (int paddedYPosition = 0; paddedYPosition < paddedHeight; paddedYPosition++) {
targetYPosition = getYPositionAtTargetImage(paddedYPosition);
fillPaddedImageLine(c, paddedYPosition, targetYPosition);
}
}
private int getYPositionAtTargetImage(int paddedYPosition) {
if (paddedYPosition < padding)
return getMirroredPositionAtTargetImage(padding - paddedYPosition, targetHeight);
else if (paddedYPosition < paddedHeight - padding)
return paddedYPosition - padding;
else
return getMirroredPositionAtTargetImage((targetHeight + paddedHeight - padding - 2) - paddedYPosition,
targetHeight);
}
private int getMirroredPositionAtTargetImage(int position, int length) {
if (position < 0)
return getMirroredPositionAtTargetImage(-position, length);
else if (position < length)
return position;
else
return getMirroredPositionAtTargetImage((2 * length) - 2 - position, length);
}
private void fillPaddedImageLine(int c, int paddedYPosition, int targetYPosition) {
int targetXPosition;
for (int paddedXPosition = 0; paddedXPosition < paddedWidth; paddedXPosition++) {
targetXPosition = getXPositionAtTargetImage(paddedXPosition);
fillPaddedImagePixel(c, paddedXPosition, paddedYPosition, targetXPosition, targetYPosition);
}
}
private int getXPositionAtTargetImage(int paddedXPosition) {
if (paddedXPosition < padding)
return getMirroredPositionAtTargetImage(padding - paddedXPosition, targetWidth);
else if (paddedXPosition < paddedWidth - padding)
return paddedXPosition - padding;
else
return getMirroredPositionAtTargetImage((targetWidth + paddedWidth - padding - 2) - paddedXPosition, targetWidth);
}
private void fillPaddedImagePixel(int c, int paddedXPosition, int paddedYPosition, int targetXPosition,
int targetYPosition) {
double value;
try {
value = getTargetImage().getData(targetXPosition, targetYPosition, c);
} catch (ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException(
String.format("Invalid target image position (%d, %d)", targetXPosition, targetYPosition));
}
try {
paddedImage.setData(paddedXPosition, paddedYPosition, c, value);
} catch (ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException(
String.format("Invalid padded image position (%d, %d)", paddedXPosition, paddedYPosition));
}
}
public IcyBufferedImage getPaddedImage() {
return paddedImage;
}
}
package org.bioimageanalysis.icy.surf.matching;
public class DescriptorMatch {
private double x1, y1;
private double x2, y2;
private double matchScore;
public double getX1() {
return x1;
}
public void setX1(double x1) {
this.x1 = x1;
}
public double getY1() {
return y1;
}
public void setY1(double y1) {
this.y1 = y1;
}
public double getX2() {
return x2;
}
public void setX2(double x2) {
this.x2 = x2;
}
public double getY2() {
return y2;
}
public void setY2(double y2) {
this.y2 = y2;
}
public double getMatchScore() {
return matchScore;
}
public void setMatchScore(double matchScore) {
this.matchScore = matchScore;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(x1);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(x2);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y1);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y2);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}