Skip to content

Commit 709f2fc

Browse files
committed
example 4
1 parent 3a0cbc9 commit 709f2fc

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed
39 KB
Loading
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Edge Detection Example
2+
#
3+
# Canny edge detection algorithm adapted from:
4+
# https://pyimagesearch.com/2015/04/06/zero-parameter-automatic-canny-edge-detection-with-python-and-opencv/
5+
#
6+
# Press 't', 'w', or any other key to set the edge detection mode.
7+
8+
import traceback
9+
10+
import numpy as np
11+
import cv2
12+
13+
from jpype import JClass
14+
15+
import py5_tools
16+
import py5
17+
18+
19+
def filter_image(img: py5.Py5Image, img_filtered: py5.Py5Image, mode: str):
20+
try:
21+
img.load_np_pixels()
22+
23+
if mode == 'auto':
24+
sigma = 0.33
25+
median = np.median(img.np_pixels)
26+
lower_limit = int(max(0, (1 - sigma) * median))
27+
upper_limit = int(min(255, (1 + sigma) * median))
28+
elif mode == 'tight':
29+
lower_limit, upper_limit = 225, 250
30+
elif mode == 'wide':
31+
lower_limit, upper_limit = 10, 200
32+
33+
img_array_gray = cv2.cvtColor(img.np_pixels, cv2.COLOR_BGR2GRAY)
34+
img_array_blurred = cv2.GaussianBlur(img_array_gray, (5, 5), 0)
35+
img_array_edges = cv2.Canny(
36+
img_array_blurred, lower_limit, upper_limit)
37+
38+
img_filtered.set_np_pixels(img_array_edges, bands='L')
39+
except Exception as e:
40+
traceback.print_exc()
41+
return JClass('java.lang.RuntimeException')(str(e))
42+
43+
44+
py5_tools.register_processing_mode_key("filter_image", filter_image)
45+
46+
47+
# run the sketch in processing mode, specifying the Java class to instantiate
48+
py5.run_sketch(jclassname='test.Example4Sketch')
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>py5</groupId>
8+
<artifactId>py5-processing-mode-example4</artifactId>
9+
<version>0.1</version>
10+
11+
<name>py5-processing-mode-example4</name>
12+
<url>https://py5coding.org/</url>
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>17</maven.compiler.source>
16+
<maven.compiler.target>17</maven.compiler.target>
17+
<jarlocation>${env.CONDA_PREFIX}/lib/python3.10/site-packages/py5/jars</jarlocation>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>py5</groupId>
23+
<artifactId>py5-processing4</artifactId>
24+
<version>0.9.0.dev0</version>
25+
<scope>system</scope>
26+
<systemPath>${jarlocation}/core.jar</systemPath>
27+
</dependency>
28+
<dependency>
29+
<groupId>py5</groupId>
30+
<artifactId>py5-jogl</artifactId>
31+
<version>0.9.0.dev0</version>
32+
<scope>system</scope>
33+
<systemPath>${jarlocation}/jogl-all.jar</systemPath>
34+
</dependency>
35+
<dependency>
36+
<groupId>py5</groupId>
37+
<artifactId>py5</artifactId>
38+
<version>0.9.0.dev0</version>
39+
<scope>system</scope>
40+
<systemPath>${jarlocation}/py5.jar</systemPath>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-dependency-plugin</artifactId>
49+
<version>3.2.0</version>
50+
<executions>
51+
<execution>
52+
<id>copy</id>
53+
<phase>package</phase>
54+
<goals>
55+
<goal>copy</goal>
56+
</goals>
57+
</execution>
58+
</executions>
59+
<configuration>
60+
<artifactItems>
61+
<artifactItem>
62+
<groupId>py5</groupId>
63+
<artifactId>py5-processing-mode-example4</artifactId>
64+
<version>0.1</version>
65+
<type>jar</type>
66+
<overWrite>true</overWrite>
67+
<outputDirectory>${project.basedir}/../jars</outputDirectory>
68+
<destFileName>py5-processing-mode-example4.jar</destFileName>
69+
</artifactItem>
70+
</artifactItems>
71+
</configuration>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
76+
</project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Edge Detection Example
3+
*
4+
* Canny edge detection algorithm adapted from:
5+
* https://pyimagesearch.com/2015/04/06/zero-parameter-automatic-canny-edge-detection-with-python-and-opencv/
6+
*
7+
* Press 't', 'w', or any other key to set the edge detection mode.
8+
*/
9+
package test;
10+
11+
import processing.core.PImage;
12+
import py5.core.SketchBase;
13+
14+
public class Example4Sketch extends SketchBase {
15+
16+
protected PImage img;
17+
protected PImage imgFiltered;
18+
protected String mode;
19+
20+
public void settings() {
21+
size(640, 360);
22+
}
23+
24+
public void setup() {
25+
mode = "auto";
26+
img = loadImage("moon.jpg");
27+
imgFiltered = createImage(img.width, img.height, RGB);
28+
29+
py5Println("Press 't' for tight edge detection");
30+
py5Println("Press 'w' for wide edge detection");
31+
py5Println("Press any other key for auto edge detection");
32+
33+
callPython("filter_image", img, imgFiltered, mode);
34+
}
35+
36+
public void draw() {
37+
image(img, 0, 0);
38+
image(imgFiltered, width/2, 0);
39+
}
40+
41+
public void keyPressed() {
42+
if (key == 't') {
43+
mode = "tight";
44+
} else if (key == 'w') {
45+
mode = "wide";
46+
} else {
47+
mode = "auto";
48+
}
49+
py5Println("Edge Detection Mode set to: " + mode);
50+
callPython("filter_image", img, imgFiltered, mode);
51+
}
52+
53+
}

0 commit comments

Comments
 (0)