2012-10-17 09:12:04 +02:00
|
|
|
#!/usr/bin/python
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
import urllib2
|
2011-07-12 14:56:03 +02:00
|
|
|
import cv2.cv as cv
|
2010-05-11 19:44:00 +02:00
|
|
|
from random import randint
|
|
|
|
|
|
|
|
def roundxy(pt):
|
|
|
|
return (cv.Round(pt[0]), cv.Round(pt[1]))
|
|
|
|
|
|
|
|
def draw_common(points):
|
|
|
|
success, center, radius = cv.MinEnclosingCircle(points)
|
|
|
|
if success:
|
|
|
|
cv.Circle(img, roundxy(center), cv.Round(radius), cv.CV_RGB(255, 255, 0), 1, cv. CV_AA, 0)
|
|
|
|
|
|
|
|
box = cv.MinAreaRect2(points)
|
|
|
|
box_vtx = [roundxy(p) for p in cv.BoxPoints(box)]
|
2012-10-17 09:12:04 +02:00
|
|
|
cv.PolyLine(img, [box_vtx], 1, cv.CV_RGB(0, 255, 255), 1, cv. CV_AA)
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
def minarea_array(img, count):
|
|
|
|
pointMat = cv.CreateMat(count, 1, cv.CV_32SC2)
|
|
|
|
for i in range(count):
|
|
|
|
pointMat[i, 0] = (randint(img.width/4, img.width*3/4),
|
|
|
|
randint(img.height/4, img.height*3/4))
|
|
|
|
|
|
|
|
cv.Zero(img)
|
|
|
|
|
|
|
|
for i in range(count):
|
|
|
|
cv.Circle(img, roundxy(pointMat[i, 0]), 2, cv.CV_RGB(255, 0, 0), cv.CV_FILLED, cv. CV_AA, 0)
|
|
|
|
|
|
|
|
draw_common(pointMat)
|
|
|
|
|
|
|
|
def minarea_seq(img, count, storage):
|
|
|
|
points = [(randint(img.width/4, img.width*3/4), randint(img.height/4, img.height*3/4)) for i in range(count)]
|
|
|
|
cv.Zero(img)
|
|
|
|
|
|
|
|
for p in points:
|
|
|
|
cv.Circle(img, roundxy(p), 2, cv.CV_RGB(255, 0, 0), cv.CV_FILLED, cv. CV_AA, 0)
|
|
|
|
|
|
|
|
draw_common(points)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
img = cv.CreateImage((500, 500), 8, 3)
|
|
|
|
storage = cv.CreateMemStorage()
|
|
|
|
|
|
|
|
cv.NamedWindow("rect & circle", 1)
|
2012-10-17 09:12:04 +02:00
|
|
|
|
2010-05-11 19:44:00 +02:00
|
|
|
use_seq = True
|
|
|
|
|
2012-10-17 09:12:04 +02:00
|
|
|
while True:
|
2010-05-11 19:44:00 +02:00
|
|
|
count = randint(1, 100)
|
|
|
|
if use_seq:
|
|
|
|
minarea_seq(img, count, storage)
|
|
|
|
else:
|
|
|
|
minarea_array(img, count)
|
|
|
|
|
|
|
|
cv.ShowImage("rect & circle", img)
|
|
|
|
key = cv.WaitKey() % 0x100
|
|
|
|
if key in [27, ord('q'), ord('Q')]:
|
|
|
|
break
|
|
|
|
|
|
|
|
use_seq = not use_seq
|
2012-03-19 00:12:55 +01:00
|
|
|
cv.DestroyAllWindows()
|