public final class TLcdConvolveOp extends ALcdImageOperator
(dx, dy)
from the center pixel is weighted by the
weight at index (-dx,-dy)
in the kernel. In other words if the kernel is rotated by 180 degrees and placed on
top of the central pixel then each pixel lines up with it kernel weight. Note that the rotation has no effect in
case of a symmetric kernel.
For example the 2D convolution of a pixel with a kernel of 3x3:
double[] kernel = {+1, -2, +3,
-4, +5, -6,
+7, -8, +9};
// Each pixel is computed as follows:
out(column, row) = 0;
for (int dy=-1; dy<=1; dy++) {
for (int dx=-1; dx<=1; dx++) {
int index = 1-dx + (1-dy)*3; // where 3 is the width of the kernel (number of columns)
out(column, row)+=kernel[index]*in(column+dx, row+dy);
}
}
Note the use of -dx and -dy in the index computation, needed to perform the 180 degree rotation.
If one of the input pixels of the convolution kernel has a
"no data" value
, the
result of the kernel for the current pixel is also "no data".
This operator expects kernels with odd dimensions (e.g., 3x3). Otherwise an IllegalArgumentException
will be thrown.
// Using the static method:
ALcdImage inputImage = ...;
double[] kernel = new double[] {
-1.0f, -1.0f, -1.0f,
-1.0f, 9.0f, -1.0f,
-1.0f, -1.0f, -1.0f
};
ALcdImage outputImage = TLcdConvolveOp.convolve(inputImage, kernel, 3, 3);
// Using a data object:
ALcdImage inputImage = ...;
double[] kernel = new double[] {
-1.0f, -1.0f, -1.0f,
-1.0f, 9.0f, -1.0f,
-1.0f, -1.0f, -1.0f
};
TLcdConvolveOp op = new TLcdConvolveOp();
ILcdDataObject params = op.getParameterDataType().newInstance();
params.setValue(TLcdConvolveOp.INPUT_IMAGE, inputImage);
params.setValue(TLcdConvolveOp.KERNEL, kernel);
params.setValue(TLcdConvolveOp.KERNEL_WIDTH, 3);
params.setValue(TLcdConvolveOp.KERNEL_HEIGHT, 3);
outputImage = op.apply(params);
ALcdImageOperator.ImageOperatorTypeBuilder
Modifier and Type | Field and Description |
---|---|
static TLcdDataType |
CONVOLVE_FILTER_TYPE
Input data type of the operator.
|
static TLcdDataProperty |
INPUT_IMAGE
The input image.
|
static TLcdDataProperty |
KERNEL
The convolution kernel in row-major order.
|
static TLcdDataProperty |
KERNEL_HEIGHT
Height of the convolution kernel.
|
static TLcdDataProperty |
KERNEL_WIDTH
Width of the convolution kernel.
|
static String |
NAME
Name of the operator.
|
INPUT_IMAGE_NAME
Constructor and Description |
---|
TLcdConvolveOp()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
ALcdImage |
apply(ILcdDataObject aParameters)
Applies this operator to the given input parameters.
|
static ALcdImage |
convolve(ALcdImage aImage,
double[] aKernel,
int aKernelWidth,
int aKernelHeight)
Applies a convolution kernel to the given image.
|
createTypeBuilder, createTypeBuilder, equals, getParameterDataType, hashCode, toString
public static final String NAME
public static final TLcdDataProperty INPUT_IMAGE
public static final TLcdDataProperty KERNEL
public static final TLcdDataProperty KERNEL_WIDTH
public static final TLcdDataProperty KERNEL_HEIGHT
public static final TLcdDataType CONVOLVE_FILTER_TYPE
public ALcdImage apply(ILcdDataObject aParameters)
ALcdImageOperator
apply
in class ALcdImageOperator
aParameters
- the parameters for the operatorpublic static ALcdImage convolve(ALcdImage aImage, double[] aKernel, int aKernelWidth, int aKernelHeight)
aKernelWidth
x aKernelHeight
.aImage
- the image to be convolvedaKernel
- the convolution kernel, in row-major orderaKernelWidth
- the width of the kernelaKernelHeight
- the height of the kernel