简体   繁体   English

Python OpenCV cv2.putText() - 如何 label 多个形状与不同的文本?

[英]Python OpenCV cv2.putText() - how to label multiple shapes with variate text?

I'm having an issue labeling my shapres with text.我在用文本标记我的形状时遇到问题。 The image pixels and the values of the texts are all from a csv file.图像像素和文本值均来自 csv 文件。 I tried the following and I'm getting the wrong output.我尝试了以下方法,但我得到了错误的 output。 The circles are correct but the text is wrong.圆圈是正确的,但文字是错误的。 As if I'm reading/printing all the string at each coordinate (which is circle).好像我正在读取/打印每个坐标(即圆形)处的所有字符串。

dfa = pd.read_csv("filter.csv")
image = cv2.imread("image.png")
dfx = dfa[dfa['Timestamp'] == 1202000000]
dfx['Error_Bool'] = dfx['Percentage_Error']>=50
dfx['Error'] = dfx['Percentage_Error'].astype('int32')
text = dfx['Error'].to_numpy()
dfx = dfx[dfx['Error_Bool'] == False]
dfx = dfx[['Project_image_X', 'Project_image_Y']]
arr_2 = dfx.to_numpy()
for item_2 in arr_2:
    image = cv2.circle(image, (item_2[0], item_2[1]), 20, (0, 255, 0), 2)
    image = cv2.putText(image, "{}".format(text), (item_2[0], item_2[1]), 
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1, cv2.LINE_AA)
cv2.imshow('test image', image)
cv2.waitKey(0)

输出

The problem was with dfx['Percentage_Error'] = str(dfx['Percentage_Error']).问题在于 dfx['Percentage_Error'] = str(dfx['Percentage_Error'])。 It seemed to convert the entire Percentage Error column into a string and then store that same string in every row of dfx['Percentage_Error'].它似乎将整个百分比错误列转换为一个字符串,然后将相同的字符串存储在 dfx['Percentage_Error'] 的每一行中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM