VOOZH about

URL: https://www.analyticsvidhya.com/blog/2021/07/some-advanced-opencv-operations-for-your-computer-vision-project/

⇱ Some Advanced OpenCV Operations For Your Computer vision Project!


India's Most Futuristic AI Conference Is Back – Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

Reading list

Some Advanced OpenCV Operations For Your Computer vision Project!

Shiv Last Updated : 22 Jul, 2021
6 min read

This article was published as a part of the Data Science Blogathon

Introduction.

Computer Vision is said to be one of the most interesting application fields of Machine Learning. Computer Vision. As implied by the name, Computer Vision enables computers to detect, identify, and recognize, objects and patterns in the 3D surroundings.

If you have not viewed my previous articles on Computer Vision and would like to do so, kindly navigate to the following hyperlinks:

This article will introduce you to more features of the OpenCV Library.

Source: Analytics Vidhya.

Recap

As we seek to explore more about the world of OpenCV, let us take a moment to recap, or brush up, on what we have learned up to this point.

  • We have obtained a short, general insight into what is Machine Learning?
  • We have been introduced to the OpenCV Library.
  • We are aware that our implementation of OpenCV is in Python Programming
    Language.
  • We loaded an image into system memory and observed an image in two different colour formats, viz., standard (default) and GRAYSCALE.
  • We understand the base nature in which OpenCV represents images, i.e., The NumPy Array comprising integers, representing pixel intensity. Up to this point, we have only looked at a GRAYSCALE image.
  • We viewed a few important properties of the image array such as the contents, shape, and type of data.

OpenCV Python Programming.

We understand exactly how to load an image into the system’s main memory. We will now look at more complex operations that can be performed on image data. To facilitate this OpenCV learning experience we will be using an image that may be downloaded from this link. Alternatively, you may save the image found below.

Source: Pinterest.

Loading The Image.

The first and foremost step will be to load the image into our system memory as follows:

import cv2
 # load it in GRAYSCALE color mode...
 image = cv2.imread("""C:/Users/Shivek/Pictures/cd0c13629f217c1ab72c61d0664b3f99.jpg""", 0)
 cv2.imshow('Analytics Vidhya Computer Vision- Nature.png Gray', image)
 cv2.waitKey()
 cv2.destroyAllWindows()

C:/Users/Shivek/Pictures/cd0c13629f217c1ab72c61d0664b3f99.jpg 

The above code successfully loads the required image into our system memory. It is good to know that we
have loaded the image in GRAYSCALE colour format.

(Explanation to the above code will be omitted as it is the same as the previous article(s).

Output to the above code block will be seen as below:

Obtaining and Understanding Image Properties.

Let us gain insight into our image by viewing the associated properties. We will now talk about the shape of the image.

print(image.shape)

Output is as below:

Notice that the shape method returns two values to us in the form of a tuple. These two values represent the height (y-axis) and width (x-axis) of the image, respectively. Essentially what I am saying is:

print("The Image Height Is: %d Pixels"%(image.shape[0]))
 print("The Image Width Is: %d Pixels"%(image.shape[1]))

Output to the above code is
as below:

There are some situations in which the shape method returns a tuple of three integer values. The first value represents the height (y-axis), the second value the width (x-axis), and the third value, the number of color channels.

Colour channels affect the colour ranges (i.e., Types/Shades of colours | Reds, Blues, Pinks, etc.) of your pixels. Taking our example, we have a GRAYSCALE image- A GRAYSCALE image has
just one color channel because each element in the pixel array contains just
one value that ranges from 0 to 255.

Looking at Image Color Channels.

For example, and experience purposes, let us re-load our nature image, and view the shape properties- However, in this particular instance, we are loading the image in standard color format.

import cv2
# re-load it in color mode...
image = cv2.imread("C:/Users/Shivek/Pictures/cd0c13629f217c1ab72c61d0664b3f99.jpg", cv2.IMREAD_COLOR)
cv2.imshow('Analytics Vidhya Computer Vision- Nature.png Color', image)
cv2.waitKey()
cv2.destroyAllWindows()

The color image will be seen as below:

Our image in color is as downloaded- i.e., it has not been manipulated in any way whatsoever. Now let us proceed to use the shape method on this image.

print(image.shape)
 print("Image Height: %d Pixels"%(image.shape[0]))
 print("Image Width: %d Pixels"%(image.shape[1]))
 print("Number Of Color Channels: %d "%(image.shape[2]))

Output to the above code will be seen as follows:

Notice that there is a third number present in the tuple. This is the number of color channels present in the image. The particular color channel present in this image is BGR Color Channel. It is BGR, not RGB.

Because the image is in standard color, looking at the image one can see several mixtures and shades of color. It is because there are three color channels- each element of the pixel array, has three values ranging from 0 to 255 and each value represents the intensity of BGR (Blue, Green, Red) at that particular pixel, therefore
allowing for the colors to be mixed and manipulated. We will focus more on color images in future articles.

Learning How to Resize an Image.

Coming back to our GRAYSCALE image, we will now attempt to resize the image, to make it cover a smaller surface area on our computer screen. To rescale an image, we need to use the resize() method offered by the OpenCV Library.

import cv2
 # load the image in a GRAYSCALE format
 image = cv2.imread("C:/Users/Shivek/Pictures/cd0c13629f217c1ab72c61d0664b3f99.jpg", cv2.IMREAD_GRAYSCALE)
 cv2.imshow('Analytics Vidhya Computer Vision- Nature Standard Image', image)
 cv2.waitKey()
 cv2.destroyAllWindows()
 # resize the image to be pixel dimensions 350 by 350
 resized_image = cv2.resize(image, dsize=(350, 350))
 cv2.imshow('Analytics Vidhya Computer Vision- Nature Resized (350, 350)', resized_image)
 cv2.waitKey()
 cv2.destroyAllWindows()

resized_image = cv2.resize(src=image, dsize=(350, 350))

We make use of the resize() method to resize the image at hand- this could mean either increasing the size or decreasing it. We chose to decrease the image size, i.e., make it smaller. This method takes in two primary arguments:

  1. src = The Source Image (Image to
    be resized).
  2. dsize = The Desired Size.
    It requires that you specify the pixel dimensions for height and width, between which the image will be resized. The values need to be passed in an immutable tuple. We have specified a height and width of both 350 pixels.
cv2.imshow('Analytics Vidhya Computer Vision- Nature Resized (350, 350)', resized_image)
cv2.waitKey()
cv2.destroyAllWindows()

Up to this point, one should be familiar with what the three lines of code above do- It will display the window, wait an indefinite period of time, and terminate all windows upon user command.

The last task for us would be to verify that the image size has been reduced.

The output will be seen as follows:

This concludes my article on . I do hope that you enjoyed reading through this article and have added new information to your knowledge base.

Please feel free to connect with me on LinkedIn.

Thank you for your time.

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.

Login to continue reading and enjoy expert-curated content.

Free Courses

How to Build an Image Generator Web App with Zero Coding

Learn to build an image generator web app with zero coding skills.

Mastering Multimodal RAG & Embeddings with Amazon Nova & Bedrock

​Master multimodal RAG and embeddings using Amazon Nova and Bedrock.

Learning Autonomous Driving Behaviors with LLMs and RL

Train RL agents for autonomous driving with safe, human-like behavior.

Building Your First Computer Vision Model

Build your first computer vision model with Pytorch.

Responses From Readers

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
πŸ‘ Av Logo White

Continue your learning for FREE

Forgot your password?
πŸ‘ Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

πŸ‘ Popup Banner
πŸ‘ AI Popup Banner