# ■ 背景

▲ 机械爪在命令控制下张开与闭合

# 01图像处理

## 1.基本方案

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# TEST1.PY                     -- by Dr. ZhuoQing 2020-08-13
#
# Note:
#============================================================
from headm import *
from tsmodule.tsstm32       import *
setnum = linspace(80, 480, 200, endpoint=False)
printf(setnum)
gifid = 2
tspgiffirst(gifid)
for sn in setnum:
stm32cmd('pwm %d'%sn)
time.sleep(1)
tspgifappend(gifid)
printf(sn)
#------------------------------------------------------------
#        END OF FILE : TEST1.PY
#============================================================


## 2.测量距离

#### (1) 获得边缘图

▲ 使用Canny算法获得图片的边缘

#### (2) 所有采集到图形的边缘图

for f in range(filenumber):
fn = os.path.join(filepath, '%04d.BMP'%f)
img = cv2.imread(fn)[:,:,1]
edges = cv2.Canny(img, sv1, sv2)


#### (3) 获得机械爪间距

#------------------------------------------------------------
def edgewidth(edge):
edges = edge[320:350,:]
edgesadd = sum(edges, 0)

edgesadd = [int(s) for s in list(edgesadd > 0)]

firstid = edgesadd.index(1) + 50
endid = len(edgesadd) - edgesadd[::-1].index(1) - 50

inneredge = edgesadd[firstid:endid]
leftid = inneredge.index(1)
rightid = len(inneredge) - inneredge[::-1].index(1)

width = rightid - leftid
return width


▲ 机械爪之间的距离的变化

# 02理论分析

▲ 机械爪运动坐标系

• L= 2 cm;
• M = 2.5cm;

▲ 角度与机械爪之间的距离

# 03结果分析

• 来自于机械装配之间的随机抖动；
• 来自于舵机角度的起始位置与终止位置的误差；

# ※ 代码

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# MEASURE1.PY                  -- by Dr. ZhuoQing 2020-08-13
#
# Note:
#============================================================

from headm import *
from PIL                    import Image
import cv2
from tsmodule.tsdraw        import *

gifid = 2

filename = tspgetdopfile(gifid)
filenumber = tspgetgifpage(gifid)
filepath = os.path.dirname(filename)

#------------------------------------------------------------
def edgewidth(edge):
edges = edge[320:350,:]
edgesadd = sum(edges, 0)

edgesadd = [int(s) for s in list(edgesadd > 0)]

firstid = edgesadd.index(1) + 50
endid = len(edgesadd) - edgesadd[::-1].index(1) - 50

inneredge = edgesadd[firstid:endid]
leftid = inneredge.index(1)
rightid = len(inneredge) - inneredge[::-1].index(1)

width = rightid - leftid
return width

#------------------------------------------------------------
'''

img = cv2.imread(filename)[:,:,::-1]

edges = cv2.Canny(img, 300, 400)[320:350,:]

edgesadd = sum(edges, 0)

edgesadd = [int(s) for s in list(edgesadd > 0)]

firstid = edgesadd.index(1) + 50
endid = len(edgesadd) - edgesadd[::-1].index(1) - 50

inneredge = edgesadd[firstid:endid]
leftid = inneredge.index(1)
rightid = len(inneredge) - inneredge[::-1].index(1)

width = rightid - leftid

printff(leftid, rightid, width)

#printf(edgesadd)

#printf(shape(edges))

plt.subplot(211), plt.imshow(img)
plt.subplot(212), plt.imshow(edges, cmap='gray')
plt.xticks([]), plt.yticks([])
plt.show()

exit()

'''
#------------------------------------------------------------
pltgif = PlotGIF()

plt.draw()
plt.pause(.001)

sv1 = 300
sv2 = 400

widthdim = []

for f in range(filenumber):
fn = os.path.join(filepath, '%04d.BMP'%f)
img = cv2.imread(fn)[:,:,1]
edges = cv2.Canny(img, sv1, sv2)
widthdim.append(edgewidth(edges))

printff(fn, widthdim[-1])

#    plt.clf()
#    plt.imshow(edges, cmap='gray')
#    plt.title('Canny(img, %d, %d), Pages:%04d'%(sv1,sv2,f))

#    plt.xticks([]), plt.yticks([])
#    plt.draw()
#    plt.pause(.001)
#    pltgif.append(plt)
#    printf(fn)

#pltgif.save(r'd:\temp\1.gif')

printf('\a')
#plt.show()

plt.plot(list(range(filenumber)), widthdim)
plt.xlabel("Sample")
plt.ylabel("Width")
plt.grid(True)
plt.tight_layout()
plt.show()

#------------------------------------------------------------
#        END OF FILE : MEASURE1.PY
#============================================================


★ 相关的博文应用：

07-31 4389

11-10 5万+
04-22 127
05-04 2040
01-11 86
05-11 2万+
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie