OpenCV-如何在轮廓内画一条线? [英] OpenCV - How to draw a line inside contour?
问题描述
我正在研究DIY 3d扫描仪项目.我将使用一种非常通用的算法.
请参见此处:
I'm working on a DIY 3d Scanner project. I'll use a pretty common algorithm for it.
See here: 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)
结果:
现在我要做的就是在轮廓内部或轮廓内部边缘绘制一条折线或类似的东西.就像本例中的蓝线一样:
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.
推荐答案
让我们从略微修整的轮廓图像开始-我碰巧是通过其他方式生成的,因为您的代码未在我的上运行OpenCV 版本:
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:
然后我将其读取为灰度,并使用 skimage 函数medial_axis()
查找中间轴,如下所示:
I would then read this as greyscale, and use skimage function medial_axis()
to find the medial axis like this:
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)
关键字:图像处理,Python,OpenCV,skimage,scikit图像,中间轴,骨架,骨架.
Keywords: Image processing, Python, OpenCV, skimage, scikit-image, medial axis, skeleton, skeletonisation.
这篇关于OpenCV-如何在轮廓内画一条线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!