gpt4 book ai didi

python - 使用 OpenCV 测量弯曲线的真实角度

转载 作者:行者123 更新时间:2023-11-30 21:53:16 24 4
gpt4 key购买 nike

我有一个可以弯曲电线的机器人设备,我尝试通过查看所需的真实角度测量值来测量弯曲是否成功。

Original and color adjusted images

我的想法是检测电线并为电线的每一段画一条线,之后它就变成了一个小三角问题。但我找不到绘制和获取这些线的坐标的方法。

我对图像处理非常陌生,所以请原谅我的粗俗术语。

最佳答案

这看起来是一个有趣的问题。我们可以利用弯曲线材和未弯曲线材的垂直边缘强度不同的事实。当我们使用垂直 Sobel 滤波器对其进行滤波时,未弯曲的导线将返回更高的值。

y方向sobel滤波器的结果

enter image description here

通过提取不同强度的区域( Blob )来提取弯曲和未弯曲的线。

Stronger edge will correspond to the unbent wire.

Weaker edge will correspond to the bent wire.

enter image description here enter image description here

通过opencv Blob 分析函数minRectArea,我们可以获得 Blob 的角度、大小和中心位置。

enter image description here

角度值 w.r.t.水平线

Unbent Wire : 3.94 degree

Bent Wire : 21.37 degree

以下是用于获取上述结果的代码:

import cv2
import numpy as np
import matplotlib.pyplot as plt

# extract blob with largest area
def get_best_blob(blobs):
best_blob = None
best_size = 0
for i,blob in enumerate(blobs):
rot_rect = cv2.minAreaRect(blob)
(cx,cy),(sx,sy),angle = rot_rect
if sx * sy >best_size :
best_blob = rot_rect
best_size = sx * sy

return best_blob

def draw_blob_rect(frame,blob,color):
box = cv2.boxPoints(blob)
box = np.int0(box)
frame = cv2.drawContours(frame,[box],0,color,1)

return frame

def process():
image = cv2.imread("wire.png")

# Change to grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

# Reduce size for speed
gray = cv2.resize(gray,(0,0),fx=0.5,fy=0.5)

# Extract wire region
_ , thresh = cv2.threshold(gray,100,255,cv2.THRESH_BINARY_INV)

# morphology kernel
kernel = np.array((
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]
), dtype="int")

# Get y-edges
dy = cv2.Sobel(thresh,cv2.CV_32F,0,1,ksize=21)

# remove negative values
dy = dy * dy

# Normalize it to 255
cv2.normalize(dy,dy,norm_type=cv2.NORM_MINMAX,alpha=255)
dy = dy.astype('uint8')

# Extract relevant information
# Stronger edge is the original part
# Weaker edge is the bended part
_ , strong = cv2.threshold(dy,0.95 * 255,255,cv2.THRESH_BINARY)
_ , mid = cv2.threshold(dy,0.5 * 255,255,cv2.THRESH_BINARY)


# Morphological closing to remove holes
strong_temp = cv2.dilate(strong,kernel, iterations=5)
strong = cv2.erode(strong_temp,kernel, iterations=5)

# remove the strong part from the mid
mid = cv2.subtract(mid,strong_temp)

# Morphological closing to remove holes
mid_temp = cv2.dilate(mid,kernel, iterations=5)
mid = cv2.erode(mid_temp,kernel, iterations=5)

# find the blobs for each bin image
_,strong_blobs,_ = cv2.findContours(strong,cv2.RETR_EXTERNAL ,cv2.CHAIN_APPROX_SIMPLE)
_,mid_blobs,_ = cv2.findContours(mid,cv2.RETR_EXTERNAL ,cv2.CHAIN_APPROX_SIMPLE)

# get the blob with the largest area
best_strong = get_best_blob(strong_blobs)
best_mid = get_best_blob(mid_blobs)

# print the angle
print( "strong_angle",90 + best_strong[2])
print( "mid_angle",90 + best_mid[2])


# Draw the segmented Box region
display_frame = cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR)
display_frame = draw_blob_rect(display_frame,best_strong,(0,0,255))
display_frame = draw_blob_rect(display_frame,best_mid,(0,255,255))

# draw result
cv2.imshow("display_frame",display_frame)
cv2.imshow("mid",mid)
cv2.imshow("strong",strong)
cv2.imshow("image",image)
cv2.imshow("dy",dy)
cv2.waitKey(0)



if __name__=='__main__':
process()

关于python - 使用 OpenCV 测量弯曲线的真实角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59733180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com