Raspberry Pi Developer's Guide: Camera

Camera


2D數位攝影機

2D數位攝影機有兩種類型,即電荷耦合元件(CCD),以及互補金屬氧化物半導體(CMOS)。相較於CMOS攝影機,CCD攝影機的解析度更高且雜訊較低,但價格較高且能效較低。
  • 優點
  • 2D攝影機可有效地辨識物體。它可以清晰地定義目標的邊緣,並以高解析度收集目標的許多細節。
  • 缺點
  • 2D攝影機對於環境中的光照條件比較敏感。它會將鏡像影像辨識為第二個物體,而不是影像。最後,2D相機的影像捕捉過程較慢,需要攝影機和物體相對靜止。

3D感測技術


3D感測資料可以透過不同的測量過程產生,如立體視覺、結構光和雷射三角測量。立體視覺採用三角測量原理,使用來自同一位置多個2D攝影機的影像,或來自不同位置的單個攝影機影像,以計算深度。
結構光非常適合測量物體的3D形狀,它使用投影光模式和2D攝影機系統進行測量。透過分析所觀察到的條紋圖案,可以收集有關物體深度的大量資訊。

雷射三角測量則使用2D攝影機和雷射。雷射投射到目標區域上,然後2D攝影機捕捉反射的雷射光束。根據多張照片上的雷射光束彎曲程度,就可以計算出物體的輪廓。但是,這種方法速度相對較慢,而且只能用於靜止影像,這意味著必須中斷某個進程才能進行資料收集。
  • 優點
  • 3D感測技術在近距離測量物體時具有很高的精確度,比超音波和紅外線解析度更高,而且在環境混亂時更加可靠。特別是在難以操控的表面或在環境光較暗的情況下,雷射三角測量也可以工作,即使是低對比度的物體,它也可以提供精確的資料。
  • 缺點
  • 3D感測技術無法辨識鏡面或玻璃,因為它會將反射物辨識為第二個物體。此外,它們對光線條件敏感,在環境過於單調且缺乏特性時表現不佳。此外,其感應速度很慢,只能用於感測靜止影像,這意味著必須中斷某個進程才能進行資料收集。最後,採用3D感測的攝影機價格昂貴、體積龐大且較難校準。

超音波技術


超音波攝影機也稱為聲納攝影機,它透過測量聲波在發射和接收之間的時間差,以測量攝影機與物體之間的距離。超音波攝影機還可以偵測到附近的其它超音波感測器或是其它帶有超音波感測器的機器人。
  • 優點
  • 相較於其它類型的感測器,超音波攝影機的感測範圍更長也更寬廣。它們不受光照條件(太亮或太暗)或空氣中微粒(如灰塵、煙霧、薄霧、蒸氣和絨毛)的影響。而且,它們不至於受到物體的顏色、透明度、反射特性或表面紋理的阻礙。因此,超音波攝影機非常適合測量液體水位和距離,檢測物體,並且可避免在陸地或水下發生碰撞。
  • 缺點
  • 超音波攝影機在定義目標的邊緣方面表現不佳。它在感測具有高密度和反射表面的目標時效果很好,但不適用於泡沫或布料等低密度物質。超音波攝影機還會受高雜訊、溫度、濕度和壓力的影響。最後,它要求盡可能小的感測距離,但又無法檢測距離太近的物體。

紅外線技術


紅外線感測器透過檢測物體發出的紅外線(IR)運作。它還可以向物體投射IR光並接收反射光,從而計算目標物體的距離或接近程度。
  • 優點
  • 紅外線感測器可以偵測大範圍的紅外光、即時運行且極具成本效益。相較於超音波感測器,紅外線感測器在定義物體的邊緣時表現優越,而且有助於辨識不同的物體。例如,它們可以辨識人和其它具有熱量的物體,並與其反射影像(例如鏡子)區分開來。此外,IR攝影機能夠在煙塵或霧氣等能見度低的條件下進行感測。如同超音波攝影機一樣,IR攝影機還可以檢測水下材料,目前已經應用於檢測建築物的隔熱材料或滲漏。
  • 缺點
  • IR感測器對於紅外光和陽光非常敏感,只能在低光下使用。而且,IR感測器對於不同材料的感測效果也不盡相同,因為不同材料對紅外光的傳遞和折射均不同。

Designer’s guide to robot vision cameras

根據特性的不同,每一種感測器類型可能更適合某些任務,而不適合其他任務。
  • 攝影機選擇流程圖
  • 各種類型的攝影機特性比較
  • 市場上主要的機器人視覺攝影機供應商


Raspberry Pi Camera

Connect the Camera Module


First of all, with the Pi switched off, you'll need to connect the Camera Module to the Raspberry Pi's camera port, then start up the Pi and ensure the software is enabled.
  • Locate the camera serial interface (CSI) port and connect the camera
  • The Rapsi camera connects directly to the GPU, and is capable of 1080p30 video encode, 5MP stills in pretty decent quality. Because its attached to the GPU, there is only a little impact on the CPU, leaving it available for other processing. Moving data over the USB to the CPU takes a fair bit of CPU, with the Pi camera, you don't have that overhead so you have more CPU available.
    • The flex cable inserts into the connector situated between the Ethernet and HDMI ports, with the silver connectors facing the HDMI port. The top part of the connector should then be pushed towards the HDMI connector and down, while the flex cable is held in place.
    • he flex cable connector should be opened by pulling the tabs on the top of the connector upwards then towards the Ethernet port.
  • Start up the Pi
  • Open the Raspberry Pi Configuration Tool from the main menu:
  • "Preferences" -- "Raspberry Pi Configuration"
  • Ensure the camera software is enabled


If it's not enabled, enable it and reboot your Pi to begin.
Now your camera is connected, you can try some software.

Python PICAMERA

python-picamera is a pure Python interface to the Raspberry Pi camera module for Python 2.7 (or above) or Python 3.2 (or above).

INSTALLATION


The python-picamera library is available in the Raspbian archives. Install with apt:

sudo apt-get update
sudo apt-get install python-picamera


Alternatively, the Python3 package is installed with

sudo apt-get install python3-picamera

An offline version of the documentation is available with

  sudo apt-get install python-picamera-docs



USAGE


First, at the Python prompt or at the top of a Python script, enter:

import picamera


This will make the library available to the script. Now create an instance of the PiCamera class:

camera = picamera.PiCamera()


And take a picture:

camera.capture('image.jpg')


Besides, you can use the utility raspistill to capture a still picture:

raspistill -w 640 -h 480 -q 5 -tl 100 -t 60 -th 0:0:0 -n -o cam.jpg

where
  • -w
  • Picture width. 1920 for HD.
  • -h
  • Picture height. 1080 for HD.
  • -q
  • JPEG compression quality. 100 is for the best picture quality.
  • -tl
  • Time lapse between pictures in mimi-seconds.
  • -t
  • Time for execution.
  • -n
  • No preview window
  • -o
  • Output filename

HORIZONTAL AND VERTICAL FLIP

You can apply a horizontal and vertical flip if your camera is positioned upside-down. This is done by changing the hflip and vflip properties directly:

camera.hflip = True
camera.vflip = True

Be sure to use an upper case T in True as this is a keyword in Python.



PREVIEW


You can display a preview showing the camera feed on screen. Warning: this will overlay your Python session by default; if you have trouble stopping the preview, simply pressing Ctrl+D to terminate the Python session is usually enough to restore the display:

camera.start_preview()

You can use the stop_preview method to remove the preview overlay and restore the display:

camera.stop_preview()

Alternatively, you can access the Pi using SSH from another computer, open a Python prompt and enter these commands, displaying the preview on the monitor connected to the Pi (not the computer you're connected from).

CAMERA SETTINGS


You can change other camera configuration by editing property values, for example:

camera.brightness = 70

This will change the brightness setting from its default 50 to 70 (values between 0 and 100).

Other settings are available. Here is a list with their default values:

camera.sharpness = 0
camera.contrast = 0
camera.brightness = 50
camera.saturation = 0
camera.ISO = 0
camera.video_stabilization = False
camera.exposure_compensation = 0
camera.exposure_mode = 'auto'
camera.meter_mode = 'average'
camera.awb_mode = 'auto'
camera.image_effect = 'none'
camera.color_effects = None
camera.rotation = 0
camera.hflip = False
camera.vflip = False
camera.crop = (0.0, 0.0, 1.0, 1.0)


SLEEP

You can add pauses between commands using sleep from the time module:

import picamera
from time import sleep

camera = picamera.PiCamera()

camera.capture('image1.jpg')
sleep(5)
camera.capture('image2.jpg')


You can also use sleep in a preview to adjust settings over time:

camera.start_preview()

for i in range(100):
    camera.brightness = i
    sleep(0.2)


VIDEO RECORDING


Record 5 seconds of video:

camera.start_recording('video.h264')
sleep(5)
camera.stop_recording()


Besides, you can use the utility raspivid to record a clip:

raspivid -o video.264 -t 10000

This will record a video for 10 seconds.

DOCUMENTATION


Full documentation for python-picamera is available at picamera.readthedocs.org

DEVELOPMENT


The python-picamera project is written and maintained by Dave Jones and the source can be found at github.com/waveform80/picamera where you can open issues or contribute to the project.


Web Camera


Java Script

setTimeout(function, milliseconds, param1, param2, ...)

The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds.

Tip: 1000 ms = 1 second.
Tip: The function is only executed once. If you need to repeat execution, use the setInterval() method.
Tip: Use the clearTimeout() method to prevent the function from running.

  • function
  • Required. The function that will be executed
  • milliseconds
  • Optional. The number of milliseconds to wait before executing the code. If omitted, the value 0 is used
  • param1, param2, ...
  • Optional. Additional parameters to pass to the function (Not supported in IE9 and earlier)

MJPEG Streamer


Install packages


sudo apt-get update
sudo apt-get install subversion
sudo apt-get install libjpeg8-dev
sudo apt-get install imagemagick 
sudo apt-get install libv4l-dev


Download and build the source code


svn co https://svn.code.sf.net/p/mjpg-streamer/code MJPG-streamer
cd MJPG-streamer/mjpg-streamer
make USE_LIBV4L2=true clean
make DESTDIR=/usr install


Setting


Load the V4L2 driver for Raspberry


You can access the camera board on /dev/video0 by running the command:

sudo modprobe bcm2835-v4l2

This will have to be run on every boot of the device.
Or you can put modprobe bcm2835-v4l2 in /etc/rc.local to make it run on every boot automatically.

Test


  • Basic
  • From the build folder,
    
    mjpg_streamer -i "/usr/lib/input_uvc.so" -o "/usr/lib/output_http.so -w ./www"
    
    
    Test it on local browser: http://localhost:8080. You will see the default Web page provided by MJPG-streamer.
  • Advanced
  • 
     mjpg_streamer -i "/usr/lib/input_uvc.so -d /dev/video0 -r 640x480 -f 30 -y -n" -o "/usr/lib/output_http.so -p 8090 -w /home/pi/MJPG-streamer/mjpg-streamer/www"
    
    
    where:
    • Input modifiers
      • -i specifies the input component "input_uvc.so". "input_file.so" is used to monitor a folder and stream files under that folder.
      • -d specifies the device
      • -r is resolution
      • -f is frame rate, in number of frame per second
      • -y specifies YUYV format, rather than MJPEG format.
      • -n
      • This is necessary when you meet the following error messages:
        
        Adding control for Pan (relative)
        UVCIOC_CTRL_ADD - Error: Inappropriate ioctl for device
        
        
    • Output modifiers
    • -o specifies the output component "output_http.so" which enables the light HTTP server provided by MJPG-streamer. -p is port -w is root folder of the Web serving . See the documentation for more details.

3w大功率樹莓派夜視攝像頭專用紅外補光燈


Features


  • 3 watt output infrared (850 nm) infrared LED
  • 3W大功率紅外燈
  • Onboard photoresistor, the ambient light detector
  • Onboard adjustable resistor, for controlling the ambient light threshold of toggling the infrared LED
  • 自帶可調電阻和光敏電阻,可用小螺絲刀輕輕扭動調節環境亮度臨界值, 根據所設定的環境光線臨界值自動切換1W大功率LED燈
  • Allows adding night vision function to RPi Camera (E) or RPi Camera (F)
NOTE: they are NOT completely compatible with the latest Raspberry Pi Camera boards (no power via mounting points)

How to Use

  • Connect the Infrared LED Board to the camera PCB by screws
  • The screw holes are used for both attachment and power supply
  • Adjust the adjustable resistor to set a proper ambient light threshold, which toggles the infrared LED automatically
  • When ambient light is lower than threshold value, the infrared LED is on, vice versa

Note

  • Limited by the Raspberry Pi power supply, the 3W high-power infrared LED might cause the Pi to increase heating, heat sinks are suggested.
  • Ideal imaging distance is within 3m. When the distance goes up to 6m, the night vision camera can just capture blurry outlines.


OpenCV(Open Source Computer Vision)-Python Tutorials


Introduction to OpenCV

Learn how to setup OpenCV-Python on your computer!

OpenCV

Currently OpenCV supports a wide variety of programming languages like C++, Python, Java etc and is available on different platforms including Windows, Linux, OS X, Android, iOS etc.
OpenCV-Python is the Python API of OpenCV. It combines the best qualities of OpenCV C++ API and Python language.

OpenCV-Python

Compared to other languages like C/C++, Python is slower. But another important feature of Python is that it can be easily extended with C/C++. This feature helps us to write computationally intensive codes in C/C++ and create a Python wrapper for it so that we can use these wrappers as Python modules.
This is how OpenCV-Python works, it is a Python wrapper around original C++ implementation.

Install OpenCV-Python in Raspbian


OpenCV-Python can be installed in Raspbian in two ways,
  • Install from pre-built binaries available in fedora repositories
  • 
    sudo apt-get install libopencv-dev 
    
  • Compile from the source
    • Install necessary packages
    • 
      sudo apt-get install build-essential cmake cmake-curses-gui pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools libeigen3-dev
      
      sudo apt-get install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs ffmpeg libavcodec-dev libavcodec53 libavformat53 libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev swig libv4l-0 libv4l-dev python-numpy libpython2.6 python-dev python2.6-dev libgtk2.0-dev
      
      
    • Download source code
    • Download the source or via git:
      
      git clone https://github.com/Itseez/opencv.git
      
      
    • Build
    • 
      cd opencv-2.4.10
      mkdir release
      cd release
      ccmake ../
      make
      sudo make install
      
      
    Test the installation:
    • Example cose
    • 
      #include 
      #include 
      #include 
      using namespace cv;
      using namespace std;
      int main( int argc, char** argv ) {
          if( argc != 2) {
           cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
           return -1;
          }
          Mat image;
          // Read the file
          image = imread(argv[1], CV_LOAD_IMAGE_COLOR);
          if(! image.data ) { // Check for invalid input
              cout <<  "Could not open or find the image" << std::endl ;
              return -1;
          }
          // Create a window for display.
          namedWindow( "Display window", WINDOW_AUTOSIZE );
          // Show our image inside it.
          imshow( "Display window", image );
          // Wait for a keystroke in the window
          waitKey(0);
          return 0;
      }
      
      
    • Build
    • 
      g++ -lopencv_highgui -lopencv_core -o display_image display_image.cpp
      
      
    • Execute
    • ./display_image lena.jpg

Pi Camera


This package provides a pure Python interface to the Raspberry Pi camera module for Python 2.7 (or above) or Python 3.2 (or above).
The source code can be obtained from GitHub.

Installation


You can check if picamera is installed by starting Python and trying to import picamera:

 python -c "import picamera"
 python3 -c "import picamera"
If you get no error, to install picamera using apt simply run:

 sudo apt-get update
 sudo apt-get install python-picamera python3-picamera
On distributions other than Raspbian, it is probably simplest to install system wide using Python’s pip tool:

 sudo pip install picamera

Getting Started


Once booted, start the Raspberry Pi Configuration utility and enable the camera module.
Then, reboot it again. Once rebooted, start a terminal and try the following command:

 raspistill -o image.jpg
A preview from the camera should appear on the display and, after a 5 second delay it should capture an image (storing it as image.jpg) before shutting down the camera.

Basic Recipes


Advanced Recipes


Frequently Asked Questions (FAQ)


Camera Hardware


Development


Deprecated Functionality


API - The PiCamera Class


API - Streams


API - Renderers

API - Encoders


API - Exceptions

API - Colors and Color Matching


API - Arrays



API - mmalobj


留言

熱門文章