简体   繁体   中英

Adding text in an image using open cv

I am tracking objects using open cv. I went through a code in the internet. While I am running that it shows some error like this.

Traceback (most recent call last):
  File "/home/pi/Downloads/Pi-tracker-master/obj.py", line 32, in <module>
    cv2.putText(img, str(i+1),font,(x,y+h),(0,255,255))
TypeError: an integer is required (got type tuple)

and I found one answer for that error in stack overflow here but I am not clear about that one.

code I am using :

import cv2
import numpy as np

lowerBound=np.array([33,80,40])
upperBound=np.array([102,255,255])

cam= cv2.VideoCapture(0)
kernelOpen=np.ones((5,5))
kernelClose=np.ones((20,20))

font=cv2.FONT_HERSHEY_SIMPLEX,2,0.5,0,3,1

while True:
    ret, img=cam.read()
    img=cv2.resize(img,(340,220))

    #convert BGR to HSV
    imgHSV= cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    # create the Mask
    mask=cv2.inRange(imgHSV,lowerBound,upperBound)
    #morphology
    maskOpen=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelOpen)
    maskClose=cv2.morphologyEx(maskOpen,cv2.MORPH_CLOSE,kernelClose)

    maskFinal=maskClose
    _,conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

    cv2.drawContours(img,conts,-1,(255,0,0),3)
    for i in range(len(conts)):
        x,y,w,h=cv2.boundingRect(conts[i])
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255), 2)
        cv2.putText(img, str(i+1),font,(x,y+h),(0,255,255))
    cv2.imshow("maskClose",maskClose)
    cv2.imshow("maskOpen",maskOpen)
    cv2.imshow("mask",mask)
    cv2.imshow("cam",img)
    cv2.waitKey(10)

Check the below code

font                   = cv2.FONT_HERSHEY_SIMPLEX
bottomLeftCornerOfText = (10,500)
fontScale              = 1
fontColor              = (255,255,255)
lineType               = 2

cv2.putText(img,'Some text', 
    bottomLeftCornerOfText, 
    font, 
    fontScale,
    fontColor,
    lineType)

You have your parameters in the wrong order. Your tuple (x,y+h) is in the position where a font is expected (fonts are just encoded as integers if I recall correctly). putText expects input in this order:

Python: cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) → None

You just need to match this. For instance:

cv2.putText(img, str(i+1),(x,y+h),font,1,(0,255,255))

Below is the working code with put text. Please take reference from the below and do necessary changes to your code.

import cv2
import numpy as np

lowerBound=np.array([33,80,40])
upperBound=np.array([102,255,255])

cam= cv2.VideoCapture('1.mp4')
kernelOpen=np.ones((5,5))
kernelClose=np.ones((20,20))

font                   = cv2.FONT_HERSHEY_SIMPLEX
bottomLeftCornerOfText = (10,500)
fontScale              = 1
fontColor              = (255,255,255)
lineType               = 2

ret, img = cam.read()
while ret:  
    img=cv2.resize(img,(340,220))
    cv2.putText(img,'Hello World!', 
    bottomLeftCornerOfText, 
    font, 
    fontScale,
    fontColor,
    lineType)
    ret, img=cam.read()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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