#OpenCV – Convert camera feed 🎦 to Grayscale and resalt Blue πŸ”΅ and Red β€οΈ

Hi !

A new twist on these days demos, this time:

  • Open a camera feed
  • Convert each frame to grayscale
  • Use a hue mask to resalt colors, Blue and Red

Here is a live sample from my office.

Convert image to GraysScale and resalt red and blue

Note: I created a mask that identifies the Captain America shield, SpyderGwen poster and Ahsoka frame, but it’s missing the Canada flag 🍁 ! Still, my guitar and working desk are converted to grayscale.

Another funny scenario !

And the sample code:

# Copyright (c) 2022
# Author : Bruno Capuano
# Create Time : 2022 June
# Change Log :
# – Open the camera feed
# – Convert the camera feed to Grayscale
# – Resalt a specific colors >> RED and BLUE
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while True:
# read image
ret, img = cap.read()
img = cv2.resize(img, (320, 240))
#convert the BGR image to HSV colour space
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#obtain the grayscale image of the original image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# set the bounds for the red and blue hue
lower_red = np.array([25,37,40])
upper_red = np.array ([255, 255, 255])
# create a mask using the bounds set
mask = cv2.inRange(hsv, lower_red, upper_red)
# create an inverse of the mask
mask_inv = cv2.bitwise_not(mask)
# Filter only the red colour from the original image using the mask(foreground)
res = cv2.bitwise_and(img, img, mask=mask)
# Filter the regions containing colours other than red from the grayscale image(background)
background = cv2.bitwise_and(gray, gray, mask = mask_inv)
# convert the one channelled grayscale background to a three channelled image
background = np.stack((background,)*3, axis=1)
# add the foreground and the background
img_ca = cv2.add(res, background)
cv2.imshow('@ElBruno – Office', img)
cv2.imshow('@ElBruno – Captain America', img_ca)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# close camera
cap.release()
cv2.destroyAllWindows()

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


Advertisement

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: