// Displaying an image, example 1 size(600, 400); PImage img = loadImage("robotmarko-g.jpg"); image(img, 0, 0); //------------------------------------ // Displaying an image, example 2 PImage img = loadImage("robotmarko-g.jpg"); size(680, 460); image(img, 30, 40); //------------------------------------ // Displaying an image, example 3 size(600, 400); PImage img = loadImage("robotmarko-g.jpg"); image(img, 30, 40); //------------------------------------ // Displaying a grayscale image, ver. 1 size(600, 400); PImage img = loadImage("robotmarko-g.jpg"); loadPixels(); img.loadPixels(); for (int i = 0; i < img.pixels.length; i++) { pixels[i] = img.pixels[i]; } updatePixels(); //------------------------------------ // Displaying a grayscale image, ver. 2 size(600, 400); PImage img = loadImage("robotmarko-g.jpg"); loadPixels(); img.loadPixels(); for (int i = 0; i < img.pixels.length; i++) { float r = brightness(img.pixels[i]); pixels[i] = color(r); } updatePixels(); //------------------------------------ // Displaying a grayscale image, ver. 3 size(600, 400); PImage img = loadImage("robotmarko-g.jpg"); loadPixels(); img.loadPixels(); for (int y = 0; y < img.height; y++) for (int x = 0; x < img.width; x++) { int index = x + y * img.width; float r = brightness(img.pixels[index]); pixels[index] = color(r); } updatePixels(); //------------------------------------ // Displaying a grayscale image, ver. 4 size(680, 460); PImage img = loadImage("robotmarko-g.jpg"); loadPixels(); img.loadPixels(); int x0 = 40; int y0 = 30; for (int y = 0; y < img.height; y++) for (int x = 0; x < img.width; x++) { int index1 = x + y * img.width; float r = brightness(img.pixels[index1]); int index2 = (x + x0) + (y + y0) * width; pixels[index2] = color(r); } updatePixels(); //------------------------------------ // Displaying a grayscale image, ver. 5 size(680, 460); PImage img = loadImage("robotmarko-g.jpg"); loadPixels(); img.loadPixels(); int x0 = -40; int y0 = 60; for (int y = 0; y < img.height; y++) for (int x = 0; x < img.width; x++) { if ((x + x0 < width) && (y + y0 < height) && (x + x0 >= 0) && (y + y0 >= 0)) { int index1 = x + y * img.width; float r = brightness(img.pixels[index1]); int index2 = (x + x0) + (y + y0) * width; pixels[index2] = color(r); } } updatePixels(); //------------------------------------ // The setup function and displaying a grayscale image, ver. 6 void setup() { size(600, 400); PImage img = loadImage("robotmarko-g.jpg"); display(img, -30, 40); } void display(PImage img, int x0, int y0) { img.loadPixels(); loadPixels(); for (int y = 0; y < img.height; y++) for (int x = 0; x < img.width; x++) { if ((x + x0 < width) && (y + y0 < height) && (x + x0 >= 0) && (y + y0 >= 0)) { int index1 = x + y * img.width; float r = brightness(img.pixels[index1]); int index2 = (x + x0) + (y + y0) * width; pixels[index2] = color(r); } } updatePixels(); } //------------------------------------ // Displaying a color image void setup() { size(600, 400); PImage img1 = loadImage("robotmarko-g.jpg"); PImage img2 = loadImage("robotmarko.jpg"); display2(img1, -30, 40); display2(img2, 130, -40); } void display2(PImage img, int x0, int y0) { img.loadPixels(); loadPixels(); for (int y = 0; y < img.height; y++) for (int x = 0; x < img.width; x++) { if ((x + x0 < width) && (y + y0 < height) && (x + x0 >= 0) && (y + y0 >= 0)) { int index1 = x + y * img.width; float r = red(img.pixels[index1]); float g = green(img.pixels[index1]); float b = blue(img.pixels[index1]); int index2 = (x + x0) + (y + y0) * width; pixels[index2] = color(r, g, b); } } updatePixels(); } //------------------------------------ // Displaying the red channel of an image void setup() { size(600, 400); PImage img1 = loadImage("robotmarko-g.jpg"); PImage img2 = loadImage("robotmarko.jpg"); redChannel(img1, -30, 40); redChannel(img2, 130, -40); } void redChannel(PImage img, int x0, int y0) { img.loadPixels(); loadPixels(); for (int y = 0; y < img.height; y++) for (int x = 0; x < img.width; x++) { if ((x + x0 < width) && (y + y0 < height) && (x + x0 >= 0) && (y + y0 >= 0)) { int index1 = x + y * img.width; float r = red(img.pixels[index1]); int index2 = (x + x0) + (y + y0) * width; pixels[index2] = color(r, 0, 0); } } updatePixels(); } //------------------------------------ // Conversion to grayscale void setup() { size(1200, 400); PImage img1 = loadImage("robotmarko.jpg"); image(img1, 0, 0); PImage img2 = grayscale(img1); image(img2, img1.width, 0); } PImage grayscale(PImage img) { PImage img_ = createImage(img.width, img.height, RGB); img.loadPixels(); img_.loadPixels(); for (int y = 0; y < img.height; y++) for (int x = 0; x < img.width; x++) { int index = x + y * img.width; float r = red(img.pixels[index]); float g = green(img.pixels[index]); float b = blue(img.pixels[index]); img_.pixels[index] = color(0.21 * r + 0.72 * g + 0.07 * b); } img_.updatePixels(); return img_; } //------------------------------------ // Negative PImage negative(PImage img) { PImage img_ = createImage(img.width, img.height, RGB); img.loadPixels(); img_.loadPixels(); for (int y = 0; y < img.height; y++) for (int x = 0; x < img.width; x++) { int index = x + y * img.width; float r = brightness(img.pixels[index]); img_.pixels[index] = color(255 - r); } img_.updatePixels(); return img_; } //------------------------------------ // Computing and representing the histogram PImage img = loadImage("robotmarko-g.jpg"); size(256, 256); background(255); loadPixels(); img.loadPixels(); int[] h = new int[256]; for (int y = 0; y < img.height; y++) for (int x = 0; x < img.width; x++) { int imgIndex = x + y * img.width; int r = int(brightness(img.pixels[imgIndex])); h[r]++; } int hMax = 0; for (int i = 0; i < 256; i++) if (h[i] > hMax) hMax = h[i]; stroke(0); for (int i = 0; i < 256; i++) line(i, height - 1, i, (height - 1) * (1 - (float) h[i] / hMax)); //------------------------------------ // Clipping (Contrast Stretching) PImage clipping(PImage img) { PImage img_ = createImage(img.width, img.height, RGB); img.loadPixels(); img_.loadPixels(); int rmin = 255, rmax = 0; for (int y = 0; y < img.height; y++) for (int x = 0; x < img.width; x++) { int imgIndex = x + y * img.width; int r = int(brightness(img.pixels[imgIndex])); if (r < rmin) rmin = r; if (r > rmax) rmax = r; } for (int y = 0; y < img.height; y++) for (int x = 0; x < img.width; x++) { int index = x + y * img.width; float r = brightness(img.pixels[index]); img_.pixels[index] = color(255 * (r - rmin) / (rmax - rmin)); } img_.updatePixels(); return img_; } //------------------------------------ // Histogram Equalization PImage histogramequalization(PImage img) { PImage img_ = createImage(img.width, img.height, RGB); img.loadPixels(); img_.loadPixels(); int[] h = new int[256]; int[] heq = new int[256]; for (int y = 0; y < img.height; y++) for (int x = 0; x < img.width; x++) { int index = x + y * img.width; int r = int(brightness(img.pixels[index])); h[r]++; } int temp = 0; for(int i = 0; i < 256; i++){ temp += h[i]; heq[i] = (int) 255 * temp / (img.width * img.height); } for (int y = 0; y < img.height; y++) for (int x = 0; x < img.width; x++) { int index = x + y * img.width; int r = int(brightness(img.pixels[index])); img_.pixels[index] = color(heq[r]); } img_.updatePixels(); return img_; } //------------------------------------ //Image binarization PImage binarization(PImage img, float thresholdvalue) { PImage img_ = createImage(img.width, img.height, RGB); img.loadPixels(); img_.loadPixels(); for(int y = 0; y < img.height; y++) for (int x = 0; x < img.width; x++) { int index = y * img.width + x; float r = brightness(img.pixels[index]); if (r <= thresholdvalue) img_.pixels[index] = color(0); else img_.pixels[index] = color(255); } img_.updatePixels(); return img_; } //------------------------------------ //Cropping to content //Notes: //1. Incomplete version (cropping along the y-axis) //2. Assumption: object - black, background - white PImage cropping(PImage img) { img.loadPixels(); boolean found = false; int y = 0; for (y = 0; (y < img.height && !found); y++) for (int x = 0; (x < img.width && !found); x++) { int index = y * img.width + x; float r = brightness(img.pixels[index]); if (r == 0) found = true; } int ymin = y; found = false; for (y = img.height - 1; (y >= 0 && !found); y--) { for (int x = 0; (x < img.width && !found); x++) { int index = y * img.width + x; float r = brightness(img.pixels[index]); if (r == 0) found = true; } } int ymax = y; return img.get(0, ymin, img.width, ymax - ymin + 1); } //------------------------------------ // Sobel filters PImage sobel(PImage img) { PImage img_ = createImage(img.width - 2, img.height - 2, RGB); img.loadPixels(); img_.loadPixels(); float[][] filter1 = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}}; float[][] filter2 = {{-1, -2, -1}, { 0, 0, 0}, { 1, 2, 1}}; for (int y = 1; y < img.height - 1; y++) for (int x = 1; x < img.width - 1; x++) { float f1 = 0, f2 = 0; for (int ky = -1; ky <= 1; ky++) for (int kx = -1; kx <= 1; kx++) { int index = (y + ky) * img.width + (x + kx); float r = brightness(img.pixels[index]); f1 += filter1[ky+1][kx+1] * r; f2 += filter2[ky+1][kx+1] * r; } img_.pixels[(y-1) * img_.width + (x-1)] = color(sqrt(f1 * f1 + f2 * f2)); //img_.pixels[(y-1) * img_.width + (x-1)] = color(abs(f1) + abs(f2))); } img_.updatePixels(); return img_; } //------------------------------------ // Laplacian operator PImage laplace(PImage img) { PImage img_ = createImage(img.width - 2, img.height - 2, RGB); img.loadPixels(); img_.loadPixels(); float[][] filter = {{-1, -1, -1}, {-1, 8, -1}, {-1, -1, -1}}; for (int y = 1; y < img.height - 1; y++) for (int x = 1; x < img.width - 1; x++) { float f = 0; for (int ky = -1; ky <= 1; ky++) for (int kx = -1; kx <= 1; kx++) { int index = (y + ky) * img.width + (x + kx); float r = brightness(img.pixels[index]); f += filter[ky+1][kx+1] * r; } img_.pixels[(y - 1) * img_.width + (x - 1)] = color(f); } img_.updatePixels(); return img_; } //------------------------------------ //Sharpening PImage sharpening(PImage img) { PImage img_ = createImage(img.width - 2, img.height - 2, RGB); img.loadPixels(); img_.loadPixels(); float[][] filter = {{-1, -1, -1}, {-1, 9, -1}, {-1, -1, -1}}; for(int y = 1; y < img.height - 1; y++) for (int x = 1; x < img.width - 1; x++) { float f1 = 0; for (int ky = -1; ky <= 1; ky++) for (int kx = -1; kx <= 1; kx++) { int index = (y + ky) * img.width + (x + kx); float r = brightness(img.pixels[index]); f1 = f1 + filter[ky + 1][kx +1] * r; } img_.pixels[(y - 1) * img_.width + (x - 1)] = color(f1); } img_.updatePixels(); return img_; } //------------------------------------ // Computing the magnitude and orientatin of a gradient vector (gx, gy), ver. 1 void gradient_vector1(float gx, float gy) { float mag = sqrt(gx * gx + gy * gy); float theta = 0; if (gx > 0) theta = atan(gy / gx); else if (gx < 0) theta = atan(gy / gx) + PI; else if (gy > 0) theta = PI / 2; else if (gy < 0) theta = - PI / 2; else theta = 0; if (theta < 0) theta += 2 * PI; println("magnitude = " + mag + ", orientation = " + theta); } //------------------------------------ // Computing the magnitude and orientatin of a gradient vector (gx, gy), ver. 2 void gradient_vector2(float gx, float gy) { float mag = sqrt(gx * gx + gy * gy); float theta = 0; theta = atan2(gy, gx); if (gy < 0) theta += 2 * PI; println("magnitude = " + mag + ", orientation = " + theta); } //------------------------------------