简体   繁体   English

OpenCV - 如何在轮廓内画一条线?

[英]OpenCV - How to draw a line inside contour?

I'm working on a DIY 3d Scanner project.我正在做一个 DIY 3d 扫描仪项目。 I'll use a pretty common algorithm for it.我将使用一个非常常见的算法。
See here: https://lesagegp.wordpress.com/2013/12/04/laser-scanning-explained/见这里: https : //lesagegp.wordpress.com/2013/12/04/laser-scanning-explained/
I've totally understood the algorithm and wrote a code for it.我已经完全理解算法并为它编写了代码。 All I got to do now is processing the images.我现在要做的就是处理图像。 I've captured couple images for testing.我已经拍摄了几张图片进行测试。 Here is one of them:这是其中之一: 在此处输入图片说明

And I've managed to find contours of the laser with a very simple code:我设法用一个非常简单的代码找到了激光的轮廓:

image = cv2.imread("frame/1.png")
image = cv2.flip(image, 1)
hsv_frame = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
low_red = np.array([161, 155, 84])
high_red = np.array([179, 255, 255])
red_mask = cv2.inRange(hsv_frame, low_red, high_red)
contour = cv2.findContours(red_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[0]
draw_it = cv2.drawContours(image, contour, -1, (0, 255, 0), 3)

cv2.imshow("contour",draw_it)

Result:结果: 在此处输入图片说明

And right now all I want to do is drawing a polyline or something like that inside of contour or inner edge of contour.现在我想要做的就是在轮廓内部或轮廓的内边缘绘制多段线或类似的东西。 Like a blue line in this example:就像这个例子中的蓝线: 在此处输入图片说明

Is there a way to do that and take that line's coordinates?有没有办法做到这一点并采用该线的坐标? Thanks in advance.提前致谢。

Let's start with a slightly trimmed version of your contour image - which I happen to have generated by other means because your code didn't run on my OpenCV version:让我们从轮廓图像的略微修剪版本开始 - 我碰巧通过其他方式生成了它,因为您的代码没有在我的OpenCV版本上运行:

在此处输入图片说明

I would then read this as greyscale, and use skimage function medial_axis() to find the medial axis like this:然后我会将其读取为灰度,并使用skimage函数medial_axis()找到像这样的中轴:

import cv2
from skimage.morphology import medial_axis

# Load your trimmed image as greyscale
image = cv2.imread("a.png", cv2.IMREAD_GRAYSCALE)

# Find medial axis
skeleton = medial_axis(image).astype(np.uint8)

# Save
cv2.imwrite("result.png", skeleton*255)

在此处输入图片说明

Keywords : Image processing, Python, OpenCV, skimage, scikit-image, medial axis, skeleton, skeletonisation.关键词:图像处理、Python、OpenCV、skimage、scikit-image、中轴、骨架、骨架化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM