opencv 实现等值线_使用opencv和python进行激光曲线检测

I have taken out the laser curve of this image :

And now, I'm trying to obtain a set of points (the more, the better), which are in the middle of this curve.

I have tried to split the image into vertical stripes, and then to detect the centroid.

But it doesn't calculate lots of points, and it's not satisfactory at all !

img = cv2.Canny(img,50,150,apertureSize = 3)

sub = 100

step=int(img.shape[1]/sub)

centroid=[]

for i in range(sub):

x0= i*step

x1=(i+1)*step-1

temp = img[:,x0:x1]

hierarchy,contours,_ = cv2.findContours(temp, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

if contours <> []:

for i in contours :

M = cv2.moments(i)

if M['m00'] <> 0:

centroid.append((x0+int(M['m10']/M['m00']),(int(M['m01']/M['m00']))))

I also tried cv2.fitLine(), but it wasn't satisfactory either.

How could I detect points in the middle of this curve efficiently ? regards.

解决方案

I think you are getting fewer points because of the following two reasons:

using an edge detector: depending on the thresholds, sometimes the edges may not reasonably represent the curve

sampling the image using a large step

Try the following instead.

# threshold the image using a threshold value 0

ret, bw = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY)

# find contours of the binarized image

contours, heirarchy = cv2.findContours(bw, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

# curves

curves = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)

for i in range(len(contours)):

# for each contour, draw the filled contour

draw = np.zeros((img.shape[0], img.shape[1]), np.uint8)

cv2.drawContours(draw, contours, i, (255,255,255), -1)

# for each column, calculate the centroid

for col in range(draw.shape[1]):

M = cv2.moments(draw[:, col])

if M['m00'] != 0:

x = col

y = int(M['m01']/M['m00'])

curves[y, x, :] = (0, 0, 255)

I get a curve like this:

You can also use distance transform and then get the row associated with max distance value for each column of individual contours.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值