-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathimage_proc
More file actions
67 lines (53 loc) · 1.98 KB
/
image_proc
File metadata and controls
67 lines (53 loc) · 1.98 KB
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
#!/usr/bin/env python
import rospy, sys, getopt, cv2 as cv, numpy as np
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
help = """Usage: python image_proc --in=/image_raw/ --out=/camera/image_raw/
-i, --in: topic to extract the video stream. Default: /image_raw/
-o, --out: topic to publish the video stream. Default: /camera/image_raw/
-b, --brightness: brightness increase. Default: 30
-c, --contrast: contrast coefficient. Default: 1
"""
inTopic = '/image_raw'
outTopic = '/camera/image_raw'
brightness = 10
contrast = 1
bridge = CvBridge()
rospy.init_node('image_proc', anonymous=True)
pub = rospy.Publisher(outTopic, Image, queue_size=10) # Offboard control
try:
opts, _ = getopt.getopt(sys.argv[1:],'hi:o:b:c:',['help', 'in=', 'out=', 'brightness=', 'contrast='])
except getopt.GetoptError:
print(help)
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
print(help)
sys.exit()
elif opt in ('-i', '--in'):
inTopic = arg
elif opt in ('-o', '--out'):
outTopic = arg
elif opt in ('-b', '--brightness'):
brightness = int(arg)
elif opt in ('-c', '--contrast'):
contrast = float(arg)
def image_callback(img_msg):
try:
cv_image = bridge.imgmsg_to_cv2(img_msg, "bgr8")
except CvBridgeError as e:
rospy.logerr("CvBridge Error: {}".format(e))
cv_image = cv.convertScaleAbs(cv_image, alpha=contrast, beta=brightness)
# Histogram egalization
# cv_image = cv.cvtColor(cv_image, cv.COLOR_BGR2GRAY);
# cv_image = cv.equalizeHist(cv_image)
# cv_image = cv.cvtColor(cv_image, cv.COLOR_GRAY2RGB)
try:
image_message = bridge.cv2_to_imgmsg(cv_image, encoding="bgr8")
pub.publish(image_message)
except CvBridgeError as e:
rospy.logerr("CvBridge Error: {}".format(e))
sub_image = rospy.Subscriber(inTopic, Image, image_callback)
rospy.loginfo('Image processing stream started...')
while not rospy.is_shutdown():
rospy.spin()