You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* @arialabel Astronaut rendered in black and white on the left and a highly sharpened version of the image on the right
4
-
* @description A high-pass filter sharpens an image. This program analyzes every pixel in an image in relation to the neighboring pixels to sharpen the image.
4
+
* @description A high-pass filter sharpens an image. This program analyzes every pixel in an image in relation to the neighboring pixels to sharpen the image.
5
5
* <br><br><span class="small"><em>This example is ported from the <a href="https://processing.org/examples/edgedetection.html">Edge Detection example</a>
6
6
* on the Processing website</em></span>
7
7
*/
@@ -12,17 +12,21 @@
12
12
// to consider all neighboring pixels we use a 3x3 array
13
13
// and normalize these values
14
14
// kernel is the 3x3 matrix of normalized values
15
-
letkernel=[[-1,-1,-1],[-1,9,-1],[-1,-1,-1]];
15
+
letkernel=[
16
+
[-1,-1,-1],
17
+
[-1,9,-1],
18
+
[-1,-1,-1]
19
+
];
16
20
17
21
// preload() runs once, before setup()
18
22
// loadImage() needs to occur here instead of setup()
19
-
// if loadImage() is called in setup(), the image won't appear
23
+
// if loadImage() is called in setup(), the image won't appear
20
24
// since noLoop() restricts draw() to execute only once
21
25
// (one execution of draw() is not enough time for the image to load),
22
26
// preload() makes sure image is loaded before anything else occurs
23
27
functionpreload(){
24
28
// load the original image
25
-
img=loadImage("assets/rover.png");
29
+
img=loadImage('assets/rover.png');
26
30
}
27
31
28
32
// setup() runs after preload, once()
@@ -36,17 +40,15 @@ function setup() {
36
40
// draw() runs after setup(), normally on a loop
37
41
// in this case it runs only once, because of noDraw()
38
42
functiondraw(){
39
-
40
43
// place the original image on the upper left corner
41
44
image(img,0,0);
42
45
43
46
// create a new image, same dimensions as img
44
47
edgeImg=createImage(img.width,img.height);
45
-
48
+
46
49
// load its pixels
47
50
edgeImg.loadPixels();
48
51
49
-
50
52
// two for() loops, to iterate in x axis and y axis
51
53
// since the kernel assumes that the pixel
52
54
// has pixels above, under, left, and right
@@ -57,17 +59,16 @@ function draw() {
57
59
for(letx=1;x<img.width-1;x++){
58
60
for(lety=1;y<img.height-1;y++){
59
61
// kernel sum for the current pixel starts as 0
60
-
letsum=0;
61
-
62
+
letsum=0;
63
+
62
64
// kx, ky variables for iterating over the kernel
63
65
// kx, ky have three different values: -1, 0, 1
64
66
for(kx=-1;kx<=1;kx++){
65
67
for(ky=-1;ky<=1;ky++){
66
-
67
68
letxpos=x+kx;
68
69
letypos=y+ky;
69
-
letpos=(y+ky)*img.width+(x+kx);
70
-
// since our image is grayscale,
70
+
letpos=(y+ky)*img.width+(x+kx);
71
+
// since our image is grayscale,
71
72
// RGB values are identical
72
73
// we retrieve the red value for this example
73
74
letval=red(img.get(xpos,ypos));
@@ -77,18 +78,18 @@ function draw() {
77
78
// if we add 1 to kx and ky, we get 0, 1, 2
78
79
// with that we can use it to iterate over kernel
79
80
// and calculate the accumulated sum
80
-
sum+=kernel[ky+1][kx+1]*val;
81
+
sum+=kernel[ky+1][kx+1]*val;
81
82
}
82
83
}
83
-
84
-
// set the pixel value of the edgeImg
84
+
85
+
// set the pixel value of the edgeImg
85
86
edgeImg.set(x,y,color(sum,sum,sum));
86
87
}
87
88
}
88
-
89
+
89
90
// updatePixels() to write the changes on edgeImg
90
91
edgeImg.updatePixels();
91
-
92
+
92
93
// draw edgeImg at the right of the original image
0 commit comments