当前位置: 首页>>代码示例>>Python>>正文


Python Box.meld方法代码示例

本文整理汇总了Python中box.Box.meld方法的典型用法代码示例。如果您正苦于以下问题:Python Box.meld方法的具体用法?Python Box.meld怎么用?Python Box.meld使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在box.Box的用法示例。


在下文中一共展示了Box.meld方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: findRoi

# 需要导入模块: from box import Box [as 别名]
# 或者: from box.Box import meld [as 别名]
def findRoi(filename):

	image = cv.imread(filename)
	image = cv.resize(image, (0, 0), fx = 0.2, fy = 0.2) # TODO: Just set a general bound on image size

	gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
	ret, thresh = cv.threshold(gray,0,255,cv.THRESH_BINARY_INV + cv.THRESH_OTSU)

	LOWAREA_THRESH = 10 
	UPAREA_THRESH = 3000
	contours, heirachy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

	contours = [con for con in contours if LOWAREA_THRESH < cv.contourArea(con) < UPAREA_THRESH]
	contours = [cv.approxPolyDP(con, 10, True) for con in contours]
	contours = [cv.convexHull(con) for con in contours]
	contours = [con for con in contours if len(con) != 1]

	# After initialization, follow this algorithm:
	# 1. Consider a box B in the set.
	# 2. For every other box C in the set
	# 3. If dist(B, C) < THRESHOLD, meld B's set with C's set
	# 4. Repeat for each box C
	# 5. For each disjoint set, meld each box in the set together. These are your clusters.

	objects = []
	for i, con in enumerate(contours):
		x, y, w, h = cv.boundingRect(con)
		box = Box(x, y, w, h)
		objects.append({"box": box, "set": set([i]), "id": i})

	THRESH = 12 
	AREA_THRESH = 200
	SMALL_DIST_THRESH = 15 
	for obj1 in objects:
		for obj2 in objects:

			# If the box is not in box 1's cluster yet
			if obj2["id"] not in obj1["set"]:
				area1 = obj1["box"].area()
				area2 = obj2["box"].area()
				if obj1["box"].dist(obj2["box"]) < THRESH:

				# TODO: Need to fix these bugs
				#	if obj1["box"].dist(obj2["box"]) != obj2["box"].dist(obj1["box"]):
				#		print "WARNING: Antisymmetric distance for {0} and {1}".format(obj1["id"], obj2["id"])
				#		print "Distances are: {0} and {1}".format(obj1["box"].dist(obj2["box"]), obj2["box"].dist(obj1["box"]))
				#		print "{0}, {1}".format(obj1["box"], obj2["box"])
					
					obj1["set"] = obj1["set"].union(obj2["set"])
					for id in obj1["set"]:
						objects[id]["set"] = obj1["set"] # Shallow copy; all point to obj1's set
	

	# Groups the members of the clusters together
	clusters = set([tuple(obj["set"]) for obj in objects])
	outputBoxes = []
	for tup in clusters:
		boxes = [objects[i]["box"] for i in tup]
		outputBoxes.append(Box.meld(*boxes))
	outputBoxes = [box.getContourRep() for box in outputBoxes]

	PADDING = 5
	return [image[box[2][0][1]-PADDING:box[0][0][1]+PADDING, box[1][0][0]-PADDING:box[0][0][0]+PADDING] for box in outputBoxes]
开发者ID:hlin117,项目名称:mahjongCV,代码行数:65,代码来源:roi.py


注:本文中的box.Box.meld方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。