]> rtime.felk.cvut.cz Git - opencv.git/commitdiff
minarea
authorjamesb <jamesb@73c94f0f-984f-4a5f-82bc-2d8db8d8ee08>
Wed, 10 Mar 2010 23:34:31 +0000 (23:34 +0000)
committerjamesb <jamesb@73c94f0f-984f-4a5f-82bc-2d8db8d8ee08>
Wed, 10 Mar 2010 23:34:31 +0000 (23:34 +0000)
git-svn-id: https://code.ros.org/svn/opencv/trunk@2787 73c94f0f-984f-4a5f-82bc-2d8db8d8ee08

opencv/samples/python/minarea.py

index dffdd4c0a7a49bb899ff4a8c35b9c60d0ee64867..e2be5496bc19d013417a91f086eaee8762517dfb 100755 (executable)
@@ -4,57 +4,47 @@ import urllib2
 import cv
 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)]
+    cv.PolyLine(img, [box_vtx], 1, cv.CV_RGB(0, 255, 255), 1, cv. CV_AA) 
+
 def minarea_array(img, count):
     pointMat = cv.CreateMat(count, 1, cv.CV_32SC2)
     for i in range(count):
-        pointMat[i] = (randint(img.width/4, img.width*3/4),
+        pointMat[i, 0] = (randint(img.width/4, img.width*3/4),
                                randint(img.height/4, img.height*3/4))
 
-    box = cv.MinAreaRect2(pointMat)
-    box_vtx = cv.BoxPoints(box)
-    success, center, radius = cv.MinEnclosingCircle(pointMat)
     cv.Zero(img)
+
     for i in range(count):
-        cv.Circle(img, cv.Get1D(pointMat, i), 2, cv.CV_RGB(255, 0, 0), cv. CV_FILLED, cv. CV_AA, 0)
+        cv.Circle(img, roundxy(pointMat[i, 0]), 2, cv.CV_RGB(255, 0, 0), cv.CV_FILLED, cv. CV_AA, 0)
 
-    box_vtx = [From32f(box_vtx[0]),
-               From32f(box_vtx[1]),
-               From32f(box_vtx[2]),
-               From32f(box_vtx[3])]
-    cv.Circle(img, From32f(center), cv.Round(radius), cv.CV_RGB(255, 255, 0), 1, cv. CV_AA, 0)
-    cv.PolyLine(img, [box_vtx], 1, cv.CV_RGB(0, 255, 255), 1, cv. CV_AA) 
-    
+    draw_common(pointMat)
 
-    
 def minarea_seq(img, count, storage):
-    ptseq = cv.CreateSeq(cv.CV_SEQ_KIND_GENERIC | cv. CV_32SC2, sizeof_CvContour, sizeof_CvPoint, storage)
-    ptseq = CvSeq_CvPoint.cast(ptseq)
-    for i in range(count):
-        pt0 = (randint(img.width/4, img.width*3/4),
-                       randint(img.height/4, img.height*3/4))
-        cv.SeqPush(ptseq, pt0)
-    box = cv.MinAreaRect2(ptseq)
-    box_vtx = cv.BoxPoints(box)
-    success, center, radius = cv.MinEnclosingCircle(ptseq)
+    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 pt in ptseq: 
-        cv.Circle(img, pt, 2, cv.CV_RGB(255, 0, 0), cv. CV_FILLED, cv. CV_AA, 0)
-
-    box_vtx = [From32f(box_vtx[0]),
-               From32f(box_vtx[1]),
-               From32f(box_vtx[2]),
-               From32f(box_vtx[3])]
-    cv.Circle(img, From32f(center), cv.Round(radius), cv.CV_RGB(255, 255, 0), 1, cv. CV_AA, 0)
-    cv.PolyLine(img, [box_vtx], 1, cv.CV_RGB(0, 255, 255), 1, cv. CV_AA) 
-    cv.ClearMemStorage(storage)
+
+    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(0)
+    storage = cv.CreateMemStorage()
 
     cv.NamedWindow("rect & circle", 1)
         
-    use_seq=True
+    use_seq = True
 
     while True: 
         count = randint(1, 100)
@@ -65,7 +55,7 @@ if __name__ == "__main__":
 
         cv.ShowImage("rect & circle", img)
         key = cv.WaitKey()
-        if(key == '\x1b'):
+        if key in [27, ord('q'), ord('Q')]:
             break
 
         use_seq = not use_seq