Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining...

15
Intro to Python 3 Day 7 07/28/15

Transcript of Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining...

Page 1: Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining all continuous points along the boundary, having the same color or intensity. Finding

Intro to Python 3

Day 7 07/28/15

Page 2: Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining all continuous points along the boundary, having the same color or intensity. Finding

Last Time1. pandas

Page 3: Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining all continuous points along the boundary, having the same color or intensity. Finding

Today1. OpenCV 2.4.10

Latest Version: OpenCV 3.0http://opencv.org/opencv-3-0.html

** This week’s code samples were compiled using Python 2.7.6

Page 4: Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining all continuous points along the boundary, having the same color or intensity. Finding

OpenCVOpen Source Computer Vision library (OpenCV) is an open source library that contains numerous computer vision algorithms that are compatible with C, C++, and Python

OpenCV Python Tutorials:https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_tutorials.html

Page 5: Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining all continuous points along the boundary, having the same color or intensity. Finding

What can be done in OpenCV? - Object Detection - Image Processing - Camera Calibration - Machine Learning - Video Analysis

Page 6: Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining all continuous points along the boundary, having the same color or intensity. Finding

Image Processingimport cv2cv2.imread(‘left.png’)

Page 7: Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining all continuous points along the boundary, having the same color or intensity. Finding

def find_handles(img, name): '''Detect the orange handles in the image by applying the mask @img = input image @name = image name

return = the calculated bitwise image residual ''' lower_handles = np.array([20, 20, 0], dtype=np.uint8) upper_handles = np.array([255, 255, 20], dtype=np.uint8)

# Apply the color range to the image RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) mask = cv2.inRange(RGB, lower_handles, upper_handles) res = cv2.bitwise_and(img, img, mask=mask)

# Save the parsed image cv2.imwrite(name, res) return res

Page 8: Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining all continuous points along the boundary, having the same color or intensity. Finding

Processed Image

Page 9: Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining all continuous points along the boundary, having the same color or intensity. Finding

Image ContoursImage contours are curves joining all continuous points along the boundary, having the same color or intensity.

Finding Contours: # Get the edge map of the image gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.bilateralFilter(gray, 11, 17, 17) edged = cv2.Canny(gray, 30, 200)

# Find the contours and sort the contours from large to small area (contours, _) = cv2.findContours( edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(contours, key=cv2.contourArea, reverse=True)

Page 10: Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining all continuous points along the boundary, having the same color or intensity. Finding

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Page 11: Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining all continuous points along the boundary, having the same color or intensity. Finding

gray = cv2.bilateralFilter(gray, 11, 17, 17)

Page 12: Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining all continuous points along the boundary, having the same color or intensity. Finding

edged = cv2.Canny(gray, 30, 200)

Page 13: Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining all continuous points along the boundary, having the same color or intensity. Finding

Contour Featureshttp://docs.opencv.org/master/dd/d49/tutorial_py_contour_features.html

Page 14: Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining all continuous points along the boundary, having the same color or intensity. Finding

Identify Rectangles rectangles = [] cnts = [] for cnt in contours: try: rectangles.append(cv2.boundingRect(cnt)) except Exception as e: pass # Drop repeats rectangles = list(set(rectangles))

# Sort rectangles from smallest to largest area

rectangles.sort( key=lambda rectangle: rectangle[2] * rectangle[3], reverse=True)

r = rectangles[0:3] # [(907, 412, 704, 98), (972, 420, 572, 84), (972, 420, 572, 80)]

Page 15: Day 7 07/28/15 - HRVIP 7 07... · 2015-07-29 · Image Contours Image contours are curves joining all continuous points along the boundary, having the same color or intensity. Finding

Processed Image with Handle Detection