奥比中光相机为了减少干扰像素点,先通过裁剪RGB图片,再利用Canny边缘算子,获取目标边缘轮廓,求取裁剪后图片的质心坐标,并还原至原RGB图,得到真实质心像素坐标,再通过pcd点云根据相机内参获取深度图,筛选深度图后得到质心的真实坐标。
# -*- coding: utf-8 -*- import numpy as np import cv2 import matplotlib.pyplot as plt from openni import openni2 import open3d as o3d def imge_show(imgCanny, imgContour): contours, hierarchy = cv2.findContours(imgCanny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # 寻找轮廓点 point = [] for obj in contours: area = cv2.contourArea(obj) # 计算轮廓内区域的面积 # 筛选非闭合轮廓 if area > 50: cv2.drawContours(imgContour[128:350, 128:450], obj, -1, (255, 0, 0), 3) # 绘制轮廓线 perimeter = cv2.arcLength(obj, True) # 计算轮廓周长 approx = cv2.approxPolyDP(obj, 0.02 * perimeter, True) # 获取轮廓角点坐标 # CornerNum = len(approx) # 轮廓角点的数量 # x, y, w, h = cv2.boundingRect(approx) # 获取坐标值和宽度、高度 # 1:近似矩形的左上角点坐标、矩形的宽和高(x, y, w, h) # 2:近似矩形的左上角点坐标(x, y),矩形的宽和高(w, h)及旋转角度θ(°) # 3:转换为矩形的四个角点坐标 rect = cv2.minAreaRect(approx) x, y, w, h = cv2.boxPoints(rect) # 还原至真实像素坐标 point1 = [x[0] + 128, x[1] + 128] point2 = [y[0] + 128, y[1] + 128] point3 = [w[0] + 128, w[1] + 128] point4 = [h[0] + 128, h[1] + 128] # print('坐标,宽度,高度', point1, point2, point3, point4)