/* Experimentation de filtre video style 'polka dots' base sur le sketch Exorciseur…
- User
/* Experimentation de filtre video style 'polka dots' base sur le sketch Exorciseur Elliptique par Martin Fabien Lavoie, 2023 */
import processing.video.; import controlP5.;
//Declaration de l'objet pour la librairie de UI ControlP5 cp5;
//Declaration de l'objet Capture pour la capture via webcam Capture cam;
int canvasWidth; int canvasHeight; int numPixels;
int dotsPerFrame = 1000; int dotSize = 10;
//Changer l'index uniquement si tu veux utiliser une camera autre que la premiere dans la liste int cameraIndex = 0;
void setup() {
size(640, 480); background(0, 0, 0); frameRate(30);
//Settings pour les sliders, voir la documentation de la librairie au https://www.sojamo.de/libraries/controlP5/#about cp5 = new ControlP5(this); cp5.addSlider("dotsPerFrame").setPosition(10, 10).setRange(0, 10000).setSize(30, 600); cp5.getController("dotsPerFrame").getCaptionLabel().align(ControlP5.RIGHT, 200); cp5.getController("dotsPerFrame").getValueLabel().align(0, -600); cp5.addSlider("dotSize").setPosition(50, 10).setRange(0, 50).setSize(30, 600); cp5.getController("dotSize").getCaptionLabel().align(ControlP5.RIGHT, 200); cp5.getController("dotSize").getValueLabel().align(0, -600);
//On va chercher la liste des cameras disponible sur le systeme. La librairie Capture s'integre directement au OS, donc normalement c'est plug and play String[] cameras = Capture.list();
//On verifie si il y a au moins un element dans la liste if (cameras.length == 0) { println("Aucunes cameras de disponible pour capture."); exit(); } else { //Si il y a au moins une cameras on imprime la liste... println("Cameras disponibles:"); for (int i = 0; i < cameras.length; i++) { println(cameras[i]); } //...puis on cree un nouvel objet Capture en utilisant une des cameras disponible (selon la variable cameraIndex) //cam = new Capture(this, 1920, 1080, cameras[cameraIndex]); cam = new Capture(this, "pipeline:autovideosrc"); cam.start(); setupOverlay(width, height); }
//Etant donne qu'on fonctionne directement avec un array de pixels et non une PImage, on doit adresser les pixels via leur index dans l'array. //On doit donc savoir exactement le nombre de pixels, ainsi que la largeur et hauteur de l'image canvasWidth = cam.width; canvasHeight = cam.height; numPixels = canvasWidth * canvasHeight; }
void draw() { if (cam.available() == true) { cam.read(); //La fonction loadPixels() permet d'adresser/modifier directement les pixels dans le frame-buffer de l'objet Capture. cam.loadPixels();
drawDots(); //Une fois nos operations faites, on doit liberer le frame-buffer avec updatePixels() cam.updatePixels(); drawOverlay(width, height);} }
void drawDots() { for (int i = 0; i < dotsPerFrame; i++) { //Selection au hasard d'un pixel int x = int(random(canvasWidth)); int y = int(random(canvasHeight));
//Les arrays de pixels dans Processing sont des representations uni-dimentionelles d'une image deux-dimentionelle. //Les pixels sont place dans l'array en ordre, de gauche a droite, puis de haut en bas. //C'est a dire, le premier pixel de l'array est le pixel le plus en haut a gauche de l'image, et le dernier pixel de l'array est celui en bas a droite. //Donc pour "descendre" d'une position en y, on doit "avancer" dans l'array du nombre d'elements equivalent a la largeur de notre image. C'est plus facile a visualiser avec l'exemple ici; https://processing.org/tutorials/pixels //Pour transformer une coordonnee en index la formule est donc la suivante; int index = (y*canvasWidth) + x; //Ensuite on vas chercher la couleur du pixel a l'index, et on dessine un dot de la couleur correspondante color dotColor = cam.pixels[index]; stroke(dotColor); fill(dotColor); square(x, y, dotSize);} }
void keyPressed() { if (keyPressed && keyCode == 72) { cp5.hide(); } if (keyPressed && keyCode == 83) { cp5.show(); } } current code is this. I want the overlay to also get drawn by drawDots and I want drawDots to use grayscale instead of colors
- Assistant
To add the overlay to the drawDots function and use grayscale instead of colors, you can modify your code as follows:
- Declare variables for the grayscale value and the opacity of the overlay at the top of your code:
float overlayOpacity = 100; int overlayGrayscale = 255;- In the drawDots function, after drawing the dot, draw a semi-transparent rectangle with the overlay color on top of it:
fill(overlayGrayscale, overlayOpacity); rect(x, y, dotSize, dotSize);- Modify the drawOverlay function to include the overlayGrayscale variable:
void drawOverlay(int width, int height) { fill(overlayGrayscale, overlayOpacity); rect(0, 0, width, height); }- Change the dotColor variable to a grayscale value using the red() function in the drawDots function:
color dotColor = color(red(cam.pixels[index]));- Remove the stroke(dotColor) line if you don't want an outline around the dots.
The modified drawDots and drawOverlay functions should look like this:
void drawDots() { for (int i = 0; i < dotsPerFrame; i++) { int x = int(random(canvasWidth)); int y = int(random(canvasHeight)); int index = (y*canvasWidth) + x; color dotColor = color(red(cam.pixels[index])); fill(dotColor); square(x, y, dotSize); fill(overlayGrayscale, overlayOpacity); rect(x, y, dotSize, dotSize); } } void drawOverlay(int width, int height) { fill(overlayGrayscale, overlayOpacity); rect(0, 0, width, height); }With these modifications, the dots drawn by the drawDots function will be grayscale and set the same overlay color and opacity as the dots.