OpenCV - How to draw a line inside contour?

痞子三分冷 提交于 2020-07-07 06:53:39

问题


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)

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.


回答1:


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:

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)

Keywords: Image processing, Python, OpenCV, skimage, scikit-image, medial axis, skeleton, skeletonisation.



来源:https://stackoverflow.com/questions/59911735/opencv-how-to-draw-a-line-inside-contour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!