python cv2 puttext - 显示变量时出错

python cv2 puttext - error while showing the variable

我正在编写我的第一个代码,我想在图像上显示变量。从文本文件中读取变量。 这是我的部分代码:

font=cv2.FONT_HERSHEY_SIMPLEX
fontScale=2
fontColor=(255,255,255)
lineType=2;

def line11():
    cv2.ellipse(img, (443,350), (35,35), 0, 0, -180, (0,0,255), 4);
    cv2.putText(img, x,(443,320),fontScale, (255,255,255),lineType)

从 texfile 读取值 x:

with open('US1.txt') as f1, open('US2.txt') as f2: 
    for x, y in zip(f1,f2):
        x = x.strip()
        y = y.strip()
        print("{0} {1}".format(x, y))

不幸的是,我得到了错误:

TypeError                                 
Traceback (most recent call last)
<ipython-input-2-bc1d3b9f83c2> in <module>
    130 
    131         if (float(x) <=0.5):
--> 132             line11();
    133 
    134         elif (0.5< float(x)<=1):

<ipython-input-2-bc1d3b9f83c2> in line11()
     16     cv2.ellipse(img, (443,350), (35,35), 0, 0, -180, (0,0,255), 4);
     17 
---> 18     cv2.putText(img, x, (443,350),fontScale, (255,255,255),lineType)
     19 
     20 def line12():

TypeError: must be real number, not tuple

我找不到解决办法。我尝试了很多选项(即 x 的 chinging 类型),现在我无能为力了。有人可以向我解释吗? 谢谢!

我猜这个错误是因为你忘了在 putText 函数中提到字体。它得到的元组是 (255,255,255),而它期望 fontScale.

cv2.putText(图像、文本、组织、字体、fontScale、颜色[、粗细[、线型[、bottomLeftOrigin]]])

尝试: cv2.putText(img, x,(443,320),字体,fontScale,fontColor,lineType)

您错过了 font 论点。试试这个:

font=cv2.FONT_HERSHEY_SIMPLEX
fontScale=2
fontColor=(255,255,255)
lineType=cv2.line_AA
org=(443,320)
text = str(x)

cv2.putText(img, text,org,font,fontScale,fontColor,lineType)

参考here