How to edit a pixel using OpenCV

This recipe helps you edit a pixel using OpenCV

Recipe Objective: How to edit a pixel using OpenCV?

In this recipe, let us understand how OpenCV reads the images as pixels and how to manipulate those pixels

Comprehensive List of Computer Vision Project Ideas for Data Enthusiasts

Step 1: Import the library and read the image

The first step is to import the library and read the image using the cv2.imshow() function. The image that we are using for this recipe is as follows.

projectpro logo
#Importing libraries
import cv2
import matplotlib.pyplot as plt

#Reading the image
image = cv2.imread('project.jpg')

Step 2: Understanding the structure of pixel array

If we look at the shape of the image variable, we can see that it is a multidimensional array of the shape (110, 335, 3)

print("The shape of the image is ",image.shape)

Output:

The shape of the image is  (110, 335, 3)

Here, 110 is the height, 335 is the width, and 3 is the number of channels (this is a color image, we have three channels in B, G, R format). Therefore, we have 110 X 335 = 36850 tuples of values each in (B, G, R). Each value in the tuple ranges from 0 to 255. Various combinations of these values form various colors. For instance,

  • (255,255,255) corresponds to white color
  • (0,0,0) corresponds to black
  • (255,0,0) corresponds to red

Step 3: Edit a pixel

Let us try to change obtain the (B, G, R) value for one pixel and edit it to our convenience

(b,g,r) = image[20][50]

This gives us the (B, G, R) tuple value of the 50th pixel in the 20th row

print("The (B,G,R) value at 50th pixel of the 20th row is ",(b,g,r))

Output:

The (B,G,R) value at 50th pixel of the 20th row is  (81, 48, 32)

Now let us change pixel the value from (81,48,32) to (255,255,255)

image[20][50] = (255,255,255)

Step 4: Display the picture

Since we have changed only one pixel, it would be better to view the image using the plt.imshow() function of the matplotlib library. Matplotlib expects an RGB format while OpenCV uses BGR format for images. Hence let us convert the color scheme using the cv2.cvtColor() function, which takes the image and the color scheme as inputs.

As we want to convert the color scheme from BGR to RGB, we use cv2.COLOR_BGR2RGB code

plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

Output:

If we look closer, we can see a tiny dot in white color above the logo, which was not previously present in the image

Output image

Download Materials

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification