Introduction: Subtracting the Background From a Video With Intel Edison and OpenCV

This is an Instructable to show you how to subtract the background of an image using the Intel Edison and OpenCV. This technique can be used to identify moving objects in an video such as cars, people, etc.

Step 1: What You Need

  1. Intel Edison
  2. Arduino Breakout Board
  3. 9V Battery to serve as a Power source

Step 2: Install the Latest IoT Developer Kit Libraries Onto the Intel Edison

Use the following command:

echo "src intel-iotdk http://iotdk.intel.com/repos/1.1/intelgalactic" > /etc/opkg/intel-iotdk.conf

Update the package repository, then upgrade all the packages:

opkg update

opkg upgrade

Step 3: ​Add an Unofficial Package Repository

Add the following lines to base-feeds.conf:

echo "src/gz all http://repo.opkg.net/edison/repo/all

src/gz edison http://repo.opkg.net/edison/repo/edison

src/gz core2-32 http://repo.opkg.net/edison/repo/core2-32" >> /etc/opkg/base-feeds.conf

Update the repository index again, since you just added new package locations:

opkg update

Next, install NumPy, OpenCV, and OpenCV-Python.

opkg install python-numpy opencv python-opencv nano

Step 4: Setup OpenCVBuildathon Framework

1. Clone the openCVBuildathon repo to your edison board from the commandline

git clone https://github.com/its2mc/openCVBuildathon.git Projectname

2. Make sure all the python pip dependencies have been installed

a. numpy : pip install numpy (pip install numpy -U "if numpy is already installed")

b. autobahn : pip install autobahn (pip install autobahn -U "if autobahn is already installed")

c. twisted : pip install twisted (pip install twisted -U "if twisted is already installed")

3. Make sure the nodejs npm dependencies have been installed

a. express : npm install -g express --update

4. Run the streaming server using this command : node stream.js > output.txt &

This will run the code in the background while any output will be written to output.txt.

Step 5: ​Running Video Viewer

  1. Edit the video.html document in the static folder. Change the localUrl variable to the edison device ip address with respect to your computer.
  2. Save and exit. Edit the video.py file in the main folder. You can choose to replace your own code as instructed on the document, or you can choose to run the sample as it is.
  3. To run the script you can run "python video.py"

Step 6: Customising the Video.py to Capture Frame and Remove Background

Create a Background Subtractor using OpenCV:

background_subtractor = cv2.BackgroundSubtractorMOG2(history=3, varThreshold=10, bShadowDetection=False)

For each frame, apply it to the background subtractor:

background_mask = background_subtractor.apply(frame, learningRate=0.1)

Apply the background mask to the original image:

background_masked_image = cv2.bitwise_and(frame, frame, mask = background_mask)

Display the new image with its background subtracted