Idle Raspberry Pi: Plant a flower and do a time-lapse!
Preparation
First of all, let's bring out the main characters: an unused Raspberry Pi, camera, and of course, flower seeds and pots.
Considering that the Raspberry Pi needs to be on for a long time, it's better to have a cooling fan,https://rockbee.cc/collections/raspberry:
For the camera I used a very common pi camera V2.1.
Build a stand and adjust the focus
After that, you need to figure out how to fix the camera in a suitable position. pi Camera has small round holes around it for fixing, plus a cell phone stand can basically fix it. Taobao also sells them, but do not recommend buying them (because it really does not help). Ensure that the camera angle is clear and stable is the most important. Mine looks like this:
After that you need to adjust the focus of the camera. pi camera is a fixed focus camera, theoretically it can't be adjusted, but the good thing is that the lens is screwed on during the production, which gives us room to maneuver.
Take a pair of tweezers and rotate them against the cross section on the lens. Rotate it to the right to get a smaller focal length, half a turn is basically about right. You can try more depending on your distance
Sharpness change before vs after rotation
After all this is done, it's time to set up the Raspberry Pi. There are a few things to consider when choosing a location:
- Power: Note that the Raspberry Pi has no batteries, so it needs a regulated power supply to power it. If the potted plant is chosen in a location with no power supply, you can only consider multiple rechargeables to take turns taking over, which is more troublesome. Also, the voltage of rechargeable batteries is usually a bit lower than the rated 2.5A, so accidents may happen.
- Heat dissipation. As mentioned earlier it is best to have a whole fan. If the temperature is too high it may fail to take pictures. (Use cat /sys/class/thermal/thermal_zone0/temp to check the temperature)
- Waterproof. This is a no-brainer, just cover it with a plastic sheet.
- Other than that it's best to put it in a place with a Wi-Fi link, otherwise you'll have to connect the cable every time you configure it, and you might accidentally knock the camera out of alignment.
Raspberry Pi Configuration
The next step starts to get into the swing of things. Delayed Photography is really about shooting some video with a relatively low frame rate. We can just take a lot of pictures and splice them together manually:
First of all, the task of taking the photos can be done using a single line of the raspistill command, while the timed task part can be done with crontab. After you get the photos, you can then use opencv or directly use ffmpeg to put the pictures together. The idea is relatively simple, let's start with the most basic part:
1 First of all, you need to ensure that the Raspberry Pi camera can work properly:
Insert the Pi camera cable into the slot of the Raspberry Pi, and then press the snap button. (Note that it is best to do this with the power off, otherwise the camera may not be recognized).
Then connect to the Raspberry Pi remotely, enter sudo raspi-config to enter the settings, find the interface options
Then enable camera.
After doing this string, you can test if your camera is fully ready by using the command raspistill -v -o test.jpg -ex auto. If it works, you should find a test.jpg file under userspace.
- 2 Write a shooting script.
In theory, you don't need to write a script at all, you can just use the raspistill command to do it. The advantage of using a script is that you can easily control the shooting time and file name. Generally speaking, it is enough to consider using jpg format for time-lapse photography, otherwise it will be more stressful when compositing video.
#!/bin/bash DATE=$(date +"%m-%d_%H%M")
|
After saving the script, remember to create a new path where the photos will be stored: mkdir /home/pi/flower_pics
- 3 Modify permissions
- 4 Configure the crontab.
If you're shooting video of flowering, you can generally choose 20s-60s intervals and shoot for 2-3 hours, or a little longer if you're just documenting plant growth. I shot my video over a period of about three weeks, every 20 minutes, and the final video was about 25s (taking into account that I deleted the nighttime photos, and the occasional scraps of poorly lit images). If the time is more ****y and drastic, like it has to be every 40 or 50 minutes, in this case you can make a separate judgment inside the script with an if conditional statement.
Input crontab -e , in the corresponding file to enter their own execution time, and the location of the script file: 0,20,40 * * * * * sh /home/pi/scripts/take_photo.sh 2>&1
After all the adjustments, you can lay back and watch the cilantro slowly grow~
Adjusting & Compositing Photos
Simply stitching pictures together is relatively simple. Use ffmpeg or directly on pr can be realized. But in order to consider the quality of the video, you need to do a little simple processing before synthesizing the video.
Above is the image I came up with. While the clarity is okay, a big problem is that the exposure level of the image varies too much. School sb dormitory only have direct sunlight in the morning, resulting in the afternoon and evening cilantro are some different degrees of underexposure, several pictures directly stitched together will not be very comfortable, so need to rely on the post, to the afternoon and evening photos to fill the light.
Considering the need for batch processing of pictures at a particular time, it is more convenient to use opencv to operate down in one line.
Enhance the brightness, on the one hand, enhance the contrast, on the other hand, consider the overall brightness of the picture, so the main consideration of gamma change and normalization. You can see the effect of their own decision.
Gamma transform
The gamma transform is a good choice. It is a non-linear transformation of the brightness of the input image, which mainly improves the details of the dark areas, and the result is more natural than directly increasing the brightness.
S=Cry,r∈[0,1]
gamma = 0.65
gamma_table=[np.power(x/255.0,gamma)*255.0 for x in range(256)]
gamma_table=np.round(np.array(gamma_table)).astype(np.uint8)
img = cv2.LUT(img,gamma_table)
Look at the effect:gamma = 0.65
- Normalization
The implementation requires just one line of code:
img = cv2.normalize(img,dst=None,alpha=255,beta=30,norm_type=cv2.NORM_MINMAX)
Where NORM_MINMAX indicates the type of normalization to use, and alpha and beta are the maximum and minimum values of the normalized values, i.e. max', min', respectively.
x'=x-minmax-min*(max'-min')+min'
Besides NORM_MINMAX, you can also choose Chebyshev distance (cv2.NORM_INF)/Euclidean distance (cv2.NORM_L2)/Manhattan distance (cv2.NORM_L1) as the normalized distance calculation.
Normalization adjusts the result more naturally, but the disadvantage is also obvious: there is no way to completely change the exposure of the picture.
min_max normalization: max = 250,min = 20
The full code is here:https://github.com/misra0514/imageProcessor
Once you get the video it's pretty simple. Open pr with a piece of music and you're done!
Originally written by: maomaozi, Compiled by: RockBee