Open CV with ROS
Introduction
In the robotic world, the sight of the robot is also possible with the help of the OpenCV. This package of OpenCV integrating with the ROS to provide the vision to the robots in the ROS simulation.
To install the OpenCV on to your system run the following command
--> sudo apt-get install ros-kinetic-opencv
To use the OpenCV you can even install the requirements by running the following commands in your command prompt.
--> sudo apt-get install ros-kinetic-usb-cam
--> sudo apt-get install ros-kinetic-image-view
Some of the basic codes to be run in the opencv to be run in the python
- img = cv.2 imread(“image.jpg”)
- type(img)
- img.size
- image.shape[0] (length)
- img.shape[1] (width)
- image.shape[2] (channels)
- len(img)
- img.dtype
Opencv is a powerful tool to work on the images and store the images in the large format of the arrays by using the library of the NumPy.
Image Encoding
Using OpenCV an image can be encoded into
Grayscale
Red, Green, Blue
Hue, Saturation, Value(HSV)
Where hue indicates the type of color, saturation indicates the amount of gray in the colour and value indicate the brightness and intensity of the colour.
The various colour angle in the wheel of 360-degree range as follows
Now, using the OpenCV we can perform the image encoding and analyze the single image in different encodings.
import numpy as np
import cv2
image_name = "tree"
print 'read an image from file'
color_image = cv2.imread("images/tree.jpg",cv2.IMREAD_COLOR)
print 'display image in native color'
cv2.imshow("Original Image",color_image)
cv2.moveWindow("Original Image",0,0)
print(color_image.shape)
height,width,channels = color_image.shape
print 'slipt the image into three channels.'
blue,green,red = cv2.split(color_image)
cv2.imshow("Blue Channel",blue)
cv2.moveWindow("Blue Channel",0,height)
cv2.imshow("Red Channel",red)
cv2.moveWindow("Red Channel",0,height)
cv2.imshow("Greeen Channel",green)
cv2.moveWindow("Green Channel",0,height)
print '---- slipt the image into Hue, Saturation, Value channels.----- '
hsv = cv2.cvtColor(color_image, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)
hsv_image = np.concatenate((h,s,v),axis=1)
cv2.imshow("Hue, Saturation, Value Image",hsv_image)
cv2.imshow("HSV Image",hsv)
print '------ converts an image to a grayscale ------'
gray_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Gray Image ",gray_image)
print gray_image
cv2.waitKey(0)
cv2.destroyAllWindows()
Now, this code execution will show the set of images in all the different formats of encodings.
Video Streaming Input
Using OpenCV one can read the video stream or the video file and can use it for processing.
import cv2
#video_capture = cv2.VideoCapture(0)
video_capture = cv2.VideoCapture('video/ros.mp4')
while(True):
ret, frame = video_capture.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = cv2.resize(frame, (0,0), fx=0.5,fy=0.5)
#cv2.line(frame,(0,0),(511,511),(255,0,0),5)
cv2.imshow("Frame",frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
Executing the following program one can read the video and can adjust the rate of reading the frames based on the value of the cv2.waitKey function.
Drawing the shapes on the Images
OpenCV can be used to draw the various shapes on the images. Generally, different shapes are drawn as the boundaries for the detection of the various objects. For example, we can use the rectangle to draw the boundary to the face detection in the image frame.
import numpy as np
import cv2
image = np.zeros((512,512,3), np.uint8)
#cv2.circle(image, (image.shape[0]/2, image.shape[1]/2),63, (255,255,255), 5)
cv2.line(image,(0,0),(511,511),(255,255,255),5)
cv2.rectangle(image,(384,0),(510,128),(0,255,0),3)
cv2.ellipse(image,(256,256),(100,50),0,0,180,255,-1)
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(image,[pts],True,(0,255,255))
font = cv2.FONT_HERSHEY_SIMPLEX
#cv2.putText(image,'ROS, OpenCV',(10,500), font, 2,(255,255,255),2,cv2.LINE_AA)
cv2.putText(image,'OpenCV',(10,500), font, 4,(255,255,255),2)
cv2.imshow("Image Panel",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
After the execution of this code, it will be showing the different images on the single frame as following
This completes the basic concepts of OpenCV with ROS and for any sort of doubts or questions, you can reach out through the comment box.