Skip to content

Commit ca3c082

Browse files
authored
Merge pull request atduskgreg#77 from qwzybug/morphological-closing
Support for morphological operations like opening/closing et al.
2 parents 37e4be0 + 1745cce commit ca3c082

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
@@ -927,6 +927,55 @@ public void erode(){
927927
Imgproc.erode(getCurrentMat(), getCurrentMat(), new Mat());
928928
}
929929

930+
/**
931+
* Apply a morphological operation (e.g., opening, closing) to the image with a given kernel element.
932+
*
933+
* See:
934+
* http://docs.opencv.org/doc/tutorials/imgproc/opening_closing_hats/opening_closing_hats.html
935+
*
936+
* @param operation
937+
* The morphological operation to apply: Imgproc.MORPH_CLOSE, MORPH_OPEN,
938+
* MORPH_TOPHAT, MORPH_BLACKHAT, MORPH_GRADIENT.
939+
* @param kernelElement
940+
* The shape to apply the operation with: Imgproc.MORPH_RECT, MORPH_CROSS, or MORPH_ELLIPSE.
941+
* @param width
942+
* Width of the shape.
943+
* @param height
944+
* Height of the shape.
945+
*/
946+
public void morphX(int operation, int kernelElement, int width, int height) {
947+
Mat kernel = Imgproc.getStructuringElement(kernelElement, new Size(width, height));
948+
Imgproc.morphologyEx(getCurrentMat(), getCurrentMat(), operation, kernel);
949+
}
950+
951+
/**
952+
* Close the image with a circle of a given size.
953+
*
954+
* See:
955+
* http://docs.opencv.org/doc/tutorials/imgproc/opening_closing_hats/opening_closing_hats.html#closing
956+
*
957+
* @param size
958+
* Radius of the circle to close with.
959+
*/
960+
public void close(int size) {
961+
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(size, size));
962+
Imgproc.morphologyEx(getCurrentMat(), getCurrentMat(), Imgproc.MORPH_CLOSE, kernel);
963+
}
964+
965+
/**
966+
* Open the image with a circle of a given size.
967+
*
968+
* See:
969+
* http://docs.opencv.org/doc/tutorials/imgproc/opening_closing_hats/opening_closing_hats.html#opening
970+
*
971+
* @param size
972+
* Radius of the circle to open with.
973+
*/
974+
public void open(int size) {
975+
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(size, size));
976+
Imgproc.morphologyEx(getCurrentMat(), getCurrentMat(), Imgproc.MORPH_OPEN, kernel);
977+
}
978+
930979
/**
931980
* Blur an image symetrically by a given number of pixels.
932981
*

0 commit comments

Comments
 (0)