# BEGIN http://www.opencvpython.blogspot.com/2012/06/hi-this-article-is-tutorial-which-try.html
!wget http://3.bp.blogspot.com/-a5blM3JLkIU/T9OXg1YhN0I/AAAAAAAAASQ/MbdfSG2oaYg/s200/test.jpg
--2013-01-31 14:21:03-- http://3.bp.blogspot.com/-a5blM3JLkIU/T9OXg1YhN0I/AAAAAAAAASQ/MbdfSG2oaYg/s200/test.jpg Resolving 3.bp.blogspot.com (3.bp.blogspot.com)... 2001:4860:4001:802::1008, 74.125.224.70, 74.125.224.72, ... Connecting to 3.bp.blogspot.com (3.bp.blogspot.com)|2001:4860:4001:802::1008|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 2605 (2.5K) [image/jpeg] Saving to: `test.jpg' 100%[======================================>] 2,605 --.-K/s in 0s 2013-01-31 14:21:03 (292 MB/s) - `test.jpg' saved [2605/2605]
import numpy as np
import cv2
import cv
import shapely.geometry
im = cv2.imread('test.jpg')
whos
Variable Type Data/Info -------------------------------- contours list n=1 cv module <module 'cv' from '/usr/l<...>odules/python2.7/cv.pyc'> cv2 module <module 'cv2' from '/usr/<...>odules/python2.7/cv2.so'> hierarchy ndarray 1x1x4: 4 elems, type `int32`, 16 bytes im ndarray 200x200x3: 120000 elems, type `uint8`, 120000 bytes (117 kb) imgray ndarray 200x200: 40000 elems, type `uint8`, 40000 bytes ret float 127.0 thresh ndarray 200x200: 40000 elems, type `uint8`, 40000 bytes
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
imshow(thresh)
<matplotlib.image.AxesImage at 0x42cfe50>
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
imshow(thresh)
<matplotlib.image.AxesImage at 0x4467dd0>
len(contours), hierarchy
(1, array([[[-1, -1, -1, -1]]], dtype=int32))
len(contours[0])
170
cv2.drawContours(im, contours, -1, (0,255,0), 3)
imshow(im)
<matplotlib.image.AxesImage at 0x4855350>
cv2.drawContours(im, contours, -1, (0,255,0), -1)
imshow(im)
<matplotlib.image.AxesImage at 0x4a61850>
cv2.drawContours(im, contours, 0, (0,255,0), 1)
imshow(im)
<matplotlib.image.AxesImage at 0x4a96650>
!wget http://3.bp.blogspot.com/-1UtLXb7c73U/T9QZT3tpVjI/AAAAAAAAATE/Nyo7SFg8T1o/s1600/balls.png
--2013-01-31 15:06:48-- http://3.bp.blogspot.com/-1UtLXb7c73U/T9QZT3tpVjI/AAAAAAAAATE/Nyo7SFg8T1o/s1600/balls.png Resolving 3.bp.blogspot.com (3.bp.blogspot.com)... 2001:4860:4001:800::1005, 74.125.224.97, 74.125.224.101, ... Connecting to 3.bp.blogspot.com (3.bp.blogspot.com)|2001:4860:4001:800::1005|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 3499 (3.4K) [image/png] Saving to: `balls.png' 100%[======================================>] 3,499 --.-K/s in 0s 2013-01-31 15:06:48 (337 MB/s) - `balls.png' saved [3499/3499]
im = cv2.imread('balls.png')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 100, 255, 0)
imshow(thresh)
<matplotlib.image.AxesImage at 0x57eb390>
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for h, cnt in enumerate(contours):
mask = np.zeros(imgray.shape, np.uint8)
cv2.drawContours(mask, [cnt], 0, 255, -1)
mean = cv2.mean(im, mask=mask)
print h, mean
0 (0.0, 242.0, 255.0, 0.0) 1 (232.0, 162.0, 0.0, 0.0) 2 (76.0, 177.0, 34.0, 0.0) 3 (39.0, 127.0, 255.0, 0.0)
# END http://www.opencvpython.blogspot.com/2012/06/hi-this-article-is-tutorial-which-try.html
moments = cv2.moments(contours[0])
area = moments['m00']
moments
{'m00': 10033.5, 'm01': 1850134.5, 'm02': 352044973.5833333, 'm03': 68983799276.15001, 'm10': 2010061.8333333333, 'm11': 368666693.125, 'm12': 69763579350.98334, 'm20': 409360323.5833333, 'm21': 74691021944.88333, 'm30': 84692116672.95001, 'mu02': 10888082.359906793, 'mu03': 0.005234025965704581, 'mu11': -1980114.5675549507, 'mu12': -33122544.260385513, 'mu20': 6674463.831166983, 'mu21': 101313.30416250229, 'mu30': 8633090.369003296, 'nu02': 0.10815497152071127, 'nu11': -0.019669141689288665, 'nu12': -0.0032846761082870463, 'nu20': 0.06629968636479547, 'nu21': 1.0046975468372928e-05, 'nu30': 0.0008561209988226333}
cv2.contourArea(contours[0])
10033.5
perimeter = cv2.arcLength(contours[0], True) # True says that curve is closed
perimeter
473.34523379802704
cnt = contours[0]
len(cnt)
231
x = cnt[:,0, 0]; y = cnt[:,0,1]
plot(x,y)
[<matplotlib.lines.Line2D at 0x5ac8f50>]
simpler = cv2.approxPolyDP(cnt, 2, True)
plot(simpler[:,0,0], simpler[:,0,1])
[<matplotlib.lines.Line2D at 0x60c09d0>]
hull = cv2.convexHull(cnt)
plot(hull[:,0,0], hull[:,0,1])
[<matplotlib.lines.Line2D at 0x6381fd0>]
simpler_hull = cv2.approxPolyDP(hull, 2.5, True)
plot(simpler_hull[:,0,0], simpler_hull[:,0,1])
plot(x,y)
[<matplotlib.lines.Line2D at 0x6957e10>]
r_x, r_y, r_w, r_h = cv2.boundingRect(cnt)
r_x, r_y, r_w, r_h # (150, 121, 103, 146)
plot((r_x, r_x, r_x+r_w, r_x+r_w, r_x), (r_y, r_y+r_h, r_y+r_h, r_y, r_y))
plot(x,y)
[<matplotlib.lines.Line2D at 0x6eab610>]
rect = cv2.minAreaRect(cnt)
rect # ((202.134521484375, 192.14178466796875), (102.39618682861328, 140.3079376220703), -5.128190994262695)
box = cv2.cv.BoxPoints(rect)
box # ((157.41201782226562, 266.59124755859375), (144.87069702148438, 126.84494018554688), (246.85702514648438, 117.69232177734375), (259.3983459472656, 257.4386291503906))
# plot( [p[0] for p in box] + [box[0][0]], [p[1] for p in box] + [box[0][1]] )
box_list = list(box)
box_list.append(box[0])
ba = np.array(box_list) # Box array
plot(ba[:,0], ba[:,1])
plot(x,y)
[<matplotlib.lines.Line2D at 0x7746a10>]
(c_x, c_y), radius = cv2.minEnclosingCircle(cnt)
c_x, c_y, radius # (197.0, 194.5, 82.92139434814453)
center = shapely.geometry.Point(c_x, c_y)
circle = np.array(center.buffer(radius).boundary.coords)
len(circle) # 66 points
plot(circle[:,0], circle[:,1])
plot(x,y)
[<matplotlib.lines.Line2D at 0x7ba81d0>]
ellipse = cv2.fitEllipse(cnt)
ellipse # ((199.31251525878906, 185.9192352294922), (93.7149658203125, 138.58531188964844), 202.948486328125)
# TODO: what is a convenient way to get the coords for an ellipse?
((199.31251525878906, 185.9192352294922), (93.7149658203125, 138.58531188964844), 202.948486328125)