diff --git a/Assignment 1/README.md b/Assignment 1/README.md new file mode 100644 index 0000000..545a79d --- /dev/null +++ b/Assignment 1/README.md @@ -0,0 +1,20 @@ +What I have done for this Assignment: + +#Learnt about openCv, classes, methods and about various dependencies to create/build a package. +#Create a package named image_processes in catkin_ws/src folder by running command line in terminal as catkin_create-pkg image_processes rospy roscpp cv_bridge stdg_msgs and more. +#Now run the catkin_make command in catkin_ws folder +#now created the node1.py node2.py node3.py files in catkin_ws/src/image_processes/src path +#now make the files executable by running command chmod +x node1.py and similarily for others two +#in node1.py imports the required libraries and then created a webcam-pub function which open the camera gather the image data and then publish it to the Webcam +-img topic +#in node2.py I subscribed to Webcam_img topic then publish a Webcam_cropped topic which has the image data 30% reduced by pixels. To crop, first we have to convert Ros image into cv image then crop it again convert into Ros image. +#in node3.py I subscribed the Webcam_cropped topic then show that cropped to image +#Finally created a launch file which launches all the three nodes at once. + + + +#resources:- +wiki.ros.org +ChatGPT +askubuntu.com +stackoverflow.com \ No newline at end of file diff --git a/Assignment 1/launch/image_cropping.launch b/Assignment 1/launch/image_cropping.launch new file mode 100644 index 0000000..3a6cabc --- /dev/null +++ b/Assignment 1/launch/image_cropping.launch @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/Assignment 1/src/node1.py b/Assignment 1/src/node1.py new file mode 100644 index 0000000..fc978b2 --- /dev/null +++ b/Assignment 1/src/node1.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +import rospy +from sensor_msgs.msg import Image +from cv_bridge import CvBridge +import cv2 + +def webcam_pub(): + #To initialize the node + rospy.init_node('webcam_publisher', anonymous=True) + #Creating a publisher for the Webcam_image topic + image_publisher = rospy.Publisher('Webcam_img', Image, queue_size=10) + #publishing rate in Hz + rate = rospy.Rate(10) + + #variable/objct to store CvBridge() class + bridge = CvBridge() + #it will open camera + camera = cv2.VideoCapture(0) + + while not rospy.is_shutdown(): + #it will capture frames + ret, frame = camera.read() + + if ret: + try: + #converts image to ros msg + image_msg = bridge.cv2_to_imgmsg(frame, "bgr8") + #publish the image message + image_publisher.publish(image_msg) + except Exception as e: + rospy.logerr(e) + + #It will maintain the publishing rate by sleeping + rate.sleep() + + camera.release() + +if __name__ == '__main__': + try: + webcam_pub() + except rospy.ROSInterruptException: + pass diff --git a/Assignment 1/src/node2.py b/Assignment 1/src/node2.py new file mode 100644 index 0000000..2cefc6e --- /dev/null +++ b/Assignment 1/src/node2.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 + +import rospy +from sensor_msgs.msg import Image +from cv_bridge import CvBridge +import cv2 + +image_publisher = None + +def img_callback(msg): + global image_publisher + + try: + #now convert the ros image into cv image + bridge = CvBridge() + cv_image = bridge.imgmsg_to_cv2(msg, "bgr8") + + #cropping by 30% pixels [y1:y2, x1:x2] where (x1,y1) is the coordinate of bottom left corner and (x2,y2) is of top right corner + cropped_image = cv_image[108:396, 192:1088] + #again converting cv image to ros image message + cropped_msg = bridge.cv2_to_imgmsg(cropped_image, "bgr8") + + #publishes the cropped image + image_publisher.publish(cropped_msg) + + except Exception as e: + rospy.logerr(e) + +def img_cropper(): + global image_publisher + + #init_node command to initialize the node + rospy.init_node('image_cropper', anonymous=True) + + #Creating a publisher for the cropped image topic + image_publisher = rospy.Publisher('Webcam_cropped', Image, queue_size=10) + + # Creating a subscriber for the image topic + rospy.Subscriber('Webcam_img', Image, img_callback) + + rospy.spin() + +if __name__ == '__main__': + try: + img_cropper() + except rospy.ROSInterruptException: + pass diff --git a/Assignment 1/src/node3.py b/Assignment 1/src/node3.py new file mode 100644 index 0000000..3909688 --- /dev/null +++ b/Assignment 1/src/node3.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +import rospy +from sensor_msgs.msg import Image +from cv_bridge import CvBridge +import cv2 + +def image_callback(msg): + try: + #converting the ros image message to opencv format + bridge = CvBridge() + cv_image = bridge.imgmsg_to_cv2(msg, "bgr8") + + #displays the cropped image + cv2.imshow("Webcam-Cropped-Image", cv_image) + cv2.waitKey(1) #Refresh display every 1 ms + + except Exception as e: + rospy.logerr(e) + +def image_viewer(): + #initializing the node + rospy.init_node('image_viewer', anonymous=True) + + #Creating a subscriber for the image topic + rospy.Subscriber('Webcam_cropped', Image, image_callback) + + rospy.spin() + + cv2.destroyAllWindows() + +if __name__ == '__main__': + try: + image_viewer() + except rospy.ROSInterruptException: + pass