diff --git a/src/image_processes/CMakeLists.txt b/src/image_processes/CMakeLists.txt new file mode 100644 index 0000000..cb5f7f3 --- /dev/null +++ b/src/image_processes/CMakeLists.txt @@ -0,0 +1,206 @@ +cmake_minimum_required(VERSION 3.0.2) +project(image_processes) + +## Compile as C++11, supported in ROS Kinetic and newer +# add_compile_options(-std=c++11) + +## Find catkin macros and libraries +## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz) +## is used, also find other catkin packages +find_package(catkin REQUIRED COMPONENTS + roscpp + rospy + std_msgs +) + +## System dependencies are found with CMake's conventions +# find_package(Boost REQUIRED COMPONENTS system) + + +## Uncomment this if the package has a setup.py. This macro ensures +## modules and global scripts declared therein get installed +## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html +# catkin_python_setup() + +################################################ +## Declare ROS messages, services and actions ## +################################################ + +## To declare and build messages, services or actions from within this +## package, follow these steps: +## * Let MSG_DEP_SET be the set of packages whose message types you use in +## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...). +## * In the file package.xml: +## * add a build_depend tag for "message_generation" +## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET +## * If MSG_DEP_SET isn't empty the following dependency has been pulled in +## but can be declared for certainty nonetheless: +## * add a exec_depend tag for "message_runtime" +## * In this file (CMakeLists.txt): +## * add "message_generation" and every package in MSG_DEP_SET to +## find_package(catkin REQUIRED COMPONENTS ...) +## * add "message_runtime" and every package in MSG_DEP_SET to +## catkin_package(CATKIN_DEPENDS ...) +## * uncomment the add_*_files sections below as needed +## and list every .msg/.srv/.action file to be processed +## * uncomment the generate_messages entry below +## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...) + +## Generate messages in the 'msg' folder +# add_message_files( +# FILES +# Message1.msg +# Message2.msg +# ) + +## Generate services in the 'srv' folder +# add_service_files( +# FILES +# Service1.srv +# Service2.srv +# ) + +## Generate actions in the 'action' folder +# add_action_files( +# FILES +# Action1.action +# Action2.action +# ) + +## Generate added messages and services with any dependencies listed here +# generate_messages( +# DEPENDENCIES +# std_msgs # Or other packages containing msgs +# ) + +################################################ +## Declare ROS dynamic reconfigure parameters ## +################################################ + +## To declare and build dynamic reconfigure parameters within this +## package, follow these steps: +## * In the file package.xml: +## * add a build_depend and a exec_depend tag for "dynamic_reconfigure" +## * In this file (CMakeLists.txt): +## * add "dynamic_reconfigure" to +## find_package(catkin REQUIRED COMPONENTS ...) +## * uncomment the "generate_dynamic_reconfigure_options" section below +## and list every .cfg file to be processed + +## Generate dynamic reconfigure parameters in the 'cfg' folder +# generate_dynamic_reconfigure_options( +# cfg/DynReconf1.cfg +# cfg/DynReconf2.cfg +# ) + +################################### +## catkin specific configuration ## +################################### +## The catkin_package macro generates cmake config files for your package +## Declare things to be passed to dependent projects +## INCLUDE_DIRS: uncomment this if your package contains header files +## LIBRARIES: libraries you create in this project that dependent projects also need +## CATKIN_DEPENDS: catkin_packages dependent projects also need +## DEPENDS: system dependencies of this project that dependent projects also need +catkin_package( +# INCLUDE_DIRS include +# LIBRARIES image_processes +# CATKIN_DEPENDS roscpp rospy stdmsg +# DEPENDS system_lib +) + +########### +## Build ## +########### + +## Specify additional locations of header files +## Your package locations should be listed before other locations +include_directories( +# include + ${catkin_INCLUDE_DIRS} +) + +## Declare a C++ library +# add_library(${PROJECT_NAME} +# src/${PROJECT_NAME}/image_processes.cpp +# ) + +## Add cmake target dependencies of the library +## as an example, code may need to be generated before libraries +## either from message generation or dynamic reconfigure +# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) + +## Declare a C++ executable +## With catkin_make all packages are built within a single CMake context +## The recommended prefix ensures that target names across packages don't collide +# add_executable(${PROJECT_NAME}_node src/image_processes_node.cpp) + +## Rename C++ executable without prefix +## The above recommended prefix causes long target names, the following renames the +## target back to the shorter version for ease of user use +## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node" +# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "") + +## Add cmake target dependencies of the executable +## same as for the library above +# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) + +## Specify libraries to link a library or executable target against +# target_link_libraries(${PROJECT_NAME}_node +# ${catkin_LIBRARIES} +# ) + +############# +## Install ## +############# + +# all install targets should use catkin DESTINATION variables +# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html + +## Mark executable scripts (Python etc.) for installation +## in contrast to setup.py, you can choose the destination +# catkin_install_python(PROGRAMS +# scripts/my_python_script +# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} +# ) + +## Mark executables for installation +## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html +# install(TARGETS ${PROJECT_NAME}_node +# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} +# ) + +## Mark libraries for installation +## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html +# install(TARGETS ${PROJECT_NAME} +# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} +# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} +# RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION} +# ) + +## Mark cpp header files for installation +# install(DIRECTORY include/${PROJECT_NAME}/ +# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION} +# FILES_MATCHING PATTERN "*.h" +# PATTERN ".svn" EXCLUDE +# ) + +## Mark other files for installation (e.g. launch and bag files, etc.) +# install(FILES +# # myfile1 +# # myfile2 +# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} +# ) + +############# +## Testing ## +############# + +## Add gtest based cpp test target and link libraries +# catkin_add_gtest(${PROJECT_NAME}-test test/test_image_processes.cpp) +# if(TARGET ${PROJECT_NAME}-test) +# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME}) +# endif() + +## Add folders to be run by python nosetests +# catkin_add_nosetests(test) diff --git a/src/image_processes/launch/image_cropping.launch b/src/image_processes/launch/image_cropping.launch new file mode 100644 index 0000000..6a23743 --- /dev/null +++ b/src/image_processes/launch/image_cropping.launch @@ -0,0 +1,22 @@ + + + + + + + \ No newline at end of file diff --git a/src/image_processes/package.xml b/src/image_processes/package.xml new file mode 100644 index 0000000..057b7b4 --- /dev/null +++ b/src/image_processes/package.xml @@ -0,0 +1,71 @@ + + + image_processes + 0.0.0 + The image_processes package + + + + + vasundhara + + + + + + TODO + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + catkin + roscpp + rospy + std_msgs + cv_bridge + roscpp + rospy + std_msgs + cv_bridge + roscpp + rospy + std_msgss + cv_bridge + + + + + + + + diff --git a/src/image_processes/scripts/node1.py b/src/image_processes/scripts/node1.py new file mode 100644 index 0000000..0a495cd --- /dev/null +++ b/src/image_processes/scripts/node1.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 + +import rospy +from sensor_msgs.msg import Image +from cv_bridge import CvBridge +import cv2 + +''' +def webcam_img_publisher(): + rospy.init_node('webcam_img_publisher', anonymous=True) + img_pub = rospy.Publisher('Webcam_img', Image, queue_size=10) + rate = rospy.Rate(10) # 10Hz + + cap = cv2.VideoCapture(0) + bridge = CvBridge() + + while not rospy.is_shutdown(): + ret, frame = cap.read() + if ret: + img_msg = bridge.cv2_to_imgmsg(frame, encoding="bgr8") + img_pub.publish(img_msg) + rospy.loginfo('publishing video frame') + rate.sleep() + + cap.release() + +if __name__ == '__main__': + try: + webcam_img_publisher() + except rospy.ROSInterruptException: + pass + +''' +def publish_message(): + + # Node is publishing to the video_frames topic using + # the message type Image + pub = rospy.Publisher('Webcam_img', Image, queue_size=10) + + # Tells rospy the name of the node. + # Anonymous = True makes sure the node has a unique name. Random + # numbers are added to the end of the name. + rospy.init_node('image_capture', anonymous=True) + + # Go through the loop 10 times per second + rate = rospy.Rate(10) # 10hz + + # Create a VideoCapture object + # The argument '0' gets the default webcam. + cap = cv2.VideoCapture(0) + + # Used to convert between ROS and OpenCV images + br = CvBridge() + + # While ROS is still running. + while not rospy.is_shutdown(): + + # Capture frame-by-frame + # This method returns True/False as well + # as the video frame. + ret, frame = cap.read() + + if ret == True: + # Print debugging information to the terminal + rospy.loginfo('publishing video frame') + # cv2.imshow("Frame", frame) + # cv2.waitKey(1) + # Publish the image. + # The 'cv2_to_imgmsg' method converts an OpenCV + # image to a ROS image message + pub.publish(br.cv2_to_imgmsg(frame)) + print(type(frame)) + + # Sleep just enough to maintain the desired rate + rate.sleep() + +if __name__ == '__main__': + try: + publish_message() + except rospy.ROSInterruptException: + pass diff --git a/src/image_processes/scripts/node2.py b/src/image_processes/scripts/node2.py new file mode 100644 index 0000000..2221259 --- /dev/null +++ b/src/image_processes/scripts/node2.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +# Import the necessary libraries +import rospy # Python library for ROS +from sensor_msgs.msg import Image # Image is the message type +from cv_bridge import CvBridge # Package to convert between ROS and OpenCV Images +import cv2 # OpenCV library +import numpy as np +import sys + +def crop_image(frame, percentage): + height, width = frame.shape[:2] + crop_height = int(height * percentage) + crop_width = int(width * percentage) + cropped_frame = frame[crop_height:, crop_width:] + return cropped_frame + + + +def imgmsg_to_cv2(img_msg): + dtype = np.dtype("uint8") # Hardcode to 8 bits... + dtype = dtype.newbyteorder('>' if img_msg.is_bigendian else '<') + image_opencv = np.ndarray(shape=(img_msg.height, img_msg.width, 3), # and three channels of data. Since OpenCV works with bgr natively, we don't need to reorder the channels. + dtype=dtype, buffer=img_msg.data) + # If the byt order is different between the message and the system. + if img_msg.is_bigendian == (sys.byteorder == 'little'): + image_opencv = image_opencv.byteswap().newbyteorder() + return image_opencv + +def img_callback(img_msg): + bridge = CvBridge() + frame = imgmsg_to_cv2(img_msg) + cropped_frame = crop_image(frame, 0.3) # Crop by 30% in pixels + cropped_img_msg = bridge.cv2_to_imgmsg(cropped_frame) + pub = rospy.Publisher('Webcam_cropped', Image, queue_size=10) + pub.publish(cropped_img_msg) + +def webcam_cropped_node(): + rospy.init_node('webcam_cropped_node', anonymous=True) + rospy.Subscriber('Webcam_img', Image, img_callback) + + rate = rospy.Rate(10) # 10Hz + + rospy.spin() + + cv2.destroyAllWindows() + + +if __name__ == '__main__': + try: + webcam_cropped_node() + except rospy.ROSInterruptException: + pass \ No newline at end of file diff --git a/src/image_processes/scripts/node3.py b/src/image_processes/scripts/node3.py new file mode 100644 index 0000000..2cb34aa --- /dev/null +++ b/src/image_processes/scripts/node3.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +import rospy +from sensor_msgs.msg import Image +from cv_bridge import CvBridge +import cv2 +import numpy as np +import sys + +def imgmsg_to_cv2(img_msg): + dtype = np.dtype("uint8") # Hardcode to 8 bits... + dtype = dtype.newbyteorder('>' if img_msg.is_bigendian else '<') + image_opencv = np.ndarray(shape=(img_msg.height, img_msg.width, 3), # and three channels of data. Since OpenCV works with bgr natively, we don't need to reorder the channels. + dtype=dtype, buffer=img_msg.data) + # If the byt order is different between the message and the system. + if img_msg.is_bigendian == (sys.byteorder == 'little'): + image_opencv = image_opencv.byteswap().newbyteorder() + return image_opencv + +def img_callback(img_msg): + frame = imgmsg_to_cv2(img_msg) + cv2.imshow('Webcam Cropped', frame) + cv2.waitKey(1) + +def webcam_show_node(): + rospy.init_node('webcam_show_node', anonymous=True) + img_sub = rospy.Subscriber('Webcam_cropped', Image, img_callback) + rospy.spin() + +if __name__ == '__main__': + try: + webcam_show_node() + except rospy.ROSInterruptException: + pass \ No newline at end of file