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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.