VOOZH about

URL: https://www.analyticsvidhya.com/blog/2021/07/performing-computer-vision-task-with-opencv-and-python/

⇱ Computer Vision With OpenCV And Python | What is OpenCV for CV?


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

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

Reading list

Performing Computer Vision Task With OpenCV And Python

Shiv Last Updated : 27 Oct, 2024
5 min read

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

Introduction

Data is often defined as raw facts. Information refers to any amounts or quantities of data that have undergone processing and holds more value than the raw facts themselves. The key difference is that decisions can be taken, and actions made, based upon the studying of Information- You should not base decisions and actions on data, Only information.

Source: StudiousGuy

Recap

Following our previous article on Computer Vision, we are now going to further explore the world of Computer Vision in the Python Programming Language, using the OpenCV Python package. This article will show us how to perform a few more of the many operations offered by OpenCV in the Python Programming Language. In the previous article, we examined the below block of code and will now look at a few more aspects to it, in this article.

Python Code:

import cv2
import imutils

img = cv2.imread('image.png')
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Source: Medium

Understanding CV Basics

As you may have seen, in our previous article, we have read (loaded) the OpenCV Logo image into our system memory, using the OpenCV library’s built-in method, imread(). Upon utilizing this method, we passed in two arguments, namely, a filename and a flag. The filename specified the name and location of the file on your personal computer, while the flag could be seen as the color setting for the image. Upon recollection, we remember that we read the image into our memory in a GRAYSCALE color format.

Images are data. When you use the OpenCV imread() method, you are converting that raw image data into another datatype. The new datatype is one that all of us on Analytics Vidhya are very familiar with and that is . Each element in the array represents pixel color intensity and may have one or more elements within it. Since we have loaded our array in GRAYSCALE color format, one will find that each pixel is represented as a single value that may take on a value equal to, and ranging from, zero (0) to two-hundred and fifty-five (255). As one moves from 0 to higher values, the intensity of a specific pixel increases, hence making it more striking to the eye.

0 = The Color Black.
255 = The Color White.

Essentially what I am trying to convey is as below(I have omitted the source as it is the same as the previous article, and also to allow for the flow of information):

We started with the image below:

πŸ‘ Computer Vision With OpenCV And Python opencv logo

Next, we returned the image in GRAYSCALE and obtained the image in a new format as below:

πŸ‘ Image

Now, we shall print the contents of the variable which is storing our GRAYSCALE image:

# variable image stored our GRAYSCALE image
print(image)

-we receive output to the above code as follows:

πŸ‘ array
πŸ‘ Image

Now, we shall print the type of the variable image:

print(type(image))

The output will be seen as follows:

πŸ‘ numpy array

Fundamentally, OpenCV has transformed our image into a NumPy array, in which there are values from 0 to 255 representing pixel intensity, that correspond to the colors we see in the GRAYSCALE image. Remember GRAYSCALE images will always return an array in which each pixel has a single value that ranges from 0 (Black) to 255 (White).

Returning The Shape of The Array.

Let us print the shape of the NumPy array to the console.

print(image.shape)

The output will be seen as follows:

πŸ‘ shape

Our array has 600 rows and 487 columns. In image terminology, one would say that the image has the dimensions 600 pixels (height), by 487 pixels (width).

Printing The Image Using Pixels

Since OpenCV has transformed our image pixels into a NumPy array with integers, we may perform NumPy operations on the array containing image pixel values, and manipulate the array.

Our array is 2-Dimensional. This means it has rows and columns. Let us perform a bit of indexing and slicing on the array, and return the contents.

cv2.imshow("AV", image[0:100])

cv2.waitKey()

cv2.destroyAllWindows()

If one is familiar with the technique of indexing and slicing, one will be able to see that we are attempting to slice a portion of our image (NumPy) array. Again, it is crucial to understand and be conscious of the fact that OpenCV Library in Python Programming Language represents its images and associated objects as NumPy nd-Arrays.

πŸ‘ Computer Vision With OpenCV Printing The Image Using Pixels

Source: Indian AI Production.

Code Explanation.

cv2.imshow(β€œAnalytics Vidhya Computer Vision β€œ, image[0:100])

The imshow() method is used to display an image to the screen employing a GUI. However, in this particular instance we passed in a name for the GUI window, and only a portion of the pixel array, using slicing. Specifically, we wish to return the first 100 rows (height) from the image.

cv2.waitKey()

This will wait infinitely for the GUI window to be closed- i.e., user action/interaction will close this window. You may pass an integer value as an argument representing the duration (in milliseconds), the GUI window should wait before terminating automatically.

cv2.destroyAllWindows()

The above line of code will terminate all active/open OpenCV GUI windows. You may pass in the name of a specific GUI window to terminate as a string.

The output will be seen as follows:

πŸ‘ Av CV

Thus, we have successfully returned the first 100 rows of pixels from our image. Feel free to experiment with the image, and look at whether the pixel at a particular position, matches that of the color found on the image itself.

This concludes my article on Computer Vision With Python. I do hope that you enjoyed reading through this article, and have learned a new concept.

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

Nano Course: Dreambooth-Stable Diffusion for Custom Images

Learn to create custom images with Dreambooth Stable Diffusion technology

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