Skip to content

Commit

Permalink
Add kernel operation and add both kernel and morphology to code gener…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
ArchdukeTim committed Dec 28, 2017
1 parent c78b49c commit 72d9919
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 10 deletions.
20 changes: 10 additions & 10 deletions core/src/main/java/edu/wpi/grip/core/operations/CVOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import edu.wpi.grip.generated.opencv_imgproc.enumeration.ColorConversionCodesEnum;
import edu.wpi.grip.generated.opencv_imgproc.enumeration.ColormapTypesEnum;
import edu.wpi.grip.generated.opencv_imgproc.enumeration.InterpolationFlagsEnum;
import edu.wpi.grip.generated.opencv_imgproc.enumeration.MorphTypesEnum;
import edu.wpi.grip.generated.opencv_imgproc.enumeration.ThresholdTypesEnum;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -313,16 +314,16 @@ public class CVOperations {
"Performs advanced morphological transformations."),
templateFactory.create(
SocketHints.Inputs.createMatSocketHint("src", false),
SocketHints.createEnumSocketHint("op", MorphTypesEnum.MORPH_OPEN),
SocketHints.Inputs.createMatSocketHint("kernel", true),
SocketHints.createEnumSocketHint("op", CVMorphologyTypesEnum.MORPH_OPEN),
new SocketHint.Builder<>(Point.class).identifier("anchor").initialValueSupplier(
() -> new Point(-1, -1)).build(),
SocketHints.Inputs.createNumberSpinnerSocketHint("iterations", 1),
SocketHints.createEnumSocketHint("borderType", BorderTypesEnum.BORDER_CONSTANT),
new SocketHint.Builder<>(Scalar.class).identifier("borderValue")
.initialValueSupplier(opencv_imgproc::morphologyDefaultBorderValue).build(),
SocketHints.Outputs.createMatSocketHint("dst"),
(src, kernel, op, anchor, iterations, borderType, borderValue, dst) -> {
(src, op, kernel, anchor, iterations, borderType, borderValue, dst) -> {
opencv_imgproc.morphologyEx(src, dst, op.value, kernel, anchor,
iterations.intValue(), borderType.value, borderValue);
}
Expand Down Expand Up @@ -447,17 +448,16 @@ public enum CVBorderTypesEnum {
}
}

public enum CVMorphologyTypesEnum {
MORPH_OPEN(2),
MORPH_CLOSE(3),
MORPH_GRADIENT(4),
MORPH_TOPHAT(5),
MORPH_BLACKHAT(6),
MORPH_HITMISS(7);
public enum CVMorphTypesEnum {
MORPH_OPEN(MorphTypesEnum.MORPH_OPEN.value),
MORPH_CLOSE(MorphTypesEnum.MORPH_CLOSE.value),
MORPH_GRADIENT(MorphTypesEnum.MORPH_GRADIENT.value),
MORPH_TOPHAT(MorphTypesEnum.MORPH_TOPHAT.value),
MORPH_BLACKHAT(MorphTypesEnum.MORPH_BLACKHAT.value);

public final int value;

CVMorphologyTypesEnum(int value) {
CVMorphTypesEnum(int value) {
this.value = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package edu.wpi.grip.core.operations.opencv;


import edu.wpi.grip.core.Description;
import edu.wpi.grip.core.OperationDescription;
import edu.wpi.grip.core.sockets.InputSocket;
import edu.wpi.grip.core.sockets.OutputSocket;
import edu.wpi.grip.core.sockets.SocketHint;
import edu.wpi.grip.core.sockets.SocketHints;

import com.google.inject.Inject;

import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacpp.opencv_core.Size;
import org.bytedeco.javacpp.opencv_imgproc;
import org.python.google.common.collect.ImmutableList;

import java.util.List;

@Description(name = "New Kernel",
summary = "Create a kernel of custom size",
category = OperationDescription.Category.OPENCV,
iconName = "kernel")
public class NewKernelOperation implements CVOperation {

private final SocketHint<KernelEnum> typeHint = SocketHints.createEnumSocketHint("kernelType",
KernelEnum.MORPH_RECT);
private final SocketHint<Number> widthHint = SocketHints.Inputs
.createNumberSpinnerSocketHint("width", 1, 1, Integer.MAX_VALUE);
private final SocketHint<Number> heightHint = SocketHints.Inputs
.createNumberSpinnerSocketHint("height", 1, 1, Integer.MAX_VALUE);
private final SocketHint<Mat> outputHint = SocketHints.Outputs.createMatSocketHint("kernel");


private final InputSocket<Number> widthSocket;
private final InputSocket<Number> heightSocket;
private final InputSocket<KernelEnum> typeSocket;

private final OutputSocket<Mat> outputSocket;

@Inject
@SuppressWarnings("JavadocMethod")
public NewKernelOperation(InputSocket.Factory inputSocketFactory,
OutputSocket.Factory outputSocketFactory) {
this.typeSocket = inputSocketFactory.create(typeHint);
this.widthSocket = inputSocketFactory.create(widthHint);
this.heightSocket = inputSocketFactory.create(heightHint);
this.outputSocket = outputSocketFactory.create(outputHint);
}

@Override
public List<InputSocket> getInputSockets() {
return ImmutableList.of(
typeSocket,
widthSocket,
heightSocket
);
}

@Override
public List<OutputSocket> getOutputSockets() {
return ImmutableList.of(
outputSocket
);
}

@Override
public void perform() {
final int widthValue = widthSocket.getValue().get().intValue();
final int heightValue = heightSocket.getValue().get().intValue();
final int kernelType = typeSocket.getValue().get().value;

outputSocket.setValue(opencv_imgproc.getStructuringElement(kernelType, new Size(widthValue,
heightValue)));
}

public enum KernelEnum {
MORPH_RECT(0),
MORPH_CROSS(1),
MORPH_ELLIPSE(2);

public final int value;

KernelEnum(int value) {
this.value = value;
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Performs advanced morphology functions.
* @param src the Image to morph.
* @param op the morph operation
* @param kernel the kernel for morphing.
* @param anchor the center of the kernel.
* @param iterations the number of times to perform the morph.
* @param borderType pixel extrapolation method.
* @param borderValue value to be used for a constant border.
* @param dst Output Image.
*/
void $className::#func($step ["src", "op", "kernel", "anchor", "iterations", "borderType", "borderValue", "dst"]) {
cv::morphologyEx(src, dst, op, kernel, anchor, (int)iterations, borderType, borderValue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Creates kernel of given shape and size
* @param shape the kernels MorphShape.
* @param size the size of the kernel.
*/
void $className::#func($step ["shape", "size"]) {
return cv::getStructuringElement(shape, size));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Performs advanced morphology functions.
* @param src the Image to morph.
* @param op the operation to perform.
* @param kernel the kernel for morphing.
* @param anchor the center of the kernel.
* @param iterations the number of times to perform the morph.
* @param borderType pixel extrapolation method.
* @param borderValue value to be used for a constant border.
* @param dst Output Image.
*/
private void $tMeth.name($step.name())(Mat src, MorphType op, Mat kernel, Point anchor, double iterations,
int borderType, Scalar borderValue, Mat dst) {
if (kernel == null) {
kernel = new Mat();
}
if (anchor == null) {
anchor = new Point(-1,-1);
}
if (borderValue == null) {
borderValue = new Scalar(-1);
}
Imgproc.morphologyEx(src, dst, op, kernel, anchor, (int)iterations, borderType, borderValue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Creates a kernel of given shape and size.
* @param shape the kernels MorphShape.
* @param size the size of the kernel.
*/
private void $tMeth.name($step.name())(Mat shape, Size size) {
Imgproc.getStructuringElement(shape, size);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@staticmethod
def $tMeth.name($step.name())(src, op, kernel, anchor, iterations, border_type, border_value):
"""Expands area of lower value in an image.
Args:
src: A numpy.ndarray.
kernel: The kernel for erosion. A numpy.ndarray.
iterations: the number of times to erode.
border_type: Opencv enum that represents a border type.
border_value: value to be used for a constant border.
Returns:
A numpy.ndarray after erosion.
"""
return cv2.morphologyEx(src, op, kernel, anchor, iterations = (int) (iterations +0.5),
borderType = border_type, borderValue = border_value)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@staticmethod
def $tMeth.name($step.name())(shape, width, height):
"""Creates kernel of given shape and size.
Args:
shape: The kernel MorphShape
size: Size of kernel as a tuple
Returns:
A numpy.ndarray representing the kernel.
"""
return cv2.getStructuringElement(shape, (int(width), int(height)))

0 comments on commit 72d9919

Please sign in to comment.