• openCV对识别出的目标对象标号(putText函数)


      示例代码:

      就像下面这段代码,直接利用cv.putText函数即可,注意数字需要转化成字符型!

      # 提取轮廓

      # findContours函数会修改原始图像

      cnts = cv.findContours(cannyImg.copy(), mode=cv.RETR_EXTERNAL, method=cv.CHAIN_APPROX_SIMPLE)

      # print(cnts)

      cnts = imutils.grab_contours(cnts)

      numCnts = contours.sort_contours(cnts, method="left-to-right")[0] ##排序,从左到右将轮廓排列

      total = 0

      num=1

      for (i,c) in enumerate(numCnts):

      #print(i)

      # 直边界矩形一个直矩形(就是没有旋转的矩形)。它不会考虑对象是否旋转。所以边界矩形的面积不是最小的。

      # (x,y)是矩形左上角坐标,(w,h)是矩形宽和高

      (x, y, w, h) = cv.boundingRect(c)

      #限制条件,剔除一些不想要的轮廓

      if cv.contourArea(c) > 80 or cv.contourArea(c) < 40 or w >= 15 or h >= 15:

      continue大连做人流多少钱 http://www.120wtrlyy.com/

      #轮廓面积

      # print(cv.contourArea(c))

      # print('w=',w,'h=',h)

      #轮廓位置

      print('(%d,%d)' % ((x + h) / 2, (y + w) / 2))

      # 在原图上描出轮廓,因为原图是三通道的,轮廓的圈显示的是红色,若是在灰度模式下读的图片上直接勾勒轮廓那就不是红色了,不利于观察!

      cv.drawContours(original, [c], -1, (0, 0, 255), 1)

      # cv.putText各参数依次是:图片,添加的文字,左上角坐标,字体,字体大小,颜色,字体粗细

      #用num而不用i,输出一下i就知道会受到其他被排出的轮廓的干扰!

      cv.putText(original,str(num), (x, y - 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)

  • 相关阅读:
    CS229 6.4 Neurons Networks Autoencoders and Sparsity
    CS229 6.3 Neurons Networks Gradient Checking
    【Leetcode】【Easy】Min Stack
    【Leetcode】【Easy】Merge Sorted Array
    【Leetcode】【Easy】ZigZag Conversion
    【Leetcode】【Easy】Valid Palindrome
    【Leetcode】【Easy】Reverse Integer
    【Leetcode】【Easy】Palindrome Number
    【Leetcode】【Easy】Length of Last Word
    【Leetcode】【Easy】Remove Nth Node From End of List
  • 原文地址:https://www.cnblogs.com/djw12333/p/14549880.html
Copyright © 2020-2023  润新知