Skip to content
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 convexity and inertia ratio to findBlobs #929

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public class FindBlobsOperation implements Operation {
.createNumberListRangeSocketHint("Circularity", 0.0, 1.0);
private final SocketHint<Boolean> colorHint = SocketHints
.createBooleanSocketHint("Dark Blobs", false);
private final SocketHint<List<Number>> convexityHint = SocketHints.Inputs
.createNumberListRangeSocketHint("Convexity", 0.0, 1.0);
private final SocketHint<List<Number>> inertiaRatioHint = SocketHints.Inputs
.createNumberListRangeSocketHint("Inertia Ratio", 0.0, 1.0);

private final SocketHint<BlobsReport> blobsHint = new SocketHint.Builder<>(BlobsReport.class)
.identifier("Blobs")
Expand All @@ -46,6 +50,8 @@ public class FindBlobsOperation implements Operation {
private final InputSocket<Number> minAreaSocket;
private final InputSocket<List<Number>> circularitySocket;
private final InputSocket<Boolean> colorSocket;
private final InputSocket<List<Number>> convexitySocket;
private final InputSocket<List<Number>> inertiaRatioSocket;

private final OutputSocket<BlobsReport> outputSocket;

Expand All @@ -57,6 +63,8 @@ public FindBlobsOperation(InputSocket.Factory inputSocketFactory, OutputSocket.F
this.minAreaSocket = inputSocketFactory.create(minAreaHint);
this.circularitySocket = inputSocketFactory.create(circularityHint);
this.colorSocket = inputSocketFactory.create(colorHint);
this.convexitySocket = inputSocketFactory.create(convexityHint);
this.inertiaRatioSocket = inputSocketFactory.create(inertiaRatioHint);

this.outputSocket = outputSocketFactory.create(blobsHint);
}
Expand All @@ -67,7 +75,10 @@ public List<InputSocket> getInputSockets() {
inputSocket,
minAreaSocket,
circularitySocket,
colorSocket
colorSocket,
//Sockets placed last to maintain backwards compatibility in deserialization
convexitySocket,
inertiaRatioSocket
);
}

Expand All @@ -85,7 +96,8 @@ public void perform() {
final Number minArea = minAreaSocket.getValue().get();
final List<Number> circularity = circularitySocket.getValue().get();
final Boolean darkBlobs = colorSocket.getValue().get();

final List<Number> convexity = convexitySocket.getValue().get();
final List<Number> inertiaRatio = inertiaRatioSocket.getValue().get();

final SimpleBlobDetector blobDetector = SimpleBlobDetector.create(new SimpleBlobDetector
.Params()
Expand All @@ -98,7 +110,15 @@ public void perform() {

.filterByCircularity(true)
.minCircularity(circularity.get(0).floatValue())
.maxCircularity(circularity.get(1).floatValue()));
.maxCircularity(circularity.get(1).floatValue())

.filterByConvexity(true)
.minConvexity(convexity.get(0).floatValue())
.maxConvexity(convexity.get(1).floatValue())

.filterByInertia(true)
.minInertiaRatio(inertiaRatio.get(0).floatValue())
.maxInertiaRatio(inertiaRatio.get(1).floatValue()));

// Detect the blobs and store them in the output BlobsReport
final KeyPointVector keyPointVector = new KeyPointVector();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* @param blobList The output where the MatOfKeyPoint is stored.
*/
//void $tMeth.name($step.name())(Mat *input, double *minArea, double circularity[2],
//bool *darkBlobs, vector<KeyPoint> *blobList) {
void $className::#func($step ["input", "minArea", "circularity", "darkBlobs", "blobList"]) {
//bool *darkBlobs, double convexity[2], double inertiaRatio[2], vector<KeyPoint> *blobList) {
void $className::#func($step ["input", "minArea", "circularity", "darkBlobs", "convexity", "inertiaRatio", "blobList"]) {
blobList.clear();
cv::SimpleBlobDetector::Params params;
params.filterByColor = 1;
Expand All @@ -21,8 +21,12 @@
params.filterByCircularity = true;
params.minCircularity = circularity[0];
params.maxCircularity = circularity[1];
params.filterByConvexity = false;
params.filterByInertia = false;
params.filterByConvexity = true;
params.minConvexity = convexity[0];
params.maxConvexity = convexity[1];
params.filterByInertia = true;
params.minInertia = inertiaRatio[0];
params.maxInertia = inertiaRatio[1];
cv::Ptr<cv::SimpleBlobDetector> detector = cv::SimpleBlobDetector::create(params);
detector->detect(input, blobList);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @param blobList The output where the MatOfKeyPoint is stored.
*/
private void $tMeth.name($step.name())(Mat input, double minArea, double[] circularity,
Boolean darkBlobs, MatOfKeyPoint blobList) {
Boolean darkBlobs, double[] convexity, double[] inertiaRatio, MatOfKeyPoint blobList) {
FeatureDetector blobDet = FeatureDetector.create(FeatureDetector.SIMPLEBLOB);
try {
File tempFile = File.createTempFile("config", ".xml");
Expand Down Expand Up @@ -40,11 +40,19 @@
config.append(circularity[1]);
config.append("</maxCircularity>\n");
config.append("<filterByInertia>1</filterByInertia>\n");
config.append("<minInertiaRatio>0.1</minInertiaRatio>\n");
config.append("<maxInertiaRatio>" + Integer.MAX_VALUE + "</maxInertiaRatio>\n");
config.append("<minInertiaRatio>");
config.append(inertiaRatio[0]);
config.append("</minInertiaRatio>\n");
config.append("<maxInertiaRatio>");
config.append(inertiaRatio[1]);
config.append("</maxInertiaRatio>\n");
config.append("<filterByConvexity>1</filterByConvexity>\n");
config.append("<minConvexity>0.95</minConvexity>\n");
config.append("<maxConvexity>" + Integer.MAX_VALUE + "</maxConvexity>\n");
config.append("<minConvexity>");
config.append(convexity[0]);
config.append("</minConvexity>\n");
config.append("<maxConvexity>");
config.append(convexity[1]);
config.append("</maxConvexity>\n");
config.append("</opencv_storage>\n");
FileWriter writer;
writer = new FileWriter(tempFile, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@staticmethod
def $tMeth.name($step.name())(input, min_area, circularity, dark_blobs):
def $tMeth.name($step.name())(input, min_area, circularity, dark_blobs, convexity, inertia_ratio):
"""Detects groups of pixels in an image.
Args:
input: A numpy.ndarray.
Expand All @@ -19,7 +19,11 @@
params.filterByCircularity = True
params.minCircularity = circularity[0]
params.maxCircularity = circularity[1]
params.filterByConvexity = False
params.filterByInertia = False
params.filterByConvexity = True
params.minConvexity = convexity[0]
params.maxConvexity = convexity[1]
params.filterByInertia = True
params.minInertia = inertia_ratio[0]
params.maxInertia = inertia_ratio[1]
detector = cv2.SimpleBlobDetector_create(params)
return detector.detect(input)