It’s advisable that before starting this project, your students and
yourself are all ready familiar with the Art Programming Basics.
Key concepts for this project
- Using external libraries
- Importing video files or the webcam-stream to Processing
- Going through the pixels of the video with two for-loops
- Converting the colors of the pixels.
- Editing the colors and shapes of the pixels to create interesting
results.
Learning Objectives
The student...
- ...can implement previous knowledge acquired from the
Basics-module.
- ...can import video-files or webcam-stream to Processing.
- ...understands the basics of for-loops
- ...can go through each pixel and check its' color
- ...can change the color to grayscale.
- ...can play with different color conversions creatively.
- ...can draw different shapes in the places of the pixels.
Introduction
Loading the Video
As the first step, I'm going to add a
video to the canvas. To load, display and access the pixels of a video in
Processing you need to use a library. A library is a bunch of code that helps
you to do things without first having to write all the code yourself. In this case,
the library tells Processing how to deal with video files, how to read the pixels,
how to display them on the canvas and more. The library you will use is called
Video and it's made by the Processing foundation. To use the library go to the
Sketch-tab and hover over "Import library". In this case the library is already on
my computer. If it's not installed yet, select add library. Type "video" in the
search bar and find the right library made by the Processing foundation. If you click "Install", it will install the library to your computer. Now that the
library is on my computer, I can add it to the project. I go back to sketch and
select "Import library" and Video-library. This creates a line at the top of my
program that says "import processing dot video". Next, I will create a movie object.
This object is part of the video library and will handle all the things related
to the video I'm going to use. Then I want to import the actual video-file to
the project. There's an example video that you can download on this page. I'm
going to use it now. You can also record your own video or even use the webcam-stream if you like. There's more instructions about utilizing the webcam
stream in the next video. Adding the video is similar to adding images. In the
Sketch-menu, press "Add file". This gives you a dialog where you can select the
right video file. This creates a data folder in the same folder as the project
and copies the video file there. To also load the video into our project, you need
to load it into the movie-object. Let's do
this in setup. When creating movie objects you have to add the word this in
the parentheses. It's a reference to the current program you are making. After
that there's the name of the video file. After loading the video file I want to
set the size of the canvas to the size of the video. Iin Windows, you can see the
size by clicking the video with the right mouse button and selecting
Properties and "Details". This gives you the video width and video height. On a Mac,
the size of the video is displayed as dimensions when right-clicking the video
in Finder and selecting "More Info". I'm going to set the video to loop with the
loop-command. This will start playing the video and keep looping it as long as the
program is running. The first thing I need to do in the draw function is to
check whether the video is ready and Processing can use it. Without this there
might be all kinds of crashes, bugs and other issues. I have to update the video
every frame using the read-command. This command will keep track of what frame to
show. Now it's time to draw the video to the canvas. Video is just the series of
images shown very fast after each other and in Processing, it's possible to draw
the frames of the video with the image -command. It works! Now there's an unmodified video playing on the canvas So, the read-command always
gets the next frame of the video and the image-command draws it. Now that the video is there, I can start playing around with the pixels!
Install the video-library as instructed in the video.
Film a video yourself and send it to your computer or use the
example-video used in these tutorials. You can download the example
video here.
Add your video to the canvas. There's example code below.
import processing.video.*;
Movie myMovie;
void setup() {
size(640, 480);
myMovie = new Movie(this, "example.mp4");
myMovie.loop();
}
void draw() {
if (myMovie.available()) {
myMovie.read();
myMovie.loadPixels();
}
// Original video in the top left corner
image(myMovie, 0, 0);
}
What is a library in the context of coding?
Using the Webcam
If you want, you can continue with this project using your webcam-stream instead of using a video-file.
The code in the video is provided below.
import processing.video.*;
Capture cam;
void setup() {
size(640, 480);
String[] cameras = Capture.list();
if (cameras.length == 0) {
println("No cameras found.");
exit();
}
else {
cam = new Capture(this, cameras[0]);
cam.start();
}
}
void draw() {
if (cam.available()) {
cam.read();
image(cam, 0, 0, width, height);
}
}
Access the Pixels
Create the two for-loops that go through the pixels of the video.
There’s more info about for-loops here and in the topic Array Storm in the fourth chapter of the basics-module.
Change the colors of the pixels like instructed in the video. Create a new version of the video with altered colors and draw it next to the original video. There’s some ideas for color conversions below.
// This is the example conversion in the video.
// Amount of red in the original color is used as the new color directly.
// Try using green and blue -commands instead of the red-command.
color newColor = red(pixelColor);
// Amount of blue in the original color will be the amount of red in the new color.
// Similarly, green will become red and blue will become green.
color newColor = color(blue(pixelColor), red(pixelColor), green(pixelColor));
// Try out different mathematical operations for the components of the color!
color newColor = color(red(pixelColor)/2, green(pixelColor) * 2, blue(pixelColor) + 40);
color newColor = red(pixelColor)
What does the line of code above mean?
color newColor = color(green(pixelColor), blue(pixelColor), red(pixelColor))
Try the color conversion above. What happens?
Tinker the Colors
Try out some more color conversions! There’s examples below.
// The grayscale-conversion showed in the video.
// Each component of the color is multiplied by 0.33 to get an average grayscale-color of the pixel.
color newColor = color(red(pixelColor) * 0.33 + green(pixelColor) * 0.33 + blue(pixelColor) * 0.33);
// The example conversion showed in the video.
// Components of the rgb-color change places.
color newColor = color(blue(pixelColor), red(pixelColor), green(pixelColor));
// If the color is red enough, it is displayed normally. Otherwise, it is displayed as black.
// Try altering the threshold value and checking the green and blue components instead of checking the red-component.
color newColor;
if(red(pixelColor) > 200){
newColor = pixelColor;
}else {
newColor = color(0);
}
color newColor = color(red(pixelColor) * 1.2, green(pixelColor) * 0.8, blue(pixelColor) * 0.8);
What does the color conversion above do?
Rectangular Pixels
Follow the instructions in the video and create two new versions of the original video.
- Try out different colors and shapes for the pixels.
- Try out different values for the variable stepSize. What is the highest possible stepSize you can use while still understanding what’s happening in the video?
- In the video, ellipses and rectangles are used in the place of the pixels. Try triangles instead!
See the code-example in the video. Which of the following statements is true?
Follow the instructions in the video and create a new version of the original video.
- First, make the size of the pixels random.
- Then, make the size of the pixels change more smoothly with the
noise-command
- Make some experiments with the noise-command. Change the
decimal-multipliers of the parameters. There's some examples below.
EXTRA
// This is the example in the video.
float circleSize = noise(0.02 * x, 0.02 * y, timeCounter) * 30;
// Change the decimal multipliers to get different results.
// The more the parameters of the noise-function vary, the more noise there will be.
// Lower multipliers make the change smoother
float circleSize = noise(0.01 * x, 0.04 * y, timeCounter) * 30;
// You can use noise with two parameters too.
float circleSize = noise(0.02 * x, timeCounter) * 30;
// You can use noise with only one parameter too!
// If you use x or y as the only parameter, the result is static.
// That is because x and y go always go through the same values.
float circleSize = noise(timeCounter) * 30;
Follow the instructions in the video and create a version with flowing colors.
Make some experiments by affecting the components of the colors with the noise-command.. There’s some examples below.
EXTRA
If you’re eager to know more about noise, check this video made by Daniel Shiffman!
// This is the example in the video. noise is used with two parameters.
float noiseRed = noise(red(pixelColor) * 0.02, timeCounter) * 255;
float noiseGreen = noise(green(pixelColor) * 0.02, timeCounter) * 255;
float noiseBlue = noise(blue(pixelColor) * 0.02, timeCounter) * 255;
// Use the brightness, hue and saturation -commands to get interesting results.
// Now, saturation of the color affects the amount of red, hue affects the amount of green and brightness affects the amount of blue.
float noiseRed = noise(saturation(pixelColor) * 0.02, timeCounter) * 255;
float noiseGreen = noise(hue(pixelColor) * 0.02, timeCounter) * 255;
float noiseBlue = noise(brightness(pixelColor) * 0.02, timeCounter) * 255;
Summary
Learn More
Transparency of Pixels
A RGB-color has three components, and you can use a fourth one to
determine the opacity of the color. Try making some pixels partly
transparent. You can e.g. use the amount of red as the opacity.
Saturation, Hue and Brightness
Saturation, hue and brigtness. It's possible to check the hue,
brightness and saturation of the color in Processing. Like all other
color-values, these are numbers between 0 and 255 so you can use them as
color-values directly.
color newColor = color(brightness(pixelColor), hue(pixelColor), saturation(pixelColor));
Motion detection?
Check this tutorial by Daniel Shiffman:
https://www.youtube.com/watch?v=QLHMtE5XsMs
How noise works?
Check these examples in Processing.org about 1D, 2D and 3D noise.
https://processing.org/examples/noise1d.html
https://processing.org/examples/noise2d.html
https://processing.org/examples/noise3d.html
What is a pixel-array?
In this project you used get-command to get the color of a pixel.
get-command needs two parameters, the x- and the y-coordinate of the
pixel.
color pixelColor = myMovie.get[x, y];
Although we see the pixels as a nice rectangle with the x- and y-axis,
to the computer pixels form a long one-dimensional array. The computer
creates this array automatically when you start to process the pixels.
If you continue with pixel processing in the future, you probably
encounter projects that use the pixel-array rather than something like
the get-command. So understanding the array may be useful!
Array is just a long one-dimensional list of things so you can’t add the
x- and y-coordinates inside the brackets, like you did with the
get-command. Instead, you have to convert the x and y coordinates to a
position in the array. Luckily, the calculation isn’t that hard.
color pixelColor = myMovie.pixels[(y*myMovie.width) + x];
For every time we’ve gone through all the pixels on the x-axis, we’ve
increased y by one. This means that if we multiply y by the width of the
video, we’ve got the number of pixels of all the full rows we’ve gone
through already. Then we add the current x position in the loop to get
to the position we are at right now.
Check this tutorial about pixels and arrays at Processing.org
If you want to know more about arrays, check the fourth chapter of the Art Programming Basics -module!