-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimg_proc.py
More file actions
83 lines (67 loc) · 3.18 KB
/
img_proc.py
File metadata and controls
83 lines (67 loc) · 3.18 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
# Copyright (c) 2014, Rethink Robotics
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the Rethink Robotics nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import cv
import cv_bridge
import PIL
'''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# PIL/cv interaction
# The functions in this secion handle interactions between
# PIL and cvmat formats.
# **gen_msg() converts an RGB PIL image to a BGR cvmat image
# **rgb_to_bgr() converts an RGB PIL image to a BGR PIL image
# **PIL_to_cv() converts a BGR PIL image to a cvmat image
# **cv_to_msg() converts a cvmat image to a rosmsg format
# **msg_to_cv() converts a rosmsg image to a cvmat image
# **overlay() takes an original image, the size of that image,
# a rectangle defined in that original image, and a new
# new image and returns the original image with the new
# image overlayed in the defined rectangle.
# For the moment, this is mainly used for the Camera
# Display demo, overlaying the selected camera's image
# onto the UI display
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'''
def gen_cv(img):
return PIL_to_cv(rgb_to_bgr(img))
def rgb_to_bgr(img):
r, g, b = img.split()
return PIL.Image.merge('RGB', (b, g, r))
def PIL_to_cv(img):
ci = cv.CreateImage((1024, 600), cv.IPL_DEPTH_8U, 3)
cv.SetData(ci, img.tostring(), 3072)
return ci
def cv_to_msg(img):
return cv_bridge.CvBridge().cv_to_imgmsg(img, encoding='bgr8')
def msg_to_cv(img):
return cv_bridge.CvBridge().imgmsg_to_cv(img, desired_encoding='bgr8')
def overlay(old_img, new_img, original_size, new_rect):
tmp = cv.CreateImage(original_size, cv.IPL_DEPTH_8U, 3)
cv.Copy(old_img, tmp)
sub = cv.GetSubRect(tmp, new_rect)
cv_img = msg_to_cv(new_img)
cv.Copy(cv_img, sub)
return cv_to_msg(tmp)