You are on page 1of 13

Computer Vision

OpenCV + Python

OpenCV
Set of Libraries for commonly used computer
vision algorithms(computer vision module)
Like using header files in C++
Eg.
Convert to grayscale
Split image into R, G, B
Edge Detection
Finding contours
Face Detection

The difficulty of Vision

Note:
Lines are not parallel
Sides are not equal
Edges change based
on viewing angle

Video

Image

Simple
functions

More
Processing

Low
Resolution

High
resolution

Fast

Slow

Specialized

General

Non Real Time

Real Time

Trade Offs

Basic Program Image from File


import numpy as np
import cv2
Import OpenCV
img1 = cv2.imread(image.jpg')
Get the Image
Do the processing

img2 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)

Show the output


cv2.imshow('image',img2)
Close and exit
cv2.waitKey(0)
cv2.destroyAllWindows()

Basic Program Image from Camera


import numpy as np
import cv2
Import OpenCV

Get the Image


Do the processing

cap = cv2.VideoCapture(0)
ret, frame = cap.read()

img2 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

Show the output


cv2.imshow('image',img2)
Close and exit
cv2.waitKey(0)
cap.release()
cv2.destroyAllWindows()

Basic Program Video from Camera


import numpy as np
import cv2
cap = cv2.VideoCapture(0)

Import OpenCV

loop

Get the Image


Do the processing

Show the output

while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1)==27:
break

Close and exit


cap.release()
cv2.destroyAllWindows()

Example of real program Motion Detection


Motion detection
Import OpenCV

loop

Get the Image

Checks for changes in image


Therefore, we need to find the
difference between two images
Literally, difference = subtraction
OpenCV function absdiff(img1,img2)

Implementation
Do the processing

Show the output

Close and exit

1. We can find the difference between


the values of the same pixel(position)
between the two images
2. Therefore, we need a grayscale image
3. After we get the two grayscale images,
use absdiff(img1,img2) to find the
difference
4. Show the output

Example of real program Showing Movement


Import OpenCV

loop

Get the Image


Do the processing

Show the output

Close and exit

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
gray_old = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
while(True):
ret, frame = cap.read()
gray_new = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
img_diff = cv2.absdiff(gray_new,gray_old)
cv2.imshow('Result',img_diff)
gray_new = gray_old
if cv2.waitKey(1)==27:
break
cap.release()
cv2.destroyAllWindows()

In the above program, we have just shown the difference, and it is easier for a human to find the motion.
However, the computer still is not judging(understanding) if motion has occurred.
Therefore, we have used image processing, but not computer vision.
To do so, we must impose a judgment on the resultant image, i.e. img_diff

Example of real program Motion Detection


Import OpenCV

loop

Get the Image


Do the processing

Show the output


Make Judgment
Close and exit

import numpy as np
import cv2
cap = cv2.VideoCapture(1)
ret, frame = cap.read()
g_old = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
h,w = g_old.shape
imgt1 = cv2.resize(g_old,(w/5,h/5), interpolation = cv2.INTER_AREA)
while(True):
ret, frame = cap.read()
g_new = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
imgt2 = cv2.resize(g_new,(w/5,h/5), interpolation = v2.INTER_AREA)
img_diff = cv2.absdiff(imgt2,imgt1)
cv2.imshow('Result',img_diff)
if cv2.countNonZero(imgt_diff) > 2000:
print yo whats up
gray_new = gray_old
if cv2.waitKey(1)==27:
break
cap.release()
cv2.destroyAllWindows()

Now, we have added 3 components:


1. Found size of image
2. Reduced resolution to make processing easier
3. Counted white pixels and made a judgment Is a motion detected?

Extra Links
OpenCV + Python Tutorials
http://docs.opencv.org/trunk/doc/py_tutorials/py_tutorials.html
https://opencv-pythontutroals.readthedocs.org/en/latest/py_tutorials/py_tutorials.html

OpenCV Documentation
http://docs.opencv.org/trunk/modules/refman.html

Python 2.7 Documentation


https://docs.python.org/2.7/

Gestalt Principles
http://www.smashingmagazine.com/2014/03/28/design-principles-visual-perceptionand-the-principles-of-gestalt/

You might also like