Linux terminal essential

cd ..                                                       
cd # back to home dir                                       
cd ~/p2 # ~ means home dir as well                             

ls # dir

# install vim: sudo is to notify running as administrater,
# apt-get install to install package,
# vim is a common editor (extension of vi)

sudo apt-get install vim 
sudo apt-cache search vim # search packages relating to vi
gedit tmp.py # open to edit a file                        
ctrl-z # break the current job                                   
bg # send job to background                                              
fg # send job back to foreground                                      
ctrl-c # kill foreground job                                           
ps aux | grep gedit # list all jobs and then search for "gedit" jobs              
kill -9 12345 # kill job with job number 12345

# extract the audio channel of dummy.mp4
# audio format suppose to be aac though
# change extension (.ac3/.flac) accordingly

ffmpeg -i dummy.mp4 -acodec copy -vn audio.aac

extract audio channels from all mp4 files in current folder

ls -1 \*.mp4|sed 's/\(.*\)\.mp4/ffmpeg -i "&" -acodec copy -vn "\1.aac"/'|sh

Setup virtualenv (tested on Ubuntu 18.04)

sudo apt-get install python-virtualenv
virtualenv ~/p2 # create a virtualenv with python2
virtualenv --python=/usr/bin/python3 ~/p3 # create a virtualenv with python3
source ~/p2/bin/activate

Install opencv

source ~/p2/bin/activate # make sure you have create virtualenv already, see above.
pip install numpy # install numpy (matlab like library for scientific computing). 
                  # Note that we don't include sudo here as we are installing to the virtualenv 
pip install opencv-python
pip install opencv-contrib-python
pip install opencv-python==3.4.5.20 # install a particular version
pip install -U opencv-python # upgrade to latest

pip search opencv # search packages relating to opencv

Load image with opencv

In [74]:
import cv2
import matplotlib.pyplot as plt
import numpy as np
from skimage import io
import os.path

filename='Lena.png'
if os.path.isfile(filename):
    im=cv2.imread('Lena.png') 
else: 
    im=io.imread('http://optipng.sourceforge.net/pngtech/img/lena.png') 
# note that io.imread and cv2.imread handle images differently, io.imread compatible with plt.imshow directly

Display with matplotlib

In [75]:
%matplotlib inline
plt.imshow(im)
Out[75]:
<matplotlib.image.AxesImage at 0x7f472ff99750>

resize image

In [76]:
ims=cv2.resize(im,(128,128))

rearrange color component and display image

In [77]:
im2=np.zeros_like(ims)
im2[:,:,0]=ims[:,:,2]
im2[:,:,1]=ims[:,:,1]
im2[:,:,2]=ims[:,:,0]

plt.imshow(im2)
Out[77]:
<matplotlib.image.AxesImage at 0x7f472ff86f10>

set r as the first component, edit r, and display the original image

In [78]:
r=im2[:,:,0] 

r[:,50:100]=0

plt.imshow(im2) # note that the original image was modified as well. You may find this surprising 
                # if you are used to the MATLAB world
                # this article may help 
                # https://jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/
Out[78]:
<matplotlib.image.AxesImage at 0x7f472fefadd0>

Define a new filter f and convolve with r

In [79]:
f=np.zeros([5,5])
f[4,4]=1
r=ims[:,:,0]

r2=cv2.filter2D(r,-1,f,borderType=cv2.BORDER_CONSTANT)

plt.imshow(r2)
Out[79]:
<matplotlib.image.AxesImage at 0x7f472fe74950>

apply Canny edge detector on r

In [80]:
edge=cv2.Canny(r,100,200)

plt.imshow(edge,cmap='gray')
Out[80]:
<matplotlib.image.AxesImage at 0x7f472fdea890>

Video capture from webcam

In [19]:
import cv2
import platform

cap=cv2.VideoCapture(0)

while (True):
    
    ret,frame=cap.read()
    
    cv2.namedWindow("camera",cv2.WND_PROP_FULLSCREEN)
    cv2.setWindowProperty("camera",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
    cv2.imshow('camera',frame)
    
    if cv2.waitKey(1) &0xFF == ord('q'): # press q or ESC to quit. You probably need to hit the screen first
        break

        
cap.release()
cv2.destroyAllWindows()
    

Edge detection with webcam input

In [22]:
import cv2
import platform

cap=cv2.VideoCapture(0)

while (True):
    
    ret,frame=cap.read()
    
    cv2.namedWindow("camera",cv2.WND_PROP_FULLSCREEN)
    cv2.setWindowProperty("camera",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
    frame=cv2.cvtColor(frame,cv2.COLOR_RGB2GRAY)
    frame=cv2.Canny(frame,50,60)

    cv2.imshow('camera',frame)
    
    if cv2.waitKey(1) &0xFF == ord('q'): # press q or ESC to quit. You probably need to hit the screen first
        break

        
cap.release()
cv2.destroyAllWindows()