Skip to content

Commit dc2c411

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

File tree

8 files changed

+174
-10
lines changed

8 files changed

+174
-10
lines changed

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

Lines changed: 10 additions & 10 deletions
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
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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", KernelEnum.MORPH_RECT);
27+
private final SocketHint<Number> widthHint = SocketHints.Inputs
28+
.createNumberSpinnerSocketHint("width", 1, 1, Integer.MAX_VALUE);
29+
private final SocketHint<Number> heightHint = SocketHints.Inputs
30+
.createNumberSpinnerSocketHint("height", 1, 1, Integer.MAX_VALUE);
31+
private final SocketHint<Mat> outputHint = SocketHints.Outputs.createMatSocketHint("kernel");
32+
33+
34+
private final InputSocket<Number> widthSocket;
35+
private final InputSocket<Number> heightSocket;
36+
private final InputSocket<KernelEnum> typeSocket;
37+
38+
private final OutputSocket<Mat> outputSocket;
39+
40+
@Inject
41+
@SuppressWarnings("JavadocMethod")
42+
public NewKernelOperation(InputSocket.Factory inputSocketFactory,
43+
OutputSocket.Factory outputSocketFactory) {
44+
this.typeSocket = inputSocketFactory.create(typeHint);
45+
this.widthSocket = inputSocketFactory.create(widthHint);
46+
this.heightSocket = inputSocketFactory.create(heightHint);
47+
this.outputSocket = outputSocketFactory.create(outputHint);
48+
}
49+
50+
@Override
51+
public List<InputSocket> getInputSockets() {
52+
return ImmutableList.of(
53+
typeSocket,
54+
widthSocket,
55+
heightSocket
56+
);
57+
}
58+
59+
@Override
60+
public List<OutputSocket> getOutputSockets() {
61+
return ImmutableList.of(
62+
outputSocket
63+
);
64+
}
65+
66+
@Override
67+
public void perform() {
68+
final int widthValue = widthSocket.getValue().get().intValue();
69+
final int heightValue = heightSocket.getValue().get().intValue();
70+
final int kernelType = typeSocket.getValue().get().value;
71+
72+
outputSocket.setValue(opencv_imgproc.getStructuringElement(kernelType, new Size(widthValue, heightValue)));
73+
}
74+
public enum KernelEnum {
75+
MORPH_RECT(0),
76+
MORPH_CROSS(1),
77+
MORPH_ELLIPSE(2);
78+
79+
public final int value;
80+
81+
KernelEnum(int value) {
82+
this.value = value;
83+
}
84+
}
85+
}
86+
Lines changed: 14 additions & 0 deletions
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+
}
Lines changed: 8 additions & 0 deletions
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+
}
Lines changed: 24 additions & 0 deletions
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+
}
Lines changed: 8 additions & 0 deletions
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+
}
Lines changed: 14 additions & 0 deletions
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)
Lines changed: 10 additions & 0 deletions
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)