如何通过序列设置数组元素?

时间:2019-03-30 10:57:35

标签: python

我的问题很简单。我有中心值和半径值。我想将这些值分别放入中心和半径数组。

centers[0][i], radius[0][i] = cv.minEnclosingCircle(contours_poly[i])

运行代码时,我在这一行中看到“设置具有序列的数组元素”。

from __future__ import print_function
import cv2 as cv
import numpy as np
import argparse
import random as rng
rng.seed(12345)

threshold = 100
parser = argparse.ArgumentParser(description='Code for Creating Bounding boxes and circles for contours tutorial.')
parser.add_argument('--input', help='Path to input image.', default='stuff.jpg')
args = parser.parse_args()
src = cv.imread('training3.jpg')
if src is None:
    print('Could not open or find the image:', args.input)
    exit(0)
# Convert image to gray and blur it
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
src_gray = cv.blur(src_gray, (3,3))
source_window = 'Source'
cv.namedWindow(source_window)
cv.imshow(source_window, src)
canny_output = cv.Canny(src_gray, threshold, threshold * 2)

contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

contours_poly = [None]*len(contours)
boundRect = [None]*len(contours)
centers = np.zeros([2,len(contours)])
radius = np.zeros([1,len(contours)])

for i, c in enumerate(contours):
   contours_poly[i] = cv.approxPolyDP(c, 3, True)
   boundRect[i] = cv.boundingRect(contours_poly[i])
   centers[0][i], radius[0][i] = cv.minEnclosingCircle(contours_poly[i])

在cv.minEnclosingCircle(contours_poly [i])里面是:

cv.minEnclosingCircle(contours_poly[0]) = ((688.5, 958.5), 2.9155757427215576)

例如,我想获得以下结果:

centers[0] =  (688.5, 958.5)
radius[0] = 2.9155757427215576

如何解决此问题?谢谢您的帮助:)

0 个答案:

没有答案