Skip to content

Commit f145411

Browse files
committed
2 parents b3eff37 + f37da6f commit f145411

File tree

5 files changed

+411
-0
lines changed

5 files changed

+411
-0
lines changed

color_object_detection/CMakeLists.txt

+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
cmake_minimum_required(VERSION 2.8.3)
2+
project(color_object_detection)
3+
4+
## Compile as C++11, supported in ROS Kinetic and newer
5+
# add_compile_options(-std=c++11)
6+
7+
## Find catkin macros and libraries
8+
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
9+
## is used, also find other catkin packages
10+
find_package(catkin REQUIRED COMPONENTS
11+
cv_bridge
12+
rospy
13+
sensor_msgs
14+
std_msgs
15+
geometry_msgs
16+
message_generation
17+
)
18+
19+
## System dependencies are found with CMake's conventions
20+
# find_package(Boost REQUIRED COMPONENTS system)
21+
22+
23+
## Uncomment this if the package has a setup.py. This macro ensures
24+
## modules and global scripts declared therein get installed
25+
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
26+
# catkin_python_setup()
27+
28+
################################################
29+
## Declare ROS messages, services and actions ##
30+
################################################
31+
32+
## To declare and build messages, services or actions from within this
33+
## package, follow these steps:
34+
## * Let MSG_DEP_SET be the set of packages whose message types you use in
35+
## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
36+
## * In the file package.xml:
37+
## * add a build_depend tag for "message_generation"
38+
## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
39+
## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
40+
## but can be declared for certainty nonetheless:
41+
## * add a exec_depend tag for "message_runtime"
42+
## * In this file (CMakeLists.txt):
43+
## * add "message_generation" and every package in MSG_DEP_SET to
44+
## find_package(catkin REQUIRED COMPONENTS ...)
45+
## * add "message_runtime" and every package in MSG_DEP_SET to
46+
## catkin_package(CATKIN_DEPENDS ...)
47+
## * uncomment the add_*_files sections below as needed
48+
## and list every .msg/.srv/.action file to be processed
49+
## * uncomment the generate_messages entry below
50+
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)
51+
52+
## Generate messages in the 'msg' folder
53+
add_message_files(
54+
FILES
55+
Rectangle.msg
56+
# Message1.msg
57+
# Message2.msg
58+
)
59+
60+
## Generate services in the 'srv' folder
61+
# add_service_files(
62+
# FILES
63+
# Service1.srv
64+
# Service2.srv
65+
# )
66+
67+
## Generate actions in the 'action' folder
68+
# add_action_files(
69+
# FILES
70+
# Action1.action
71+
# Action2.action
72+
# )
73+
74+
## Generate added messages and services with any dependencies listed here
75+
generate_messages(
76+
DEPENDENCIES
77+
sensor_msgs
78+
std_msgs
79+
)
80+
81+
################################################
82+
## Declare ROS dynamic reconfigure parameters ##
83+
################################################
84+
85+
## To declare and build dynamic reconfigure parameters within this
86+
## package, follow these steps:
87+
## * In the file package.xml:
88+
## * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
89+
## * In this file (CMakeLists.txt):
90+
## * add "dynamic_reconfigure" to
91+
## find_package(catkin REQUIRED COMPONENTS ...)
92+
## * uncomment the "generate_dynamic_reconfigure_options" section below
93+
## and list every .cfg file to be processed
94+
95+
## Generate dynamic reconfigure parameters in the 'cfg' folder
96+
# generate_dynamic_reconfigure_options(
97+
# cfg/DynReconf1.cfg
98+
# cfg/DynReconf2.cfg
99+
# )
100+
101+
###################################
102+
## catkin specific configuration ##
103+
###################################
104+
## The catkin_package macro generates cmake config files for your package
105+
## Declare things to be passed to dependent projects
106+
## INCLUDE_DIRS: uncomment this if your package contains header files
107+
## LIBRARIES: libraries you create in this project that dependent projects also need
108+
## CATKIN_DEPENDS: catkin_packages dependent projects also need
109+
## DEPENDS: system dependencies of this project that dependent projects also need
110+
catkin_package(
111+
# INCLUDE_DIRS include
112+
# LIBRARIES color_object_detection
113+
CATKIN_DEPENDS cv_bridge rospy sensor_msgs std_msgs geometry_msgs message_generation
114+
# DEPENDS system_lib
115+
)
116+
117+
###########
118+
## Build ##
119+
###########
120+
121+
## Specify additional locations of header files
122+
## Your package locations should be listed before other locations
123+
include_directories(
124+
# include
125+
${catkin_INCLUDE_DIRS}
126+
)
127+
128+
## Declare a C++ library
129+
# add_library(${PROJECT_NAME}
130+
# src/${PROJECT_NAME}/color_object_detection.cpp
131+
# )
132+
133+
## Add cmake target dependencies of the library
134+
## as an example, code may need to be generated before libraries
135+
## either from message generation or dynamic reconfigure
136+
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
137+
138+
## Declare a C++ executable
139+
## With catkin_make all packages are built within a single CMake context
140+
## The recommended prefix ensures that target names across packages don't collide
141+
# add_executable(${PROJECT_NAME}_node src/color_object_detection_node.cpp)
142+
143+
## Rename C++ executable without prefix
144+
## The above recommended prefix causes long target names, the following renames the
145+
## target back to the shorter version for ease of user use
146+
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
147+
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")
148+
149+
## Add cmake target dependencies of the executable
150+
## same as for the library above
151+
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
152+
153+
## Specify libraries to link a library or executable target against
154+
# target_link_libraries(${PROJECT_NAME}_node
155+
# ${catkin_LIBRARIES}
156+
# )
157+
158+
#############
159+
## Install ##
160+
#############
161+
162+
# all install targets should use catkin DESTINATION variables
163+
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html
164+
165+
## Mark executable scripts (Python etc.) for installation
166+
## in contrast to setup.py, you can choose the destination
167+
# install(PROGRAMS
168+
# scripts/my_python_script
169+
# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
170+
# )
171+
172+
## Mark executables and/or libraries for installation
173+
# install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_node
174+
# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
175+
# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
176+
# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
177+
# )
178+
179+
## Mark cpp header files for installation
180+
# install(DIRECTORY include/${PROJECT_NAME}/
181+
# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
182+
# FILES_MATCHING PATTERN "*.h"
183+
# PATTERN ".svn" EXCLUDE
184+
# )
185+
186+
## Mark other files for installation (e.g. launch and bag files, etc.)
187+
# install(FILES
188+
# # myfile1
189+
# # myfile2
190+
# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
191+
# )
192+
193+
#############
194+
## Testing ##
195+
#############
196+
197+
## Add gtest based cpp test target and link libraries
198+
# catkin_add_gtest(${PROJECT_NAME}-test test/test_color_object_detection.cpp)
199+
# if(TARGET ${PROJECT_NAME}-test)
200+
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
201+
# endif()
202+
203+
## Add folders to be run by python nosetests
204+
# catkin_add_nosetests(test)
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int64 x1
2+
int64 y1
3+
int64 x2
4+
int64 y2

color_object_detection/package.xml

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?xml version="1.0"?>
2+
<package format="2">
3+
<name>color_object_detection</name>
4+
<version>0.0.0</version>
5+
<description>The color_object_detection package</description>
6+
7+
<!-- One maintainer tag required, multiple allowed, one person per tag -->
8+
<!-- Example: -->
9+
<!-- <maintainer email="[email protected]">Jane Doe</maintainer> -->
10+
<maintainer email="[email protected]">soil</maintainer>
11+
12+
13+
<!-- One license tag required, multiple allowed, one license per tag -->
14+
<!-- Commonly used license strings: -->
15+
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
16+
<license>TODO</license>
17+
18+
19+
<!-- Url tags are optional, but multiple are allowed, one per tag -->
20+
<!-- Optional attribute type can be: website, bugtracker, or repository -->
21+
<!-- Example: -->
22+
<!-- <url type="website">http://wiki.ros.org/color_object_detection</url> -->
23+
24+
25+
<!-- Author tags are optional, multiple are allowed, one per tag -->
26+
<!-- Authors do not have to be maintainers, but could be -->
27+
<!-- Example: -->
28+
<!-- <author email="[email protected]">Jane Doe</author> -->
29+
30+
31+
<!-- The *depend tags are used to specify dependencies -->
32+
<!-- Dependencies can be catkin packages or system dependencies -->
33+
<!-- Examples: -->
34+
<!-- Use depend as a shortcut for packages that are both build and exec dependencies -->
35+
<!-- <depend>roscpp</depend> -->
36+
<!-- Note that this is equivalent to the following: -->
37+
<!-- <build_depend>roscpp</build_depend> -->
38+
<!-- <exec_depend>roscpp</exec_depend> -->
39+
<!-- Use build_depend for packages you need at compile time: -->
40+
<!-- <build_depend>message_generation</build_depend> -->
41+
<!-- Use build_export_depend for packages you need in order to build against this package: -->
42+
<!-- <build_export_depend>message_generation</build_export_depend> -->
43+
<!-- Use buildtool_depend for build tool packages: -->
44+
<!-- <buildtool_depend>catkin</buildtool_depend> -->
45+
<!-- Use exec_depend for packages you need at runtime: -->
46+
<!-- <exec_depend>message_runtime</exec_depend> -->
47+
<!-- Use test_depend for packages you need only for testing: -->
48+
<!-- <test_depend>gtest</test_depend> -->
49+
<!-- Use doc_depend for packages you need only for building documentation: -->
50+
<!-- <doc_depend>doxygen</doc_depend> -->
51+
<buildtool_depend>catkin</buildtool_depend>
52+
<build_depend>cv_bridge</build_depend>
53+
<build_depend>rospy</build_depend>
54+
<build_depend>sensor_msgs</build_depend>
55+
<build_depend>std_msgs</build_depend>
56+
<build_depend>geometry_msgs</build_depend>
57+
<build_depend>vision_msgs</build_depend>
58+
<build_depend>message_generation</build_depend>
59+
<build_export_depend>cv_bridge</build_export_depend>
60+
<build_export_depend>rospy</build_export_depend>
61+
<build_export_depend>sensor_msgs</build_export_depend>
62+
<build_export_depend>std_msgs</build_export_depend>
63+
<build_export_depend>geometry_msgs</build_export_depend>
64+
<build_export_depend>vision_msgs</build_export_depend>
65+
<build_export_depend>message_generation</build_export_depend>
66+
<exec_depend>cv_bridge</exec_depend>
67+
<exec_depend>rospy</exec_depend>
68+
<exec_depend>sensor_msgs</exec_depend>
69+
<exec_depend>std_msgs</exec_depend>
70+
<exec_depend>geometry_msgs</exec_depend>
71+
<exec_depend>vision_msgs</exec_depend>
72+
<exec_depend>message_runtime</exec_depend>
73+
<exec_depend>message_generation</exec_depend>
74+
75+
76+
<!-- The export tag contains other, unspecified, tags -->
77+
<export>
78+
<!-- Other tools can request additional information be placed here -->
79+
80+
</export>
81+
</package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python
2+
from __future__ import print_function
3+
4+
import roslib
5+
import sys
6+
import rospy
7+
import cv2
8+
from std_msgs.msg import String
9+
from sensor_msgs.msg import Image
10+
from cv_bridge import CvBridge, CvBridgeError
11+
import numpy as np
12+
13+
from color_object_detection.msg import Rectangle
14+
15+
class image_converter:
16+
17+
def __init__(self):
18+
self.image_pub = rospy.Publisher("/camera/color/image_raw/segmented",Image, queue_size=10)
19+
self.bbox_pub = rospy.Publisher("/bbox", Rectangle, queue_size=1000)
20+
21+
self.bridge = CvBridge()
22+
self.image_sub = rospy.Subscriber("/camera/color/image_raw",Image,self.callback)
23+
24+
def callback(self,data):
25+
try:
26+
cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
27+
except CvBridgeError as e:
28+
print(e)
29+
30+
(rows,cols,channels) = cv_image.shape
31+
hsv_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)
32+
33+
# define range of blue color in HSV
34+
lower_red1 = np.array([0,50,100])
35+
upper_red1 = np.array([10,255,255])
36+
37+
lower_red2 = np.array([175,50,100])
38+
upper_red2 = np.array([180,255,255])
39+
40+
# Threshold the HSV image to get only blue colors
41+
mask1 = cv2.inRange(hsv_image, lower_red1, upper_red1)
42+
mask2 = cv2.inRange(hsv_image, lower_red2, upper_red2)
43+
mask = cv2.bitwise_or(mask1,mask2)
44+
kernel = np.ones((5,5),np.uint8)
45+
46+
mask = cv2.erode(mask,kernel,iterations = 2)
47+
mask = cv2.dilate(mask,kernel,iterations = 2)
48+
49+
ret,thresh = cv2.threshold(mask,200,255,0)
50+
im, contours,hierarchy= cv2.findContours(thresh, 1, 2)
51+
cnt = contours[0]
52+
x,y,w,h = cv2.boundingRect(cnt)
53+
54+
# Bitwise-AND mask and original image
55+
# res1 = cv2.bitwise_and(cv_image,cv_image, mask= mask1)
56+
# res2 = cv2.bitwise_and(cv_image,cv_image, mask= mask2)
57+
# res = cv2.bitwise_or(res1, res2)
58+
59+
rect = Rectangle(x,y,x+w,y+h)
60+
self.bbox_pub.publish(rect)
61+
62+
res = cv2.bitwise_and(cv_image,cv_image, mask= mask)
63+
cv2.rectangle(res,(x,y),(x+w,y+h),(0,255,0),2)
64+
65+
# cv2.imshow("Image window", res)
66+
# cv2.waitKey(3)
67+
68+
try:
69+
self.image_pub.publish(self.bridge.cv2_to_imgmsg(res, "bgr8"))
70+
except CvBridgeError as e:
71+
print(e)
72+
73+
def main(args):
74+
rospy.init_node('image_converter', anonymous=True)
75+
ic = image_converter()
76+
77+
try:
78+
rospy.spin()
79+
except KeyboardInterrupt:
80+
print("Shutting down")
81+
cv2.destroyAllWindows()
82+
83+
if __name__ == '__main__':
84+
main(sys.argv)

0 commit comments

Comments
 (0)