Skip to content

Commit 1745cce

Browse files
committed
Support for morphological operations like opening/closing et al.
1 parent 157888a commit 1745cce

4 files changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import gab.opencv.*;
2+
3+
import org.opencv.imgproc.Imgproc;
4+
5+
OpenCV opencv;
6+
PImage img, opened, closed, tophat;
7+
8+
void setup() {
9+
img = loadImage("test.jpg");
10+
size(img.width, img.height);
11+
12+
opencv = new OpenCV(this, img);
13+
PImage snap = opencv.getSnapshot();
14+
15+
opencv.open(16);
16+
opened = opencv.getSnapshot();
17+
18+
opencv.loadImage(snap);
19+
opencv.close(16);
20+
closed = opencv.getSnapshot();
21+
22+
opencv.loadImage(snap);
23+
opencv.morphX(Imgproc.MORPH_TOPHAT, Imgproc.MORPH_CROSS, 8, 8);
24+
tophat = opencv.getSnapshot();
25+
}
26+
27+
void draw() {
28+
pushMatrix();
29+
scale(0.5);
30+
image(img, 0, 0);
31+
image(opened, img.width, 0);
32+
image(closed, 0, img.height);
33+
image(tophat, img.width, img.height);
34+
popMatrix();
35+
36+
fill(0);
37+
text("source", img.width/2 - 100, 20 );
38+
text("open(16)", img.width - 100, 20 );
39+
text("close(16)", img.width/2 - 100, img.height/2 + 20 );
40+
fill(255);
41+
text("tophat(cross, 8, 8)", img.width - 150, img.height/2 + 20 );
42+
}
43+
145 KB
Loading

readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,10 @@ An in-depth advanced example. Detect a CV marker in an image, warp perspective,
191191

192192
Code: [MarkerDetection.pde](https://github.com/atduskgreg/opencv-processing/blob/master/examples/MarkerDetection/MarkerDetection.pde)
193193

194+
#### MorphologyOperations
194195

196+
Open and close an image, or do more complicated morphological transformations.
197+
198+
<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>
199+
200+
Code: [MorphologyOperations.pde](examples/MorphologyOperations/MorphologyOperations.pde)

src/gab/opencv/OpenCV.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,55 @@ public void erode(){
914914
Imgproc.erode(getCurrentMat(), getCurrentMat(), new Mat());
915915
}
916916

917+
/**
918+
* Apply a morphological operation (e.g., opening, closing) to the image with a given kernel element.
919+
*
920+
* See:
921+
* http://docs.opencv.org/doc/tutorials/imgproc/opening_closing_hats/opening_closing_hats.html
922+
*
923+
* @param operation
924+
* The morphological operation to apply: Imgproc.MORPH_CLOSE, MORPH_OPEN,
925+
* MORPH_TOPHAT, MORPH_BLACKHAT, MORPH_GRADIENT.
926+
* @param kernelElement
927+
* The shape to apply the operation with: Imgproc.MORPH_RECT, MORPH_CROSS, or MORPH_ELLIPSE.
928+
* @param width
929+
* Width of the shape.
930+
* @param height
931+
* Height of the shape.
932+
*/
933+
public void morphX(int operation, int kernelElement, int width, int height) {
934+
Mat kernel = Imgproc.getStructuringElement(kernelElement, new Size(width, height));
935+
Imgproc.morphologyEx(getCurrentMat(), getCurrentMat(), operation, kernel);
936+
}
937+
938+
/**
939+
* Close the image with a circle of a given size.
940+
*
941+
* See:
942+
* http://docs.opencv.org/doc/tutorials/imgproc/opening_closing_hats/opening_closing_hats.html#closing
943+
*
944+
* @param size
945+
* Radius of the circle to close with.
946+
*/
947+
public void close(int size) {
948+
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(size, size));
949+
Imgproc.morphologyEx(getCurrentMat(), getCurrentMat(), Imgproc.MORPH_CLOSE, kernel);
950+
}
951+
952+
/**
953+
* Open the image with a circle of a given size.
954+
*
955+
* See:
956+
* http://docs.opencv.org/doc/tutorials/imgproc/opening_closing_hats/opening_closing_hats.html#opening
957+
*
958+
* @param size
959+
* Radius of the circle to open with.
960+
*/
961+
public void open(int size) {
962+
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(size, size));
963+
Imgproc.morphologyEx(getCurrentMat(), getCurrentMat(), Imgproc.MORPH_OPEN, kernel);
964+
}
965+
917966
/**
918967
* Blur an image symetrically by a given number of pixels.
919968
*

0 commit comments

Comments
 (0)