OpenCV Library from Python

OpenCV Library from Python

? We will use Python (which is trivial to learn) to call on modules from the OpenCV library for homework assignments.

? This should allow students to get into doing computer vision sooner and to using more advanced operators than you have time to code yourselves.

? It is common in the CV community to make use of the code of others.

Basic IO

? Read Image im_color = cv2.imread(`a.jpg') im_gray = cv2.imread(`a.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE) im_gray = cv2.imread(`a.jpg', 0)

? Write Image cv2.imwrite(`a.jpg', im_color)

? Display Image cv2.imshow(`WINDOW NAME', im_color) cv2.waitKey(0) /* Wait for it to display */

Image Thresholding

? th, dst = cv2.threshold(src, thresh, maxval, type)

src ? input image,

thresh ? threshold value

maxval ? maximum value to use, type ? thresholding type

cv2.THRESH_BINARY

cv2.THRESH_BINARY_INV

cv2.THRESH_TRUNC

cv2.THRESH_TOZERO

cv2.THRESH_TOZERO_INV

Image thresholding

? For example

import cv2 /* Read the image */ im_apple = cv2.imread('d:/apple.png', 0) /* Threshold to 128 those pixels less than 225 */ _, im_apple_th = cv2.threshold(im_apple, 225, 128, cv2.THRESH_BINARY_INV) /* Display */ cv2.imshow('apple', im_apple_th) cv2.waitKey(0)

Morphology Operations

? To construct the kernel kernel = cv2.getStructuringElement(type, size) type ? kernel shape cv2.MORPH_RECT: rectangle shape in size(width, height) cv2.MORPH_ELLIPSE : ellipse shape in size(width, height) cv2.MORPH_CROSS : cross shape in size(width, height) size ? e.g. (3, 3)

? Dilate and erode des = cv2.dilate(src, kernel) des = cv2.erode(src, kernel)

? Opening and closing (see next slide for general morphology function)

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download