In [1]:
# BEGIN http://www.opencvpython.blogspot.com/2012/06/hi-this-article-is-tutorial-which-try.html
In [2]:
!wget http://3.bp.blogspot.com/-a5blM3JLkIU/T9OXg1YhN0I/AAAAAAAAASQ/MbdfSG2oaYg/s200/test.jpg
In [4]:
import numpy as np
import cv2
import cv
import shapely.geometry
In [28]:
im = cv2.imread('test.jpg')
In [29]:
whos
In [30]:
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
imshow(imgray)
Out[30]:
In [8]:
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
imshow(thresh)
Out[8]:
In [9]:
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
imshow(thresh)
Out[9]:
In [10]:
len(contours), hierarchy
Out[10]:
In [11]:
len(contours[0])
Out[11]:
In [12]:
cv2.drawContours(im, contours, -1, (0,255,0), 3)
imshow(im)
Out[12]:
In [13]:
cv2.drawContours(im, contours, -1, (0,255,0), -1)
imshow(im)
Out[13]:
In [14]:
cv2.drawContours(im, contours, 0, (0,255,0), 1)
imshow(im)
Out[14]:
In [15]:
!wget http://3.bp.blogspot.com/-1UtLXb7c73U/T9QZT3tpVjI/AAAAAAAAATE/Nyo7SFg8T1o/s1600/balls.png
In [16]:
im = cv2.imread('balls.png')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
In [17]:
ret, thresh = cv2.threshold(imgray, 100, 255, 0)
imshow(thresh)
Out[17]:
In [18]:
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
In [19]:
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
In [20]:
# END http://www.opencvpython.blogspot.com/2012/06/hi-this-article-is-tutorial-which-try.html
In [21]:
moments = cv2.moments(contours[0])
area = moments['m00']
moments
Out[21]:
In [22]:
cv2.contourArea(contours[0])
Out[22]:
In [23]:
perimeter = cv2.arcLength(contours[0], True) # True says that curve is closed
perimeter
Out[23]:
In [24]:
cnt = contours[0]
len(cnt)
Out[24]:
In [25]:
x = cnt[:,0, 0]; y = cnt[:,0,1]
plot(x,y)
Out[25]:
In [26]:
simpler = cv2.approxPolyDP(cnt, 2, True)
plot(simpler[:,0,0], simpler[:,0,1])
Out[26]:
In [63]:
hull = cv2.convexHull(cnt)
plot(hull[:,0,0], hull[:,0,1])
Out[63]:
In [69]:
simpler_hull = cv2.approxPolyDP(hull, 2.5, True)
plot(simpler_hull[:,0,0], simpler_hull[:,0,1])
plot(x,y)
Out[69]:
In [73]:
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)
Out[73]:
In [88]:
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)
Out[88]:
In [97]:
(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)
Out[97]:
In [100]:
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?
Out[100]:
In [ ]: