forked from chicagoedt/revo_robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskeletonize.py
executable file
·84 lines (67 loc) · 2.55 KB
/
skeletonize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
import sys
import rospy
from dynamic_reconfigure.server import Server
from lane_detection import LaneDetection
from line_detection.cfg import LineDetectionConfig
import cv2
import numpy as np
###############################################################################
# Chicago Engineering Design Team
# Skeletonize filter using Python OpenCV for autonomous robot Scipio
# (IGVC competition).
#
# This node "skeletonizes" input images by iteratively eroding the pixels until
# they are a single-pixel thick. This makes it easier to apply line-fitting or
# hough transforms on thick lines.
#
# @author Basheer Subei
# @email [email protected]
class Skeletonize(LaneDetection):
roi_top_left_x = 0
roi_top_left_y = 0
roi_width = 2000
roi_height = 2000
max_erode_iterations = 150
def __init__(self, namespace, node_name):
LaneDetection.__init__(self, namespace, node_name)
# this is what gets called when an image is received
def image_callback(self, ros_image):
cv2_image = LaneDetection.ros_to_cv2_image(self, ros_image)
# this filter needs a mono image, no colors
roi = LaneDetection.convert_to_mono(self, cv2_image)
roi = LaneDetection.get_roi(self, roi)
# skeletonize image
size = np.size(roi)
skel = np.zeros(roi.shape, np.uint8)
element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
# iteratively erode, dilate, subtract, then OR the image
# until it's 1 pixel thick
for i in range(self.max_erode_iterations):
eroded = cv2.erode(roi, element)
temp = cv2.dilate(eroded, element)
temp = cv2.subtract(roi, temp)
skel = cv2.bitwise_or(skel, temp)
roi = eroded.copy()
zeros = size - cv2.countNonZero(roi)
if zeros == size:
break
final_image = skel
final_image_message = LaneDetection.cv2_to_ros_message(
self, final_image
)
# publishes final image message in ROS format
self.line_image_pub.publish(final_image_message)
# end image_callback()
def main(args):
node_name = "skeletonize"
namespace = rospy.get_namespace()
# create a Skeletonize object
s = Skeletonize(namespace, node_name)
# start the line_detector node and start listening
rospy.init_node("skeletonize", anonymous=True)
# starts dynamic_reconfigure server
srv = Server(LineDetectionConfig, s.reconfigure_callback)
rospy.spin()
if __name__ == '__main__':
main(sys.argv)