Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

python - OpenCV - How to draw a line inside contour?

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:enter image description here

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:enter image description here

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:enter image description here

Is there a way to do that and take that line's coordinates? Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

enter image description here

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)

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...