Permalink
Browse files

Merge pull request #77 from qwzybug/morphological-closing

Support for morphological operations like opening/closing et al.
  • Loading branch information...
atduskgreg committed May 22, 2017
2 parents 37e4be0 + 1745cce commit ca3c082b659b273259b7c21cc989689af27e65ef
@@ -0,0 +1,43 @@
import gab.opencv.*;
import org.opencv.imgproc.Imgproc;
OpenCV opencv;
PImage img, opened, closed, tophat;
void setup() {
img = loadImage("test.jpg");
size(img.width, img.height);
opencv = new OpenCV(this, img);
PImage snap = opencv.getSnapshot();
opencv.open(16);
opened = opencv.getSnapshot();
opencv.loadImage(snap);
opencv.close(16);
closed = opencv.getSnapshot();
opencv.loadImage(snap);
opencv.morphX(Imgproc.MORPH_TOPHAT, Imgproc.MORPH_CROSS, 8, 8);
tophat = opencv.getSnapshot();
}
void draw() {
pushMatrix();
scale(0.5);
image(img, 0, 0);
image(opened, img.width, 0);
image(closed, 0, img.height);
image(tophat, img.width, img.height);
popMatrix();
fill(0);
text("source", img.width/2 - 100, 20 );
text("open(16)", img.width - 100, 20 );
text("close(16)", img.width/2 - 100, img.height/2 + 20 );
fill(255);
text("tophat(cross, 8, 8)", img.width - 150, img.height/2 + 20 );
}
Binary file not shown.
@@ -191,4 +191,10 @@ An in-depth advanced example. Detect a CV marker in an image, warp perspective,
Code: [MarkerDetection.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/MarkerDetection/MarkerDetection.pde)
#### MorphologyOperations
Open and close an image, or do more complicated morphological transformations.
<a href="https://flic.kr/p/tazj7r" title="Morphology operations"><img src="https://farm6.staticflickr.com/5340/17829980821_1734e8bab8_z_d.jpg" width="640" height="393" alt="Morphology operations"></a>
Code: [MorphologyOperations.pde](examples/MorphologyOperations/MorphologyOperations.pde)
@@ -927,6 +927,55 @@ public void erode(){
Imgproc.erode(getCurrentMat(), getCurrentMat(), new Mat());
}
/**
* Apply a morphological operation (e.g., opening, closing) to the image with a given kernel element.
*
* See:
* http://docs.opencv.org/doc/tutorials/imgproc/opening_closing_hats/opening_closing_hats.html
*
* @param operation
* The morphological operation to apply: Imgproc.MORPH_CLOSE, MORPH_OPEN,
* MORPH_TOPHAT, MORPH_BLACKHAT, MORPH_GRADIENT.
* @param kernelElement
* The shape to apply the operation with: Imgproc.MORPH_RECT, MORPH_CROSS, or MORPH_ELLIPSE.
* @param width
* Width of the shape.
* @param height
* Height of the shape.
*/
public void morphX(int operation, int kernelElement, int width, int height) {
Mat kernel = Imgproc.getStructuringElement(kernelElement, new Size(width, height));
Imgproc.morphologyEx(getCurrentMat(), getCurrentMat(), operation, kernel);
}
/**
* Close the image with a circle of a given size.
*
* See:
* http://docs.opencv.org/doc/tutorials/imgproc/opening_closing_hats/opening_closing_hats.html#closing
*
* @param size
* Radius of the circle to close with.
*/
public void close(int size) {
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(size, size));
Imgproc.morphologyEx(getCurrentMat(), getCurrentMat(), Imgproc.MORPH_CLOSE, kernel);
}
/**
* Open the image with a circle of a given size.
*
* See:
* http://docs.opencv.org/doc/tutorials/imgproc/opening_closing_hats/opening_closing_hats.html#opening
*
* @param size
* Radius of the circle to open with.
*/
public void open(int size) {
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(size, size));
Imgproc.morphologyEx(getCurrentMat(), getCurrentMat(), Imgproc.MORPH_OPEN, kernel);
}
/**
* Blur an image symetrically by a given number of pixels.
*

0 comments on commit ca3c082

Please sign in to comment.