【发布时间】:2016-09-04 04:59:12
【问题描述】:
假设我有一个列表列表。例如,
([[[362, 239]], [[362, 367]], [[386, 367]], [[386, 239]]])
如何分别获取第 1 列和第 2 列的最大值?
这个例子的答案应该是:
col1_max = 386, col2_max = 367
我已经试过了:
col1_max = max(cnts.iteritems(), key=operator.itemgetter(1))[0]
print col1_max
但在这里我得到了只是
errorcode: "AttributeError: 'list' object has no attribute 'iteritems'"
更新:
我会用不同的方式重新表述我的问题 现在我想写一个代码来
-
检测对象
-
这些的中心
目前的代码是:
import imutils
import cv2
import os, os.path
DIR = 'pics/'
numberOfPictures = len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])
for i in range(0, numberOfPictures):
image = cv2.imread('pics/' + str(i) + '.png', 0)
thresh = cv2.threshold(image, 60, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
j = 1
for c in cnts:
M = cv2.moments(c)
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
else:
continue
print cnts
j = j + 1
cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
cv2.circle(image, (cX, cY), 7, (255, 255, 255), -1)
cv2.putText(image, "center", (cX - 20, cY - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
cv2.imshow("Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
仅此一项效果很好,但现在我想从 cnts 中获取最大值和最小值。
我使用 cv2.findContours 来创建我的 cnts
cnts = [array([[[362, 239]], [[362, 367]], [[386, 367]], [[386, 239]]])]
如何提取第一列的最大值和第二列的最大值?
【问题讨论】:
-
这实际上是一个列表列表..这是故意的吗?