Lecture08 OpenCV
Lecture08 OpenCV
References: 1. "Learning OpenCV: Computer Vision with the OpenCV Library", Bradski & Kaehler (O'Reilley 2008) 2. http://opencv.willowgarage.com/wiki/
(2D Image)
Scene "Information" Skin Tones Parallel Lines (building) Depth calculations ... "Man in front of building"
What is OpenCV?
~2500 computer vision algorithms Highly optimized (originally for Intel) BSD license Languages:
C (function-based) OpenCV 1.x C++ (class based) OpenCV 2.x Python (2.6 and 2.7)
Import cv2 for the OpenCV 2.x style bindings Import cv2.cv for the OpenCV 1.x style bindings Very poor documentation!
Links
Download:
http://opencv.org/
C / C++
http://docs.opencv.org/
Python
Tutorials: https://opencv-python-tutroals.readthedocs.org/en/latest/ [Not really any documentation just read the C++ docs and translate it yourself]
Python setup
Copy [OpenCVdir]\build\Python\2.7\Lib\sitepackages cv2.pyd (a .dll file) Paste it in the same directory as your script (or put in your python install folder)
A tour of CV algorithms
1. Noise reduction:
a. Blurring b. Thresholding c. Erode / Dilate
3. Histograms
a. Back-projection
4. Background-subtraction
im = cv.LoadImage("apple.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) cv.NamedWindow("orig") cv.ShowImage("orig", im) print dir(im) blurred = cv.CreateImage((im.width, im.height), im.depth, 1) cv.Smooth(im, blurred, cv.CV_GAUSSIAN, 9, 9)
2a. Moments
An easy way to analyze a shape
Assumptions (here):
binary image (0=black, 1=white) Mainly One shape: the white part (pass CV_THRESH_BINARY_INV instead of CV_THRESH_BINARY to Threshold)
Notation:
I(x, y): intensity of pixel (x,y) (a 0 or 1) 1 1
( , )
=0 =0
= Note:
( , )
=0 =0
2a. Hu Moments
Invariant (mostly) to
Scale Rotation Reflection
the seventh has different sign for reflection
Hu1 = M20 + M02 Hu2 = (N20 N02)2 + 4M112 Hu3 = (M30 3M12)2 + (3M21 M03)2 Hu7 = (3M21 M03)(M21 + M03)[3(M30 + M12)2 (M21 + M03)2] (M30 3M12)(M21 + M03)[3(M30 + M12)2 (M21 + M03)2]
If you were to treat this as a Vector7, you could compare it to a database of other Vector7's to do simple shape-matching
0.1606958551 (diff2 = 0.000153) 0.0000105604 (diff2 = 0.00000731) 0.0000937233 (diff2 = 6.455e-9) 0.0000001.183 (diff2 = 8.322e-10) 0.000000000000341 (diff2 = 3.227e-19) 0.000000000269809 (diff2 = 2.15e13) -0.000000000000197 (diff2 = 2e-21) Total = 0.0016
cv.Threshold(blurred, edImg, 128, 255.0, cv.CV_THRESH_BINARY_INV) cv.Dilate(edImg, edImg, None, 10) cv.Erode(edImg, edImg, None, 10) edMat = cv.GetMat(edImg) moments = cv.Moments(edMat, 1) hu = cv.GetHuMoments(moments)
0.3608422951 (diff2 = 0.0353) 0.0568850707 (diff2 = 0.003) 0.0170774107 (diff2 = 0.0003) 0.0026356011 (diff2 = 6.8e-6) -0.00001702199712 (diff2 = 2.9e-7) -0.00062800188755 (diff2 = 3.94e-7) 0.000004785759613 (diff2 = 2.3e-11) Total = 0.0385 (~30x "farther")
3. Histograms
Basically, an n-dimensional plot Bins (buckets) Examples:
1D: In a grayscale image, number of pixels in a bin (0-5 intensity, 5-10 intensity, , 250-255 intensity) 2D: Hue-Saturation graph (x-axis = hue, y-axis = saturation)
3. Histograms, cont.
A histogram tells us how often a value (color) appears in the image the histogram was built from.
"Fuller" bin = more prevalent "Emptier" bin = less prevalent
3a. Back-projection
Back-Projection example:
Create an image with flesh tones. Create a histogram from it
Hue-Saturation generally ignores race
Now, given a new color, we can determine how likely it is to be flesh-toned by looking up that spot in the histogram.
If it's a full bin, it's probably a flesh-tone If it's an empty bin, it's probably not a flesh-tone
3a. Back-projection
3a. Back-projection
# Convert img (RGB) to HS(V) hsv = cv.CreateImage(cv.GetSize(img), 8, 3) cv.CvtColor(img, hsv, cv.CV_BGR2HSV) # Get images for the hue and sat "planes" of hsv h_plane = cv.CreateMat(i.height, i.width, cv.CV_8UC1) s_plane = cv.CreateMat(i.height, i.width, cv.CV_8UC1) cv.Split(hsv, h_plane, s_plane, None, None) h_plane = cv.GetImage(h_plane) # CvMat => CvImg s_plane = cv.GetImage(s_plane) hsPlanes = [h_plane, s_plane] # Do the back-projection. Note: hist was as created on a # previous slide backPropImg = cv.CreateImage((img.width, img.height), 8, 1) cv.CalcBackProject(hsPlanes, backPropImg, hist)
4. Background-Subtraction
Goal:
Mark non-background pixels in a mask (1=nonbackground, 0=background) Analyze the shape of the non-background pixels.
4. Background Subtraction
Nave Approach:
cv.AbsDiff(curFrame, bgOnlyFrame, diffImg) # Maybe a threshold now, erosion, dilate, etc.
Problems:
A lot of frame-to-frame noise Webcam auto-adjusting intensity (@#$! Logitechs) Clouds passing by, trees waving in wind,