Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 225
Onur Demirci Avatar asked Oct 27 '25 09:10

Onur Demirci


1 Answers

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.

like image 102
Mark Setchell Avatar answered Oct 30 '25 00:10

Mark Setchell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!