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.

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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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