OpenCV-Python实现有参照物条件下的长方形物体尺寸推算(可实时、附源码)
2021/5/17 20:27:04
本文主要是介绍OpenCV-Python实现有参照物条件下的长方形物体尺寸推算(可实时、附源码),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
OpenCV杂谈_04
一. 需要做的前期准备
- 环境配置:
Python版本: 3.7.6
功能包: opencv-python (4.2.0.32), numpy(1.20.2) - 一张A4纸(尺寸为:宽210mm,长297mm)作为参照物;想要进行测量的长方形物体(放置在A4纸上)之后对放在A4纸上的物体进行拍照保存,或者实时被摄像头拍摄
- 由于需要写两个py文件,故需要注意调用时的路径
- 一个用的顺手的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)
- 被调用文件(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
三. 结果展示
- 实时
- 非实时
四. 感悟与分享
- 想要实现单眼相机无参照物情况下的尺寸推算需要更高阶的算法才能实现。
- 需要做阴影的去除才能推算出更为精准的结果。
- 在进行获取物体轮廓角点时,获取的结果是乱序的,需要做重排序。
- 获取完被检测的长方形物体的轮廓角点后,需要根据三角形定理计算被检测物体的长和宽。
- 参考课程及推荐:https://www.youtube.com/watch?v=tk9war7_y0Q(内容为英文,且需要翻墙)
如有问题,敬请指正。欢迎转载,但请注明出处。
这篇关于OpenCV-Python实现有参照物条件下的长方形物体尺寸推算(可实时、附源码)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python