ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

OpenCV-Python实现有参照物条件下的长方形物体尺寸推算(可实时、附源码)

2021-05-17 18:01:51  阅读:514  来源: 互联网

标签:img myPoints Python newPoints cv2 OpenCV 源码 A4 np


OpenCV杂谈_04


一. 需要做的前期准备

  1. 环境配置:
    Python版本: 3.7.6
    功能包: opencv-python (4.2.0.32), numpy(1.20.2)
  2. 一张A4纸(尺寸为:宽210mm,长297mm)作为参照物;想要进行测量的长方形物体(放置在A4纸上)之后对放在A4纸上的物体进行拍照保存,或者实时被摄像头拍摄
  3. 由于需要写两个py文件,故需要注意调用时的路径
  4. 一个用的顺手的IDE(本人推荐Pycharm)

二. 源码如下

1.主文件(object_measurement.py)

import cv2
import numpy as np
import Resources.utils  # 注意更改被调用文件的路径

###############################
# 参数设定
webcam = False  # 是否打开电脑自带摄像头或外置摄像头进行实时尺寸推算,如果想要进行实时,将False修改为True即可
path = "Resources/measurement.jpg"  # 存放调用文件的路径
cap = cv2.VideoCapture(0)  # 摄像头
cap.set(3, 1920)  # 摄像头捕捉画面的宽
cap.set(4, 1080)  # 摄像头捕捉画面的高
cap.set(10, 160)  # 摄像头捕捉画面的亮度
scale = 3  # 用于尺寸的缩放   
widthA4 = 210*scale  # A4纸的宽
heightA4 = 297*scale  # A4纸的长
###############################

while True:
    # 选择实时与否
    if webcam:
        success, img = cap.read()
    else:
        img = cv2.imread(path)

    imgContour1, contours1 = Resources.utils.getContours(img, minArea=50000, filter=4) # 抓到画面中面积最大轮廓(即A4纸)
    
    if len(contours1) != 0:
        biggest = contours1[0][2]  # 获取A4纸轮廓的四个角点
        
        # 将画面中的A4纸抓出来
        imgWarp = Resources.utils.warpImg(img, biggest, widthA4, heightA4)
        
        # 抓到A4纸中的长方形物体的轮廓
        imgContour2, contours2 = Resources.utils.getContours(imgWarp, minArea=2000, filter=4,
                                                             cThr=[50, 50], draw=False)  
                                                             
        # 让A4纸中的长方形物体的轮廓看起来更加合适、顺滑
        if len(contours1) != 0:
            for obj in contours2:
                cv2.polylines(imgContour2, [obj[2]], True, (0, 255, 0), 2)

                # 将A4纸中的长方形物体的四个角点,按照:左上 -> 右上 -> 左下 -> 右下 的顺序排列
                newPoints = Resources.utils.reorder(obj[2])

                # 推算A4纸中的长方形物体的长和宽
                newWidth = round((Resources.utils.findDis(newPoints[0][0] // scale, newPoints[1][0] // scale) / 10), 1)
                newHeight = round((Resources.utils.findDis(newPoints[0][0] // scale, newPoints[2][0] // scale) / 10), 1)

                # 在显示时,对A4纸中的长方形物体的长和宽进行标识以及赋予数值
                cv2.arrowedLine(imgContour2, (newPoints[0][0][0], newPoints[0][0][1]),
                                (newPoints[1][0][0], newPoints[1][0][1]),
                                (255, 0, 255), 3, 8, 0, 0.05)
                cv2.arrowedLine(imgContour2, (newPoints[0][0][0], newPoints[0][0][1]),
                                (newPoints[2][0][0], newPoints[2][0][1]),
                                (255, 0, 255), 3, 8, 0, 0.05)
                x, y, w, h = obj[3]
                cv2.putText(imgContour2, '{}cm'.format(newWidth), (x + 30, y - 10), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
                            (255, 0, 255), 2)
                cv2.putText(imgContour2, '{}cm'.format(newHeight), (x - 70, y + h // 2), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
                            (255, 0, 255), 2)
        cv2.imshow("A4", imgContour2)  # 展示推算结果

    img = cv2.resize(img, (0, 0), None, 0.5, 0.5)  # 由于拍摄的图片尺寸较大,将其缩小为原本的 1/2
    cv2.imshow("Original", img)  # 展示用于推算尺寸的图像或摄像机原始镜头所拍摄的情况
    cv2.waitKey(1)
  1. 被调用文件(utils.py)
"""OpenCV-Python utils"""
import cv2
import numpy as np


def getContours(img, cThr=[100,100], showCanny=False, minArea=1000, filter=0, draw =False):
    """获取长方形物体的轮廓,以及按照左上 -> 右上 -> 左下 -> 右下顺序排列的轮廓的四个角点"""
    imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    imgBlur = cv2.GaussianBlur(imgGray, (5, 5), 1)
    imgCanny = cv2.Canny(imgBlur, cThr[0], cThr[1])
    kernel = np.ones((5, 5))
    imgDial = cv2.dilate(imgCanny, kernel, iterations=3)  # 膨胀
    imgThre = cv2.erode(imgDial, kernel, iterations=2)  # 腐蚀
    if showCanny:
        cv2.imshow('Canny', imgThre)
    contours, hiearchy = cv2.findContours(imgThre, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    finalCountours = []
    for i in contours:
        area = cv2.contourArea(i)
        if area > minArea:
            peri = cv2.arcLength(i,True)  # 获取轮廓参数
            approx = cv2.approxPolyDP(i, 0.02*peri, True)  # 获取轮廓的四个角点
            bbox = cv2.boundingRect(approx)  # 给物体添加 bounding box
            if filter > 0:
                if len(approx) == filter:
                    finalCountours.append([len(approx), area, approx, bbox, i])
            else:
                finalCountours.append([len(approx), area, approx, bbox, i])
    finalCountours = sorted(finalCountours, key=lambda x: x[1], reverse=True)  # 将四个角点按照左上 -> 右上 -> 左下 -> 右下顺序排列
    if draw:  # 画出轮廓
        for con in finalCountours:
            cv2.drawContours(img, con[4], -1, (0, 0, 255), 3)
    return img, finalCountours


def reorder(myPoints):
    """由于获取轮廓的角点时是乱序获取的,因此需要将其按照左上 -> 右上 -> 左下 -> 右下的顺序排列"""
    print(myPoints.shape)
    myPointsNew = np.zeros_like(myPoints)
    myPoints = myPoints.reshape((4, 2))
    add = myPoints.sum(1)
    myPointsNew[0] = myPoints[np.argmin(add)]  # 左上
    myPointsNew[3] = myPoints[np.argmax(add)]  # 右下
    diff = np.diff(myPoints, axis=1)
    myPointsNew[1] = myPoints[np.argmin(diff)]  # 右上
    myPointsNew[2] = myPoints[np.argmax(diff)]  # 左下
    return myPointsNew


def warpImg(img, points, width, height, pad = 20):
	"""从画面中抓取出A4纸"""
    points = reorder(points)

    pts1 = np.float32(points)
    pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
    matrix = cv2.getPerspectiveTransform(pts1, pts2)
    imgWarp = cv2.warpPerspective(img, matrix, (width, height))
    imgWarp = imgWarp[pad:imgWarp.shape[0] - pad, pad:imgWarp.shape[1] - pad]  # 去掉抓取结果中的一些多出来的白边
    return imgWarp


def findDis(pts1, pts2):
    """推算A4纸上的长方形物体的长和宽"""
    return ((pts2[0] - pts1[0])**2 + (pts2[1] - pts1[1])**2)**0.5

三. 结果展示

  1. 实时
    by demo
  2. 非实时by demo

四. 感悟与分享

  1. 想要实现单眼相机无参照物情况下的尺寸推算需要更高阶的算法才能实现。
  2. 需要做阴影的去除才能推算出更为精准的结果。
  3. 在进行获取物体轮廓角点时,获取的结果是乱序的,需要做重排序。
  4. 获取完被检测的长方形物体的轮廓角点后,需要根据三角形定理计算被检测物体的长和宽。
  5. 参考课程及推荐:https://www.youtube.com/watch?v=tk9war7_y0Q(内容为英文,且需要翻墙)

如有问题,敬请指正。欢迎转载,但请注明出处。

标签:img,myPoints,Python,newPoints,cv2,OpenCV,源码,A4,np
来源: https://blog.csdn.net/weixin_40247876/article/details/116938487

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有