zoukankan      html  css  js  c++  java
  • python-opencv boundingRect使用注意

    矩形边框(Bounding Rectangle)是说,用一个最小的矩形,把找到的形状包起来。还有一个带旋转的矩形,面积会更小,效果见下图

    Bounding Rectangle

    上代码

    首先介绍下cv2.boundingRect(img)这个函数

    这个函数很简单,img是一个二值图,也就是它的参数;

    返回四个值,分别是x,y,w,h;

    x,y是矩阵左上点的坐标,w,h是矩阵的宽和高

    然后利用cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)画出矩行

    参数解释

    第一个参数:img是原图

    第二个参数:(x,y)是矩阵的左上点坐标

    第三个参数:(x+w,y+h)是矩阵的右下点坐标

    第四个参数:(0,255,0)是画线对应的rgb颜色

    第五个参数:2是所画的线的宽度

    # 用绿色(0, 255, 0)来画出最小的矩形框架
    x, y, w, h = cv2.boundingRect(cnt)
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
    
    # 用红色表示有旋转角度的矩形框架
    rect = cv2.minAreaRect(cnt)
    box = cv2.cv.BoxPoints(rect)
    box = np.int0(box)
    cv2.drawContours(img, [box], 0, (0, 0, 255), 2)
    cv2.imwrite('contours.png', img)
    但是要是在Python中使用,没有vector或者mat作为boundingRect的输入,会出现以下报错:
        x, y, w, h = cv2.boundingRect(landmarks)
    TypeError: points is not a numpy array, neither a scalar
    上面的landmark作为输入是一个list,
    解决方案:
    因此需要引入numpy对他进行强转,具体操作如下:

    import  numpy as np
    
     x, y, w, h = cv2.boundingRect(np.array(landmarks))
      cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

    这样就可以把list转成array。







  • 相关阅读:
    java.util.concurrent学习
    mysql慢查优化总结
    mysql怎么限制某些查询语句的执行?
    数据库操作提交事务如果不关闭,会有什么样的后果?
    apache的500错误是写到哪个文件里面
    apache也可以做负载均衡,跟nignx的区别是什么?
    ajax提交请求为啥url要用这个函数encodeURI
    MySQL性能调优与架构设计读书笔记
    java枚举的作用
    linux的命令
  • 原文地址:https://www.cnblogs.com/Anita9002/p/8033101.html
Copyright © 2011-2022 走看看