CONTENTS LIST
Save Images with cv2
Saving an image is quite easy as well in cv2
. Let’s add a Gaussian noise to the image used in the previous post and save the image as a new jpg image:
img = cv2.imread('./tmp_image.png') # (648, 600, 3) numpy array
noise = np.random.randn(648, 600, 3) * 60
noise = np.abs(noise)
img_new = img + noise
cv2.imwrite('new_image.jpg', img_new) # save the image as jpg
I used cv2.imwirte()
function to save the new image.
Resize images
An image is easily resized with cv2.resize()
function:
img = cv2.imread('./tmp_image.png')
img_small = cv2.resize(img, (300, 300))
cv2.imwrite('small_image.jpg', img_small)
Draw Rectangles
In object detection, we usually show the detected objects with rectangles drawn around it like the image we have used so far. These rectangle are called ‘bounding boxes’ in object detection.
Suppose we have a picture of a terrorist, and an object detection predicted the bounding box as:
- top left corner coordinate = (377, 245)
- bottom right corner coordinate = (512, 565)
Then we draw a rectangle with this information with cv2.rectangle()
function:
img = cv2.imread('./terrorist.jpg')
top_left = (377, 245) # (x, y)
bottom_right = (512, 565) # (x, y)
color = (0, 255, 0) # (blue, green, red)
thickness = 2
img_new = cv2.rectangle(img, top_left, bottom_right, color, thickness)
cv2.imwrite('terrorist_with_rectangle.jpg', img_new)
Add Text
Along with the rectangle, we have to specify what class the detected object belongs to. This can be done with the cv2.putText()
function.
img = cv2.imread('./terrorist.jpg')
top_left = (377, 245) # (x, y)
bottom_right = (512, 565) # (x, y)
color = (0, 255, 0) # (blue, green, red)
thickness = 2
img_new = cv2.rectangle(img, top_left, bottom_right, color, thickness)
img_new = cv2.putText(img_new, 'human', top_left, cv2.FONT_HERSHEY_COMPLEX, 0.7, color, 2)
cv2.imwrite('terrorist_with_rectangle_and_text.jpg', img_new)
Conclusion
Resizing images is used to fix the input size of neural networks.
In the next post, I’m going to show you how to capture the screen in real-time using Python-mss module. I used this screen capture to create a cheat AI that can be used on an online game. Please see my post about that if you have time!
NEXT: Python Computer Vision Tutorials — cv2 and mss basics / part 3