【问题标题】:How to draw contour on image from csv file如何在csv文件中的图像上绘制轮廓
【发布时间】:2023-03-06 00:55:01
【问题描述】:

我需要从 CSV 文件中绘制轮廓,我单独获取每个 x、y 列表,但我需要创建元组以将它们用于折线函数,但我得到 'int' object is not iterable

代码:

import numpy as np
import cv2
import matplotlib.pyplot as plt
image5= cv2.imread("C:/Users/Pc/Desktop/code prjt/5_Vesicule.jpg")
cv2.imshow("im",image5)
cv2.waitKey()
centrev5 = pd.read_csv("C:/Users/Pc/Desktop/code prjt/centrev5.csv",sep=";")
print(centrev5)
cx=int(centrev5.get(['centreX'][0]) )
cy =int(centrev5.get(['centreY'][0]))
image = cv2.circle(image5, (cx,cy), radius=0, color=(255, 0, 0), thickness=10)
cv2.imshow("centre",image)
cv2.waitKey()

point5 = pd.read_csv("C:/Users/Pc/Desktop/code prjt/point5.csv",sep=";")
print(point5)

for i in range(len(point5)):
    x=int(point5.values[i][1])
    y=int(point5.values[i][2])
   # transform x,y vectors to tuples
    c = [tuple(x), tuple(y)]
    img = cv2.polylines(image5, c, 1, (0,0,255),4)


cv2.imshow("contour",image5)
cv2.waitKey()


#print(c)

#c = [i for i in zip(x, y)]
#c = list(zip(x,y)) 
#[(x[i],y[i]) for i in range(min(len(x),len(y)))]

the error is 'int' object is not iterable

result of x,y

【问题讨论】:

  • cv2.polylines(image5, zip(x,y), 1, (0,0,255),4) 能解决您的问题吗?
  • 不,问题出在 c = [tuple(x), tuple(y)]
  • 尝试打印 x 和 y 并更新您的问题以包含输出
  • 我明白了,我建议您评论该行 c = ... 并将您的 cv2.pol.... 行替换为我之前提到的内容
  • 谢谢你的帮助,我照你说的做了,但还是一样的错误

标签: python numpy csv opencv-drawcontour


【解决方案1】:

您的问题是您正在尝试将 int 转换为元组她 tuple(x)。 x 被定义为intints 不可迭代。所以它不能直接转换为tuple。如果您想要一个只包含x 的touple,请使用(x,)(注意尾随,)。

cv2.polylines() 期望它的第二个参数采用格式

[(x1,y1),(x2,y2),...)]

然后将绘制一个多边形连接列表中的所有点。

这意味着您不需要 for 循环。它已经集成在cv2.polylines() 中。见Here

为了将您的数据转换为正确的格式,我建议如下:

points = [(value[1],value[2]) for value in point5.values]

然后像这样画线:

cv2.polylines(image5, points, 1, (0,0,255),4)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 2020-11-24
    • 2021-09-07
    相关资源
    最近更新 更多