mirror of
https://github.com/processing/processing4.git
synced 2026-01-29 19:31:16 +01:00
Re-adding Topics to SVN
This commit is contained in:
43
java/examples/Topics/Image Processing/Blur/Blur.pde
Normal file
43
java/examples/Topics/Image Processing/Blur/Blur.pde
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Blur.
|
||||
*
|
||||
* Bluring half of an image by processing it through a
|
||||
* low-pass filter.
|
||||
*/
|
||||
|
||||
float v = 1.0/9.0;
|
||||
float[][] kernel = { { v, v, v },
|
||||
{ v, v, v },
|
||||
{ v, v, v } };
|
||||
|
||||
size(200, 200);
|
||||
|
||||
PImage img = loadImage("trees.jpg"); // Load the original image
|
||||
image(img, 0, 0); // Displays the image from point (0,0)
|
||||
img.loadPixels();
|
||||
|
||||
// Create an opaque image of the same size as the original
|
||||
PImage edgeImg = createImage(img.width, img.height, RGB);
|
||||
|
||||
// Loop through every pixel in the image.
|
||||
for (int y = 1; y < img.height-1; y++) { // Skip top and bottom edges
|
||||
for (int x = 1; x < img.width-1; x++) { // Skip left and right edges
|
||||
float sum = 0; // Kernel sum for this pixel
|
||||
for (int ky = -1; ky <= 1; ky++) {
|
||||
for (int kx = -1; kx <= 1; kx++) {
|
||||
// Calculate the adjacent pixel for this kernel point
|
||||
int pos = (y + ky)*img.width + (x + kx);
|
||||
// Image is grayscale, red/green/blue are identical
|
||||
float val = red(img.pixels[pos]);
|
||||
// Multiply adjacent pixels based on the kernel values
|
||||
sum += kernel[ky+1][kx+1] * val;
|
||||
}
|
||||
}
|
||||
// For this pixel in the new image, set the gray value
|
||||
// based on the sum from the kernel
|
||||
edgeImg.pixels[y*img.width + x] = color(sum);
|
||||
}
|
||||
}
|
||||
// State that there are changes to edgeImg.pixels[]
|
||||
edgeImg.updatePixels();
|
||||
image(edgeImg, 100, 0); // Draw the new image
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Brightness
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* Adjusts the brightness of part of the image
|
||||
* Pixels closer to the mouse will appear brighter.
|
||||
*/
|
||||
|
||||
PImage img;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
frameRate(30);
|
||||
img = loadImage("wires.jpg");
|
||||
img.loadPixels();
|
||||
// Only need to load the pixels[] array once, because we're only
|
||||
// manipulating pixels[] inside draw(), not drawing shapes.
|
||||
loadPixels();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
for (int x = 0; x < img.width; x++) {
|
||||
for (int y = 0; y < img.height; y++ ) {
|
||||
// Calculate the 1D location from a 2D grid
|
||||
int loc = x + y*img.width;
|
||||
// Get the R,G,B values from image
|
||||
float r,g,b;
|
||||
r = red (img.pixels[loc]);
|
||||
//g = green (img.pixels[loc]);
|
||||
//b = blue (img.pixels[loc]);
|
||||
// Calculate an amount to change brightness based on proximity to the mouse
|
||||
float maxdist = 50;//dist(0,0,width,height);
|
||||
float d = dist(x,y,mouseX,mouseY);
|
||||
float adjustbrightness = 255*(maxdist-d)/maxdist;
|
||||
r += adjustbrightness;
|
||||
//g += adjustbrightness;
|
||||
//b += adjustbrightness;
|
||||
// Constrain RGB to make sure they are within 0-255 color range
|
||||
r = constrain(r,0,255);
|
||||
//g = constrain(g,0,255);
|
||||
//b = constrain(b,0,255);
|
||||
// Make a new color and set pixel in the window
|
||||
//color c = color(r,g,b);
|
||||
color c = color(r);
|
||||
pixels[y*width + x] = c;
|
||||
}
|
||||
}
|
||||
updatePixels();
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Convolution
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* Applies a convolution matrix to a portion of the index.
|
||||
* Move mouse to apply filter to different parts of the image.
|
||||
*/
|
||||
|
||||
PImage img;
|
||||
int w = 80;
|
||||
|
||||
// It's possible to convolve the image with
|
||||
// many different matrices
|
||||
|
||||
float[][] matrix = { { -1, -1, -1 },
|
||||
{ -1, 9, -1 },
|
||||
{ -1, -1, -1 } };
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
frameRate(30);
|
||||
img = loadImage("end.jpg");
|
||||
}
|
||||
|
||||
void draw() {
|
||||
// We're only going to process a portion of the image
|
||||
// so let's set the whole image as the background first
|
||||
image(img,0,0);
|
||||
// Where is the small rectangle we will process
|
||||
int xstart = constrain(mouseX-w/2,0,img.width);
|
||||
int ystart = constrain(mouseY-w/2,0,img.height);
|
||||
int xend = constrain(mouseX+w/2,0,img.width);
|
||||
int yend = constrain(mouseY+w/2,0,img.height);
|
||||
int matrixsize = 3;
|
||||
loadPixels();
|
||||
// Begin our loop for every pixel
|
||||
for (int x = xstart; x < xend; x++) {
|
||||
for (int y = ystart; y < yend; y++ ) {
|
||||
color c = convolution(x,y,matrix,matrixsize,img);
|
||||
int loc = x + y*img.width;
|
||||
pixels[loc] = c;
|
||||
}
|
||||
}
|
||||
updatePixels();
|
||||
}
|
||||
|
||||
color convolution(int x, int y, float[][] matrix,int matrixsize, PImage img)
|
||||
{
|
||||
float rtotal = 0.0;
|
||||
float gtotal = 0.0;
|
||||
float btotal = 0.0;
|
||||
int offset = matrixsize / 2;
|
||||
for (int i = 0; i < matrixsize; i++){
|
||||
for (int j= 0; j < matrixsize; j++){
|
||||
// What pixel are we testing
|
||||
int xloc = x+i-offset;
|
||||
int yloc = y+j-offset;
|
||||
int loc = xloc + img.width*yloc;
|
||||
// Make sure we haven't walked off our image, we could do better here
|
||||
loc = constrain(loc,0,img.pixels.length-1);
|
||||
// Calculate the convolution
|
||||
rtotal += (red(img.pixels[loc]) * matrix[i][j]);
|
||||
gtotal += (green(img.pixels[loc]) * matrix[i][j]);
|
||||
btotal += (blue(img.pixels[loc]) * matrix[i][j]);
|
||||
}
|
||||
}
|
||||
// Make sure RGB is within range
|
||||
rtotal = constrain(rtotal,0,255);
|
||||
gtotal = constrain(gtotal,0,255);
|
||||
btotal = constrain(btotal,0,255);
|
||||
// Return the resulting color
|
||||
return color(rtotal,gtotal,btotal);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Edge Detection.
|
||||
*
|
||||
* Exposing areas of contrast within an image
|
||||
* by processing it through a high-pass filter.
|
||||
*/
|
||||
|
||||
float[][] kernel = { { -1, -1, -1 },
|
||||
{ -1, 9, -1 },
|
||||
{ -1, -1, -1 } };
|
||||
|
||||
size(200, 200);
|
||||
PImage img = loadImage("house.jpg"); // Load the original image
|
||||
image(img, 0, 0); // Displays the image from point (0,0)
|
||||
img.loadPixels();
|
||||
// Create an opaque image of the same size as the original
|
||||
PImage edgeImg = createImage(img.width, img.height, RGB);
|
||||
// Loop through every pixel in the image.
|
||||
for (int y = 1; y < img.height-1; y++) { // Skip top and bottom edges
|
||||
for (int x = 1; x < img.width-1; x++) { // Skip left and right edges
|
||||
float sum = 0; // Kernel sum for this pixel
|
||||
for (int ky = -1; ky <= 1; ky++) {
|
||||
for (int kx = -1; kx <= 1; kx++) {
|
||||
// Calculate the adjacent pixel for this kernel point
|
||||
int pos = (y + ky)*img.width + (x + kx);
|
||||
// Image is grayscale, red/green/blue are identical
|
||||
float val = red(img.pixels[pos]);
|
||||
// Multiply adjacent pixels based on the kernel values
|
||||
sum += kernel[ky+1][kx+1] * val;
|
||||
}
|
||||
}
|
||||
// For this pixel in the new image, set the gray value
|
||||
// based on the sum from the kernel
|
||||
edgeImg.pixels[y*img.width + x] = color(sum);
|
||||
}
|
||||
}
|
||||
// State that there are changes to edgeImg.pixels[]
|
||||
edgeImg.updatePixels();
|
||||
image(edgeImg, 100, 0); // Draw the new image
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Histogram.
|
||||
*
|
||||
* Calculates the histogram of an image.
|
||||
* A histogram is the frequency distribution
|
||||
* of the gray levels with the number of pure black values
|
||||
* displayed on the left and number of pure white values on the right.
|
||||
*
|
||||
* Updated 28 February, 2010.
|
||||
* Note that this sketch will behave differently on Android,
|
||||
* since most images will no longer be full 24-bit color.
|
||||
*/
|
||||
|
||||
size(200, 200);
|
||||
|
||||
// Load an image from the data directory
|
||||
// Load a different image by modifying the comments
|
||||
PImage img = loadImage("cdi01_g.jpg");
|
||||
image(img, 0, 0);
|
||||
int[] hist = new int[256];
|
||||
|
||||
// Calculate the histogram
|
||||
for (int i = 0; i < img.width; i++) {
|
||||
for (int j = 0; j < img.height; j++) {
|
||||
int bright = int(brightness(get(i, j)));
|
||||
hist[bright]++;
|
||||
}
|
||||
}
|
||||
|
||||
// Find the largest value in the histogram
|
||||
int histMax = max(hist);
|
||||
|
||||
stroke(255);
|
||||
// Draw half of the histogram (skip every second value)
|
||||
for (int i = 0; i < img.width; i += 2) {
|
||||
// Map i (from 0..img.width-1) to a location in the histogram (0..255)
|
||||
int which = int(map(i, 0, img.width, 0, 255));
|
||||
// Convert the histogram value to a location between
|
||||
// the bottom and the top of the picture
|
||||
int y = int(map(hist[which], 0, histMax, img.height, 0));
|
||||
line(i, img.height, i, y);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Linear Image.
|
||||
*
|
||||
* Click and drag mouse up and down to control the signal.
|
||||
* Press and hold any key to watch the scanning.
|
||||
*
|
||||
* Updated 28 February 2010.
|
||||
*/
|
||||
|
||||
PImage img;
|
||||
int direction = 1;
|
||||
|
||||
float signal;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
stroke(255);
|
||||
img = loadImage("florence03.jpg");
|
||||
img.loadPixels();
|
||||
loadPixels();
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (signal > img.height-1 || signal < 0) {
|
||||
direction = direction * -1;
|
||||
}
|
||||
if (mousePressed) {
|
||||
signal = abs(mouseY % img.height);
|
||||
} else {
|
||||
signal += (0.3*direction);
|
||||
}
|
||||
|
||||
if (keyPressed) {
|
||||
set(0, 0, img);
|
||||
line(0, signal, img.width, signal);
|
||||
} else {
|
||||
int signalOffset = int(signal)*img.width;
|
||||
for (int y = 0; y < img.height; y++) {
|
||||
arrayCopy(img.pixels, signalOffset, pixels, y*width, img.width);
|
||||
}
|
||||
updatePixels();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Pixel Array.
|
||||
*
|
||||
* Click and drag the mouse up and down to control the signal and
|
||||
* press and hold any key to see the current pixel being read.
|
||||
* This program sequentially reads the color of every pixel of an image
|
||||
* and displays this color to fill the window.
|
||||
*
|
||||
* Updated 28 February 2010.
|
||||
*/
|
||||
|
||||
PImage img;
|
||||
int direction = 1;
|
||||
float signal;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
noFill();
|
||||
stroke(255);
|
||||
frameRate(30);
|
||||
img = loadImage("ystone08.jpg");
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (signal > img.width*img.height-1 || signal < 0) {
|
||||
direction = direction * -1;
|
||||
}
|
||||
|
||||
if (mousePressed) {
|
||||
int mx = constrain(mouseX, 0, img.width-1);
|
||||
int my = constrain(mouseY, 0, img.height-1);
|
||||
signal = my*img.width + mx;
|
||||
} else {
|
||||
signal += 0.33*direction;
|
||||
}
|
||||
|
||||
int sx = int(signal) % img.width;
|
||||
int sy = int(signal) / img.width;
|
||||
|
||||
if (keyPressed) {
|
||||
set(0, 0, img); // fast way to draw an image
|
||||
point(sx, sy);
|
||||
rect(sx - 5, sy - 5, 10, 10);
|
||||
} else {
|
||||
color c = img.get(sx, sy);
|
||||
background(c);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user