--- /dev/null
+#! /usr/bin/env python
+
+print "OpenCV Python version of contours"
+
+# import the necessary things for OpenCV
+import cv
+
+# some default constants
+_SIZE = 500
+_DEFAULT_LEVEL = 3
+
+# definition of some colors
+_red = (0, 0, 255, 0);
+_green = (0, 255, 0, 0);
+_white = cv.RealScalar (255)
+_black = cv.RealScalar (0)
+
+# the callback on the trackbar, to set the level of contours we want
+# to display
+def on_trackbar (position):
+
+ # create the image for putting in it the founded contours
+ contours_image = cv.CreateImage ( (_SIZE, _SIZE), 8, 3)
+
+ # compute the real level of display, given the current position
+ levels = position - 3
+
+ # initialisation
+ _contours = contours
+
+ if levels <= 0:
+ # zero or negative value
+ # => get to the nearest face to make it look more funny
+ _contours = contours.h_next().h_next().h_next()
+
+ # first, clear the image where we will draw contours
+ cv.SetZero (contours_image)
+
+ # draw contours in red and green
+ cv.DrawContours (contours_image, _contours,
+ _red, _green,
+ levels, 3, cv.CV_AA,
+ (0, 0))
+
+ # finally, show the image
+ cv.ShowImage ("contours", contours_image)
+
+if __name__ == '__main__':
+
+ # create the image where we want to display results
+ image = cv.CreateImage ( (_SIZE, _SIZE), 8, 1)
+
+ # start with an empty image
+ cv.SetZero (image)
+
+ # draw the original picture
+ for i in range (6):
+ dx = (i % 2) * 250 - 30
+ dy = (i / 2) * 150
+
+ cv.Ellipse (image,
+ (dx + 150, dy + 100),
+ (100, 70),
+ 0, 0, 360, _white, -1, 8, 0)
+ cv.Ellipse (image,
+ (dx + 115, dy + 70),
+ (30, 20),
+ 0, 0, 360, _black, -1, 8, 0)
+ cv.Ellipse (image,
+ (dx + 185, dy + 70),
+ (30, 20),
+ 0, 0, 360, _black, -1, 8, 0)
+ cv.Ellipse (image,
+ (dx + 115, dy + 70),
+ (15, 15),
+ 0, 0, 360, _white, -1, 8, 0)
+ cv.Ellipse (image,
+ (dx + 185, dy + 70),
+ (15, 15),
+ 0, 0, 360, _white, -1, 8, 0)
+ cv.Ellipse (image,
+ (dx + 115, dy + 70),
+ (5, 5),
+ 0, 0, 360, _black, -1, 8, 0)
+ cv.Ellipse (image,
+ (dx + 185, dy + 70),
+ (5, 5),
+ 0, 0, 360, _black, -1, 8, 0)
+ cv.Ellipse (image,
+ (dx + 150, dy + 100),
+ (10, 5),
+ 0, 0, 360, _black, -1, 8, 0)
+ cv.Ellipse (image,
+ (dx + 150, dy + 150),
+ (40, 10),
+ 0, 0, 360, _black, -1, 8, 0)
+ cv.Ellipse (image,
+ (dx + 27, dy + 100),
+ (20, 35),
+ 0, 0, 360, _white, -1, 8, 0)
+ cv.Ellipse (image,
+ (dx + 273, dy + 100),
+ (20, 35),
+ 0, 0, 360, _white, -1, 8, 0)
+
+ # create window and display the original picture in it
+ cv.NamedWindow ("image", 1)
+ cv.ShowImage ("image", image)
+
+ # create the storage area
+ storage = cv.CreateMemStorage (0)
+
+ # find the contours
+ contours = cv.FindContours(image,
+ storage,
+ cv.CV_RETR_TREE,
+ cv.CV_CHAIN_APPROX_SIMPLE,
+ (0,0))
+
+ # comment this out if you do not want approximation
+ contours = cv.ApproxPoly (contours,
+ storage,
+ cv.CV_POLY_APPROX_DP, 3, 1)
+
+ # create the window for the contours
+ cv.NamedWindow ("contours", 1)
+
+ # create the trackbar, to enable the change of the displayed level
+ cv.CreateTrackbar ("levels+3", "contours", 3, 7, on_trackbar)
+
+ # call one time the callback, so we will have the 1st display done
+ on_trackbar (_DEFAULT_LEVEL)
+
+ # wait a key pressed to end
+ cv.WaitKey (0)
--- /dev/null
+#! /usr/bin/env python
+
+print "OpenCV Python version of convexhull"
+
+# import the necessary things for OpenCV
+import cv
+
+# to generate random values
+import random
+
+# how many points we want at max
+_MAX_POINTS = 100
+
+if __name__ == '__main__':
+
+ # main object to get random values from
+ my_random = random.Random ()
+
+ # create the image where we want to display results
+ image = cv.CreateImage ( (500, 500), 8, 3)
+
+ # create the window to put the image in
+ cv.NamedWindow ('hull', cv.CV_WINDOW_AUTOSIZE)
+
+ while True:
+ # do forever
+
+ # get a random number of points
+ count = my_random.randrange (0, _MAX_POINTS) + 1
+
+ # initialisations
+ points = []
+
+ for i in range (count):
+ # generate a random point
+ points.append ( (
+ my_random.randrange (0, image.width / 2) + image.width / 4,
+ my_random.randrange (0, image.width / 2) + image.width / 4
+ ))
+
+ # compute the convex hull
+ storage = cv.CreateMemStorage(0)
+ hull = cv.ConvexHull2 (points, storage, cv.CV_CLOCKWISE, 1)
+
+ # start with an empty image
+ cv.SetZero (image)
+
+ # draw all the points as circles in red
+ for i in range (count):
+ cv.Circle (image, points [i], 2,
+ (0, 0, 255, 0),
+ cv.CV_FILLED, cv.CV_AA, 0)
+
+ # Draw the convex hull as a closed polyline in green
+ cv.PolyLine(image, [hull], 1, cv.RGB(0,255,0), 1, cv.CV_AA)
+
+ # display the final image
+ cv.ShowImage ('hull', image)
+
+ # handle events, and wait a key pressed
+ k = cv.WaitKey (0)
+ if k == 27:
+ # user has press the ESC key, so exit
+ break
--- /dev/null
+#!/usr/bin/python
+"""
+the script demostrates iterative construction of
+delaunay triangulation and voronoi tesselation
+
+Original Author (C version): ?
+Converted to Python by: Roman Stanchak
+"""
+import cv
+import random
+
+def draw_subdiv_point( img, fp, color ):
+ cv.Circle( img, (cv.Round(fp[0]), cv.Round(fp[1])), 3, color, cv.CV_FILLED, 8, 0 );
+
+def draw_subdiv_edge( img, edge, color ):
+ org_pt = cv.Subdiv2DEdgeOrg(edge);
+ dst_pt = cv.Subdiv2DEdgeDst(edge);
+
+ if org_pt and dst_pt :
+
+ org = org_pt.pt;
+ dst = dst_pt.pt;
+
+ iorg = ( cv.Round( org[0] ), cv.Round( org[1] ));
+ idst = ( cv.Round( dst[0] ), cv.Round( dst[1] ));
+
+ cv.Line( img, iorg, idst, color, 1, cv.CV_AA, 0 );
+
+
+def draw_subdiv( img, subdiv, delaunay_color, voronoi_color ):
+
+ for edge in subdiv.edges:
+ edge_rot = cv.Subdiv2DRotateEdge( edge, 1 )
+
+ draw_subdiv_edge( img, edge_rot, voronoi_color );
+ draw_subdiv_edge( img, edge, delaunay_color );
+
+
+def locate_point( subdiv, fp, img, active_color ):
+
+ (res, e0) = cv.Subdiv2DLocate( subdiv, fp );
+
+ if res in [ cv.CV_PTLOC_INSIDE, cv.CV_PTLOC_ON_EDGE ]:
+ e = e0
+ while True:
+ draw_subdiv_edge( img, e, active_color );
+ e = cv.Subdiv2DGetEdge(e, cv.CV_NEXT_AROUND_LEFT);
+ if e == e0:
+ break
+
+ draw_subdiv_point( img, fp, active_color );
+
+
+def draw_subdiv_facet( img, edge ):
+
+ t = edge;
+ count = 0;
+
+ # count number of edges in facet
+ while count == 0 or t != edge:
+ count+=1
+ t = cv.Subdiv2DGetEdge( t, cv.CV_NEXT_AROUND_LEFT );
+
+ buf = []
+
+ # gather points
+ t = edge;
+ for i in range(count):
+ assert t>4
+ pt = cv.Subdiv2DEdgeOrg( t );
+ if not pt:
+ break;
+ buf.append( ( cv.Round(pt.pt[0]), cv.Round(pt.pt[1]) ) );
+ t = cv.Subdiv2DGetEdge( t, cv.CV_NEXT_AROUND_LEFT );
+
+ if( len(buf)==count ):
+ pt = cv.Subdiv2DEdgeDst( cv.Subdiv2DRotateEdge( edge, 1 ));
+ cv.FillConvexPoly( img, buf, cv.RGB(random.randrange(256),random.randrange(256),random.randrange(256)), cv.CV_AA, 0 );
+ cv.PolyLine( img, [buf], 1, cv.RGB(0,0,0), 1, cv.CV_AA, 0);
+ draw_subdiv_point( img, pt.pt, cv.RGB(0,0,0));
+
+def paint_voronoi( subdiv, img ):
+
+ cv.CalcSubdivVoronoi2D( subdiv );
+
+ for edge in subdiv.edges:
+
+ # left
+ draw_subdiv_facet( img, cv.Subdiv2DRotateEdge( edge, 1 ));
+
+ # right
+ draw_subdiv_facet( img, cv.Subdiv2DRotateEdge( edge, 3 ));
+
+if __name__ == '__main__':
+ win = "source";
+ rect = ( 0, 0, 600, 600 );
+
+ active_facet_color = cv.RGB( 255, 0, 0 );
+ delaunay_color = cv.RGB( 0,0,0);
+ voronoi_color = cv.RGB(0, 180, 0);
+ bkgnd_color = cv.RGB(255,255,255);
+
+ img = cv.CreateImage( (rect[2],rect[3]), 8, 3 );
+ cv.Set( img, bkgnd_color );
+
+ cv.NamedWindow( win, 1 );
+
+ storage = cv.CreateMemStorage(0);
+ subdiv = cv.CreateSubdivDelaunay2D( rect, storage );
+
+ print "Delaunay triangulation will be build now interactively."
+ print "To stop the process, press any key\n";
+
+ for i in range(200):
+ fp = ( random.random()*(rect[2]-10)+5, random.random()*(rect[3]-10)+5 )
+
+ locate_point( subdiv, fp, img, active_facet_color );
+ cv.ShowImage( win, img );
+
+ if( cv.WaitKey( 100 ) >= 0 ):
+ break;
+
+ cv.SubdivDelaunay2DInsert( subdiv, fp );
+ cv.CalcSubdivVoronoi2D( subdiv );
+ cv.Set( img, bkgnd_color );
+ draw_subdiv( img, subdiv, delaunay_color, voronoi_color );
+ cv.ShowImage( win, img );
+
+ if( cv.WaitKey( 100 ) >= 0 ):
+ break;
+
+
+ cv.Set( img, bkgnd_color );
+ paint_voronoi( subdiv, img );
+ cv.ShowImage( win, img );
+
+ cv.WaitKey(0);
+
+ cv.DestroyWindow( win );
--- /dev/null
+#!/usr/bin/python
+import cv
+import sys
+import urllib2
+
+hist_size = 64
+range_0 = [0, 256]
+ranges = [ range_0 ]
+
+class DemHist:
+
+ def __init__(self, src_image):
+ self.src_image = src_image
+ self.dst_image = cv.CloneMat(src_image)
+ self.hist_image = cv.CreateImage((320, 200), 8, 1)
+ self.hist = cv.CreateHist([hist_size], cv.CV_HIST_ARRAY, ranges, 1)
+
+ self.brightness = 0
+ self.contrast = 0
+
+ cv.NamedWindow("image", 0)
+ cv.NamedWindow("histogram", 0)
+ cv.CreateTrackbar("brightness", "image", 100, 200, self.update_brightness)
+ cv.CreateTrackbar("contrast", "image", 100, 200, self.update_contrast)
+
+ self.update_brightcont()
+
+ def update_brightness(self, val):
+ self.brightness = val - 100
+ self.update_brightcont()
+
+ def update_contrast(self, val):
+ self.contrast = val - 100
+ self.update_brightcont()
+
+ def update_brightcont(self):
+ # The algorithm is by Werner D. Streidt
+ # (http://visca.com/ffactory/archives/5-99/msg00021.html)
+
+ if self.contrast > 0:
+ delta = 127. * self.contrast / 100
+ a = 255. / (255. - delta * 2)
+ b = a * (self.brightness - delta)
+ else:
+ delta = -128. * self.contrast / 100
+ a = (256. - delta * 2) / 255.
+ b = a * self.brightness + delta
+
+ cv.ConvertScale(self.src_image, self.dst_image, a, b)
+ cv.ShowImage("image", self.dst_image)
+
+ cv.CalcArrHist([self.dst_image], self.hist)
+ (min_value, max_value, _, _) = cv.GetMinMaxHistValue(self.hist)
+ cv.Scale(self.hist.bins, self.hist.bins, float(self.hist_image.height) / max_value, 0)
+
+ cv.Set(self.hist_image, cv.ScalarAll(255))
+ bin_w = round(float(self.hist_image.width) / hist_size)
+
+ for i in range(hist_size):
+ cv.Rectangle(self.hist_image, (int(i * bin_w), self.hist_image.height),
+ (int((i + 1) * bin_w), self.hist_image.height - cv.Round(self.hist.bins[i])),
+ cv.ScalarAll(0), -1, 8, 0)
+
+ cv.ShowImage("histogram", self.hist_image)
+
+if __name__ == "__main__":
+ # Load the source image.
+ if len(sys.argv) > 1:
+ src_image = cv.GetMat(cv.LoadImage(sys.argv[1], 0))
+ else:
+ url = 'https://code.ros.org/svn/opencv/trunk/opencv/samples/c/baboon.jpg'
+ filedata = urllib2.urlopen(url).read()
+ imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1)
+ cv.SetData(imagefiledata, filedata, len(filedata))
+ src_image = cv.DecodeImageM(imagefiledata, 0)
+
+ dh = DemHist(src_image)
+
+ cv.WaitKey(0)