How to read an image using OpenCV and read a pixel in it

This recipe helps you read an image using OpenCV and read a pixel in it

Recipe Objective: How to read an image using OpenCV and read a pixel in it?

In this recipe, let us try to understand how to read an image in OpenCV.

Step 1: Importing OpenCV in python

Before we start working on OpenCV, we have to import the library in python using the following line of code.

import cv2

Step 2: Reading local image

After importing the library, we can read an image as an array in OpenCV using the built-in cv2.imread() function. This function takes two parameters which are as follows

  • path: used to specify the path to the image location
  • flag: used to determine how the image should be read

The flag parameter can take three values as inputs which are as follows

  • cv2.IMREAD_COLOR: Coloured Image format (excludes transparency)
  • cv2.IMREAD_UNCHANGED: Coloured Image format (includes transparency)
  • cv2.IMREAD_GRAYSCALE: Black and white format

These parameters can also be passed as integers such as 1, -1, and 0, respectively. The default flag value is 1.

Let us try to read an image in colored format.

OpenCV_Logo.jpg

This is the image that we are going to read

image = cv2.imread("OpenCV_Logo.jpg", cv2.IMREAD_COLOR)

Step 3: Understanding Pixel values

If we print the image variable, we can see the pixel values of the image like this.

print(image)

    array([[[255, 255, 255],
    [255, 255, 255],
    [255, 255, 255],
    ...,
    [255, 255, 255],
    [255, 255, 255],
    [255, 255, 255]],

   [[255, 255, 255],
    [255, 255, 255],
    [255, 255, 255],
    ...,
    [255, 255, 255],
    [255, 255, 255],
    [255, 255, 255]],

   [[255, 255, 255],
    [255, 255, 255],
    [255, 255, 255],
    ...,
    [255, 255, 255],
    [255, 255, 255],
    [255, 255, 255]],

   ...,

   [[255, 255, 255],
    [255, 255, 255],
    [255, 255, 255],
    ...,
    [255, 255, 255],
    [255, 255, 255],
    [255, 255, 255]],

   [[255, 255, 255],
    [255, 255, 255],
    [255, 255, 255],
    ...,
    [255, 255, 255],
    [255, 255, 255],
    [255, 255, 255]],

   [[255, 255, 255],
    [255, 255, 255],
    [255, 255, 255],
    ...,
    [255, 255, 255],
    [255, 255, 255],
    [255, 255, 255]]], dtype=uint8)

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

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Linear Regression Model Project in Python for Beginners Part 2
Machine Learning Linear Regression Project for Beginners in Python to Build a Multiple Linear Regression Model on Soccer Player Dataset.

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.