Skip to content

Commit 72d9919

Browse files
committed
Add kernel operation and add both kernel and morphology to code generation
1 parent c78b49c commit 72d9919

File tree

8 files changed

+177
-10
lines changed

8 files changed

+177
-10
lines changed

core/src/main/java/edu/wpi/grip/core/operations/CVOperations.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import edu.wpi.grip.generated.opencv_imgproc.enumeration.ColorConversionCodesEnum;
1818
import edu.wpi.grip.generated.opencv_imgproc.enumeration.ColormapTypesEnum;
1919
import edu.wpi.grip.generated.opencv_imgproc.enumeration.InterpolationFlagsEnum;
20+
import edu.wpi.grip.generated.opencv_imgproc.enumeration.MorphTypesEnum;
2021
import edu.wpi.grip.generated.opencv_imgproc.enumeration.ThresholdTypesEnum;
2122

2223
import com.google.common.annotations.VisibleForTesting;
@@ -313,16 +314,16 @@ public class CVOperations {
313314
"Performs advanced morphological transformations."),
314315
templateFactory.create(
315316
SocketHints.Inputs.createMatSocketHint("src", false),
317+
SocketHints.createEnumSocketHint("op", MorphTypesEnum.MORPH_OPEN),
316318
SocketHints.Inputs.createMatSocketHint("kernel", true),
317-
SocketHints.createEnumSocketHint("op", CVMorphologyTypesEnum.MORPH_OPEN),
318319
new SocketHint.Builder<>(Point.class).identifier("anchor").initialValueSupplier(
319320
() -> new Point(-1, -1)).build(),
320321
SocketHints.Inputs.createNumberSpinnerSocketHint("iterations", 1),
321322
SocketHints.createEnumSocketHint("borderType", BorderTypesEnum.BORDER_CONSTANT),
322323
new SocketHint.Builder<>(Scalar.class).identifier("borderValue")
323324
.initialValueSupplier(opencv_imgproc::morphologyDefaultBorderValue).build(),
324325
SocketHints.Outputs.createMatSocketHint("dst"),
325-
(src, kernel, op, anchor, iterations, borderType, borderValue, dst) -> {
326+
(src, op, kernel, anchor, iterations, borderType, borderValue, dst) -> {
326327
opencv_imgproc.morphologyEx(src, dst, op.value, kernel, anchor,
327328
iterations.intValue(), borderType.value, borderValue);
328329
}
@@ -447,17 +448,16 @@ public enum CVBorderTypesEnum {
447448
}
448449
}
449450

450-
public enum CVMorphologyTypesEnum {
451-
MORPH_OPEN(2),
452-
MORPH_CLOSE(3),
453-
MORPH_GRADIENT(4),
454-
MORPH_TOPHAT(5),
455-
MORPH_BLACKHAT(6),
456-
MORPH_HITMISS(7);
451+
public enum CVMorphTypesEnum {
452+
MORPH_OPEN(MorphTypesEnum.MORPH_OPEN.value),
453+
MORPH_CLOSE(MorphTypesEnum.MORPH_CLOSE.value),
454+
MORPH_GRADIENT(MorphTypesEnum.MORPH_GRADIENT.value),
455+
MORPH_TOPHAT(MorphTypesEnum.MORPH_TOPHAT.value),
456+
MORPH_BLACKHAT(MorphTypesEnum.MORPH_BLACKHAT.value);
457457

458458
public final int value;
459459

460-
CVMorphologyTypesEnum(int value) {
460+
CVMorphTypesEnum(int value) {
461461
this.value = value;
462462
}
463463
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package edu.wpi.grip.core.operations.opencv;
2+
3+
4+
import edu.wpi.grip.core.Description;
5+
import edu.wpi.grip.core.OperationDescription;
6+
import edu.wpi.grip.core.sockets.InputSocket;
7+
import edu.wpi.grip.core.sockets.OutputSocket;
8+
import edu.wpi.grip.core.sockets.SocketHint;
9+
import edu.wpi.grip.core.sockets.SocketHints;
10+
11+
import com.google.inject.Inject;
12+
13+
import org.bytedeco.javacpp.opencv_core.Mat;
14+
import org.bytedeco.javacpp.opencv_core.Size;
15+
import org.bytedeco.javacpp.opencv_imgproc;
16+
import org.python.google.common.collect.ImmutableList;
17+
18+
import java.util.List;
19+
20+
@Description(name = "New Kernel",
21+
summary = "Create a kernel of custom size",
22+
category = OperationDescription.Category.OPENCV,
23+
iconName = "kernel")
24+
public class NewKernelOperation implements CVOperation {
25+
26+
private final SocketHint<KernelEnum> typeHint = SocketHints.createEnumSocketHint("kernelType",
27+
KernelEnum.MORPH_RECT);
28+
private final SocketHint<Number> widthHint = SocketHints.Inputs
29+
.createNumberSpinnerSocketHint("width", 1, 1, Integer.MAX_VALUE);
30+
private final SocketHint<Number> heightHint = SocketHints.Inputs
31+
.createNumberSpinnerSocketHint("height", 1, 1, Integer.MAX_VALUE);
32+
private final SocketHint<Mat> outputHint = SocketHints.Outputs.createMatSocketHint("kernel");
33+
34+
35+
private final InputSocket<Number> widthSocket;
36+
private final InputSocket<Number> heightSocket;
37+
private final InputSocket<KernelEnum> typeSocket;
38+
39+
private final OutputSocket<Mat> outputSocket;
40+
41+
@Inject
42+
@SuppressWarnings("JavadocMethod")
43+
public NewKernelOperation(InputSocket.Factory inputSocketFactory,
44+
OutputSocket.Factory outputSocketFactory) {
45+
this.typeSocket = inputSocketFactory.create(typeHint);
46+
this.widthSocket = inputSocketFactory.create(widthHint);
47+
this.heightSocket = inputSocketFactory.create(heightHint);
48+
this.outputSocket = outputSocketFactory.create(outputHint);
49+
}
50+
51+
@Override
52+
public List<InputSocket> getInputSockets() {
53+
return ImmutableList.of(
54+
typeSocket,
55+
widthSocket,
56+
heightSocket
57+
);
58+
}
59+
60+
@Override
61+
public List<OutputSocket> getOutputSockets() {
62+
return ImmutableList.of(
63+
outputSocket
64+
);
65+
}
66+
67+
@Override
68+
public void perform() {
69+
final int widthValue = widthSocket.getValue().get().intValue();
70+
final int heightValue = heightSocket.getValue().get().intValue();
71+
final int kernelType = typeSocket.getValue().get().value;
72+
73+
outputSocket.setValue(opencv_imgproc.getStructuringElement(kernelType, new Size(widthValue,
74+
heightValue)));
75+
}
76+
77+
public enum KernelEnum {
78+
MORPH_RECT(0),
79+
MORPH_CROSS(1),
80+
MORPH_ELLIPSE(2);
81+
82+
public final int value;
83+
84+
KernelEnum(int value) {
85+
this.value = value;
86+
}
87+
}
88+
}
89+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Performs advanced morphology functions.
3+
* @param src the Image to morph.
4+
* @param op the morph operation
5+
* @param kernel the kernel for morphing.
6+
* @param anchor the center of the kernel.
7+
* @param iterations the number of times to perform the morph.
8+
* @param borderType pixel extrapolation method.
9+
* @param borderValue value to be used for a constant border.
10+
* @param dst Output Image.
11+
*/
12+
void $className::#func($step ["src", "op", "kernel", "anchor", "iterations", "borderType", "borderValue", "dst"]) {
13+
cv::morphologyEx(src, dst, op, kernel, anchor, (int)iterations, borderType, borderValue);
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Creates kernel of given shape and size
3+
* @param shape the kernels MorphShape.
4+
* @param size the size of the kernel.
5+
*/
6+
void $className::#func($step ["shape", "size"]) {
7+
return cv::getStructuringElement(shape, size));
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Performs advanced morphology functions.
3+
* @param src the Image to morph.
4+
* @param op the operation to perform.
5+
* @param kernel the kernel for morphing.
6+
* @param anchor the center of the kernel.
7+
* @param iterations the number of times to perform the morph.
8+
* @param borderType pixel extrapolation method.
9+
* @param borderValue value to be used for a constant border.
10+
* @param dst Output Image.
11+
*/
12+
private void $tMeth.name($step.name())(Mat src, MorphType op, Mat kernel, Point anchor, double iterations,
13+
int borderType, Scalar borderValue, Mat dst) {
14+
if (kernel == null) {
15+
kernel = new Mat();
16+
}
17+
if (anchor == null) {
18+
anchor = new Point(-1,-1);
19+
}
20+
if (borderValue == null) {
21+
borderValue = new Scalar(-1);
22+
}
23+
Imgproc.morphologyEx(src, dst, op, kernel, anchor, (int)iterations, borderType, borderValue);
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Creates a kernel of given shape and size.
3+
* @param shape the kernels MorphShape.
4+
* @param size the size of the kernel.
5+
*/
6+
private void $tMeth.name($step.name())(Mat shape, Size size) {
7+
Imgproc.getStructuringElement(shape, size);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@staticmethod
2+
def $tMeth.name($step.name())(src, op, kernel, anchor, iterations, border_type, border_value):
3+
"""Expands area of lower value in an image.
4+
Args:
5+
src: A numpy.ndarray.
6+
kernel: The kernel for erosion. A numpy.ndarray.
7+
iterations: the number of times to erode.
8+
border_type: Opencv enum that represents a border type.
9+
border_value: value to be used for a constant border.
10+
Returns:
11+
A numpy.ndarray after erosion.
12+
"""
13+
return cv2.morphologyEx(src, op, kernel, anchor, iterations = (int) (iterations +0.5),
14+
borderType = border_type, borderValue = border_value)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@staticmethod
2+
def $tMeth.name($step.name())(shape, width, height):
3+
"""Creates kernel of given shape and size.
4+
Args:
5+
shape: The kernel MorphShape
6+
size: Size of kernel as a tuple
7+
Returns:
8+
A numpy.ndarray representing the kernel.
9+
"""
10+
return cv2.getStructuringElement(shape, (int(width), int(height)))

0 commit comments

Comments
 (0)