#OpenCV – How to convert to grayscale and show a single color an image πŸ–ΌοΈ using Python πŸ

Hi !

Super quick post today with a cool scenario to support my TikTok videos in Spanish (I know πŸ˜€):

How to convert to grayscale an image and show a single color using python and opencv

Here is the output with an original photo from my office and a new image with this effect:

opencv convert to grayscale and show a single color

Disclaimer: I got this example in my notes from long time ago. I thik I got this code from a blog post or a public repository, however, I can’t find the original source. If you know it, please let me know so I can mention the author.

Sample code:

# Copyright (c) 2022
# Author : Bruno Capuano
# Create Time : 2022 Feb
# Change Log :
# – Open a local image
# – Convert an image to B&W, and resalt a specific color in RED
#
# 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
# open an image using opencv
imgOriginal = cv2.imread('Office2.jpg')
img = cv2.resize(imgOriginal, (324, 720))
cv2.imshow('@ElBruno – Office', img)
# get image height and width
height, width, channels = img.shape
#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 hue
lower_red = np.array([160,100,50])
upper_red = np.array([180,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 – Captain America', img_ca)
# save image using opencv
cv2.imwrite('OfficeCaptainAmerica.jpg', img_ca)
# key controller
cv2.waitKey(0)
cv2.destroyAllWindows()

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.


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 )

Twitter picture

You are commenting using your Twitter 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: