Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Assignment 1/README.md
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions Assignment 1/launch/image_cropping.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<launch>
<!-- node tag used to launch a node. here it will launch node1 which is webcam_publisher.-->
<!-- pkg stores package name. type stores name of node file -->
<node name="webcam_publisher" pkg="image_processes" type="node1.py">

<param name="image_topic" value="webcam_image"/>
</node>

<!-- here it will launch the node2 which is image_cropper -->
<node name="image_cropper" pkg="image_processes" type="node2.py">

<param name="input_image_topic" value="webcam_image"/>
<param name="output_image_topic" value="cropped_image"/>
</node>

<!-- now final node3 will launch which is image_viewer node -->
<node name="image_viewer" pkg="image_processes" type="node3.py">

<param name="image_topic" value="cropped_image"/>
</node>
</launch>
43 changes: 43 additions & 0 deletions Assignment 1/src/node1.py
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions Assignment 1/src/node2.py
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions Assignment 1/src/node3.py
Original file line number Diff line number Diff line change
@@ -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