50字范文,内容丰富有趣,生活中的好帮手!
50字范文 > 水下机器人基于双目测距原理测量鱼长

水下机器人基于双目测距原理测量鱼长

时间:2018-11-13 03:57:59

相关推荐

水下机器人基于双目测距原理测量鱼长

这是我发表的第一篇文章,在学习前人和师兄的基础上完成的。

硬件:双目相机

因为是水下机器人拍的,借用了树莓派作为图像处理的平台

效果图:

实现思路:视差-距离-长度

图像前期处理:HSV阈值切割进行边缘检测,然后通过视差获取图像形心到机器人的距离,再根据三角形相似比得到最终鱼长。

1、图像前期处理:

# 图像前期处理def color_filter(img, Lower, Upper, k_e, k_d): # k_e为腐蚀处理的卷积核,k_d为膨胀处理的卷积核HSV_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)if Lower[0] > Upper[0]:mask1 = cv2.inRange(HSV_img, Lower, 180)mask2 = cv2.inRange(HSV_img, 0, Upper)mask = cv2.bitwise_and(mask1, mask2)else:mask = cv2.inRange(HSV_img, Lower, Upper)kernel_e = np.ones((k_e, k_e), np.uint8) # 卷积核设置erosion = cv2.erode(mask, kernel_e, iterations=3) # 腐蚀处理kernel_d = np.ones((k_d, k_d), np.uint8) # 卷积核设置dilation = cv2.dilate(erosion, kernel_d, iterations=3) # 膨胀处理img_maskcolor = dilationreturn img_maskcolor

2、视差图获取图像形心到机器人距离:

其中有一些参数需要根据相机情况和检测目标进行一定的更改。

# 图像处理img_binary = color_filter(frame, Lower, Upper, k_e, k_d)img_binary_split = np.hsplit(img_binary, 2)img_binary_1_split = img_binary_split[0]img_binary_2_split = img_binary_split[1]sample_flag = 1if sample_flag == 1:img_binary_1_split_down = cv2.pyrDown(img_binary_1_split)img_binary_2_split_down = cv2.pyrDown(img_binary_2_split)# 是否进行下采样contours1, _ = cv2.findContours(img_binary_1_split_down, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)contours2, _ = cv2.findContours(img_binary_2_split_down, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)x_array_1 = []y_array_1 = []len_array_1 = list()x_array_2 = []y_array_2 = []len_array_2 = list()# 求形心for contour1 in contours1:if cv2.contourArea(contour1) > 50:M = cv2.moments(contour1)# print("len:" + str(len(approx)))# cv2.drawContours(img, [approx], 0, (0, 0, 0), 5) # 绘制轮廓线# if M['m00'] < 0.5:# continuex = int(M['m10'] / M['m00']) # 廓线坐标y = int(M['m01'] / M['m00']) # 廓线坐标if (y < 360 * (2 - sample_flag)) & (y > 120 * (2 - sample_flag)) & (x > 120 * (2 - sample_flag)) & (x < 520 * (2 - sample_flag)) & (cv2.contourArea(contour1) > 100):x_array_1.append(x)y_array_1.append(y)cv2.circle(img_binary_1_split_down, (x, y), 10, 2)x_array_1_ch = np.array(x_array_1)for contour2 in contours2:if cv2.contourArea(contour2) > 50:M = cv2.moments(contour2)if M['m00'] < 0.5:continuex = int(M['m10'] / M['m00']) # 廓线坐标y = int(M['m01'] / M['m00']) # 廓线坐标if (y < 360 * (2 - sample_flag)) & (y > 120 * (2 - sample_flag)) & (x > 120 * (2 - sample_flag)) & (x < 520 * (2 - sample_flag)) & (cv2.contourArea(contour2) > 100): # 1-120;3-100x_array_2.append(x)y_array_2.append(y)cv2.circle(img_binary_2_split_down, (x, y), 10, 2)x_array_2_ch = np.array(x_array_2)

3、计算长度还需要获取图像在图片中占的像素数,这里我们简化为最小外切圆的直径

#寻找最小外切圆直径作为像素长度def find_marker(img):#imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)ret, thresh = cv2.threshold(img, 127, 255, 0)contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)cv2.drawContours(img, contours, -1, (0, 0, 255), )cv2.imshow('img', img)for contour1 in contours:area = []# 找到最大的轮廓for k in range(len(contours)):area.append(cv2.contourArea(contours[k]))max_idx = np.argmax(np.array(area))#print(max_idx)# 找最小外接圆(x, y), radius = cv2.minEnclosingCircle(contours[max_idx])# 最小外接圆半径radius = int(radius)# print(radius)return radius

最终通过相似比获取机器人长度较为简单,就不在这罗列代码了。

在做的过程中遇到了一些问题,首先是在测距前需要先用已知长度的物体放在一个合适的距离进行标定,对代码中一些相关的系数进行更改。然后嘛,阈值慢慢调,总能切出来的(手动狗头)

第一次写文章,时间也比较匆忙,有很多表述不清楚的地方欢迎大家指点。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。