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)
"""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