Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

引入

在准备工训实践大赛的过程中,你一定被机械臂困扰过吧?得到了一个物体的框了,应该让机械臂旋转多少度才能更好的抓取物体呢?例如,这里有一个矿泉水瓶。

image-20250315150550940

我们应该让机械臂旋转到刚好能抓到瓶子的“腰上”,而不是从瓶盖抓到瓶身,因为我们的机械夹爪可能没这么长。

下面这个视频是我们当时做的效果。

实现

原理十分简单,通过轮廓检测算子得到物体的轮廓,然后进行线性回归得到w和b即可,最后对w取反正切函数变为角度值。

image-20250315152116737

image-20250315151359796

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import cv2
import numpy as np
def calcAng(img):
edges = cv2.Canny(img, 100, 200)
w, h, _ = image.shape
y, x = np.where(edges)
X = np.stack((x,), axis=-1)
A = np.hstack((np.ones((X.shape[0], 1)), X))
w = np.linalg.lstsq(A, y, rcond=None)[0]

theta = np.arctan(-w[::-1][0]) * 180 / np.pi
if h>w and theta<30:
return 90
return theta

调用方法

1
theta = calcAng(img)

非常简单

评论