-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add morphology operation #874
Open
ArchdukeTim
wants to merge
5
commits into
WPIRoboticsProjects:master
Choose a base branch
from
ArchdukeTim:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+222
−15
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c78b49c
Add morphology operation
ArchdukeTim 72d9919
Add kernel operation and add both kernel and morphology to code gener…
ArchdukeTim 5bbadde
Don't reallocate Point, Size, or Kernel
ArchdukeTim bd1c4f4
Add morph function to python code
ArchdukeTim 5cfc0ea
Fix python generated code format
ArchdukeTim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
core/src/main/java/edu/wpi/grip/core/operations/opencv/NewKernelOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
14 changes: 14 additions & 0 deletions
14
ui/src/main/resources/edu/wpi/grip/ui/codegeneration/cpp/operations/CV_morphologyEx.vm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
8 changes: 8 additions & 0 deletions
8
ui/src/main/resources/edu/wpi/grip/ui/codegeneration/cpp/operations/New_Kernel.vm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
24 changes: 24 additions & 0 deletions
24
ui/src/main/resources/edu/wpi/grip/ui/codegeneration/java/operations/CV_morphologyEx.vm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
8 changes: 8 additions & 0 deletions
8
ui/src/main/resources/edu/wpi/grip/ui/codegeneration/java/operations/New_Kernel.vm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
14 changes: 14 additions & 0 deletions
14
ui/src/main/resources/edu/wpi/grip/ui/codegeneration/python/operations/CV_morphologyEx.vm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
@staticmethod | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested this and it works |
||
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) |
10 changes: 10 additions & 0 deletions
10
ui/src/main/resources/edu/wpi/grip/ui/codegeneration/python/operations/New_Kernel.vm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this allocate a new matrix every time this perform method is called.
Don't you instead want to pass in an existing matrix so the memory can be reused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally you'd create the kernel once with all the variables, but idk how to get that behavior.
Do you mean the Size matrix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be an overload that takes a
dest
as a parameter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand. Is there an example in the code I can copy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately no.
What we are trying to avoid is allocating new memory on every call.
So there should be an alternative method called
getStructuringElement
that takes an additional arg calleddest
. You can pass a pre-allocated matrix so we aren't allocating more memory on every call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's december of the next year, which means it's my turn to comment 😂
Going back and looking through this, I understand what we are going for, but not sure how that functionality is created in GRIP. The kernel creating isn't part of the pipeline, just the setup, although I think i'm combining the code generation with the actual application..lemme see what I can come up with for the application part and then I'll worry about code gen later