Session 3 · Find a flashlight, add a pattern
Find a flashlight, add a pattern
These supplementary notes let you redo the flashlight exercise from Session 3 (Sep 5) at home, one step at a time. Each step has its own file, so you can see where each new piece comes in. Open them in order from Step 1 and you'll see what gets added where.
All you need is a flashlight. A phone flashlight works.
Check first
- Installing Processing 4 and the libraries is covered in Installing Processing. Of the four libraries, this exercise uses Video and BlobDetection.
- If you just installed the libraries, quit Processing and open it again. Libraries installed while it is open are not recognized.
- On macOS, the first run asks whether the sketch may use the camera. Click Allow.
Download the files
One file per step.
- Step 1 · Open the webcam
- Step 2 · Outline the bright areas
- Step 3 · Keep only the flashlight
- Step 4 · A pattern where it's found
- Step 5 · Make it yours
When you double-click a downloaded file, Processing asks whether to move it into a folder with the same name. Click Yes and it sorts things out.
You can also take everything from the repository. tools/processing has the same files and notes. If you already forked the repository, click Sync fork on GitHub to see the new folder.
Step 1 · Open the webcam
Open Step1Webcam and press ▶, and your face appears in the window. This was the first stage of the exercise.
If the camera doesn't open, look at the black area at the bottom (the console). It lists the available cameras by number and name. To use a camera other than the laptop's built-in one, put its name in as the comment in setup() describes.
Step 2 · Outline the bright areas
Step2Blobs finds bright patches on screen and draws green outlines around them. This is the screen you saw when the BlobDetection example was opened in class.
It shrinks the camera image to 160x120 before searching, because going through the full 640x480 is slow. The reduced size is set by BLOB_W and BLOB_H near the top of the code.
For now, your face and the windows get picked up too. The next step keeps only the flashlight.
Step 3 · Raise the threshold to keep only the flashlight
The threshold is the rule "only look at things brighter than this." In the code it is threshold, and while the sketch runs the Up and Down arrow keys change it by 0.02.
Turn on the flashlight and raise the value with the arrow key, watching other things disappear one by one. The value that leaves only the flashlight depends on how bright the room is. That's why the number isn't fixed in the code and you find it with the keys instead.
Press B and a small black-and-white image appears in the top right. It is the image the detector is actually looking at, so you can see what gets erased as you raise the threshold. If small specks keep getting picked up, raise the minimum size with [ ].
Step 4 · A pattern where it's found
Step4Pattern picks the largest detected blob and draws concentric circles and lines there. Move the flashlight and the pattern follows.
The drawing code is all inside drawPattern(). To change the pattern, you only need to edit inside this function. You don't need to touch the rest.
C clears the trail, and V turns off the camera image so only the drawing remains. How fast the trail slowly fades is the number in trail.fill(0, 18). Set it to 0 and the trail keeps building up.
Step 5 · Make it yours
Switch what it looks for: 1 bright things, 2 dark things, 3 a chosen color. In mode 3, click on the screen to target the color at that spot, and use + - to set how loosely it matches.
P cycles through three patterns. Edit prepareSmall() to change the detecting side and drawPattern() to change the drawing side. The two don't know about each other, so you can change one and the other keeps working.
Going on to movement
- OpenCVCamDemo Use number keys 1 to 8 to switch between grayscale, edges, thresholding, contours, faces, Optical Flow and background subtraction. You can see for yourself what the terms you only heard in passing look like. Requires the OpenCV for Processing library.
- OpticalFlow The concept covered in class. It shows, with vectors and colors, which parts of the screen moved in which direction and by how much.
When it doesn't work
The package "blobDetection" does not exist→ The library isn't installed yet. Install BlobDetection from Manage Libraries, then quit and reopen Processing.- It stops at "waiting for camera frame" → Check whether another app is using the camera. An open video call window won't let go of the camera.
- Nothing gets detected → The threshold is too high. Lower it with the down arrow and watch. If the room is bright and even the walls get detected, raise it.
- Any other red text → Copy the console message as it is, paste it into Cursor, and ask what to fix. You can also ask on KakaoTalk with one screenshot and one line about what you were doing when it happened.
Where this comes from
Session 3 was taught by artist Jeongseop Lee. The step-by-step sketches and explanations here were written separately by Joonhyung Bae after sitting in on the class. The example files Jeongseop Lee used in class are not included.
Finding bright patches is done by the BlobDetection library (Julien 'v3ga' Gachadoat). OpenCVCamDemo and OpticalFlow use OpenCV for Processing (Greg Borenstein, Florian Bruggisser).