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

Post on 12-Jul-2020

3 views 0 download

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

Intro to Python 3

Day 7 07/28/15

Last Time1. pandas

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

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

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

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

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

Processed Image

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)

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

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

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

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

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)]

Processed Image with Handle Detection