15 Mar Add a Color Tint with OpenCV
With OpenCV, easily add a color tint (e.g., blue or red) to the image. We will create a blue tint here. Set the red and green channels to 0 for the blue-tint array:
1 2 3 4 5 6 |
# Create a blue tint blue_tint = image.copy() blue_tint[:, :, 1] = 0 # Set the greenchannel to 0 blue_tint[:, :, 2] = 0 # Set the red channel to 0 |
Let us see an example of adding a color tint to an image:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# Read and display an image with OpenCV. Add a color tint. import cv2 # Load an image image = cv2.imread(r'C:\Users\hp\Downloads\Astronaut.png') # Create a blue tint blue_tint = image.copy() blue_tint[:, :, 1] = 0 # Set the green channel to 0 blue_tint[:, :, 2] = 0 # Set the red channel to 0 # Display the result cv2.imshow("Blue Tint Filter", blue_tint) # Wait for a key press and close the window cv2.waitKey(0) cv2.destroyAllWindows() |
The following is the output:
Explanation:
This Python script demonstrates how to use OpenCV to load an image, apply a blue tint to it, and display the result:
- The script begins by importing OpenCV and loading an image from the specified file path into the variable
image
using thecv2.imread()
function. - To create a blue tint effect, the script first makes a copy of the loaded image and assigns it to the variable
blue_tint
. - Then, it sets the green and red color channels of the copied image to
0
, effectively removing those colors. This is achieved by modifying the array representing the image:blue_tint[:, :, 1] = 0
sets the green channel to zero, andblue_tint[:, :, 2] = 0
sets the red channel to zero, leaving only the blue channel active. - The resulting image, now tinted blue, is displayed in a window titled “Blue Tint Filter” using the
cv2.imshow()
function. - The script waits for a key press with
cv2.waitKey(0)
before closing the display window usingcv2.destroyAllWindows()
. This approach allows users to visualize the applied blue tint effect interactively.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
- Generative AI Tutorial
- Machine Learning Tutorial
- Deep Learning Tutorial
- Ollama Tutorial
- Retrieval Augmented Generation (RAG) Tutorial
- Copilot Tutorial
- Gemini Tutorial
- ChatGPT Tutorial
No Comments