VOOZH about

URL: https://www.analyticsvidhya.com/blog/2021/07/advanced-opencv-numpy-operations-cropping-copying-and-pasting/

⇱ OpenCV and NumPy Operations: Cropping, Copying, And Pasting


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

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

Reading list

Advanced OpenCV and NumPy Operations: Cropping, Copying, And Pasting

Shiv Last Updated : 28 Jul, 2021
7 min read

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

Introduction

Computer Vision is a real-world application of Machine Learning, that provides computers with the ability to see and identify physical 3D objects in real-time. Computer Vision plays an extremely important role when a robot is required to be autonomous in its navigation and mobility. Computer Vision allows us to indulge in high-level computation and processing tasks.

If you have not read through my previous articles on Computer Vision and OpenCV and would like to do so, please navigate to the following hyperlinks:

This article will aim to introduce us to more features pertaining to the OpenCV package in Python Programming Language. To facilitate this particular learning process, we will attempt to perform our OpenCV Operations on an image that may be downloaded from this link. Alternatively, you may save the image found below.

Source: The Humble I.

Getting Started

At this point in our OpenCV learning experience, we should by now, be familiar with the first and foremost task to perform- That is, the task of loading the image into our system memory-
and to do so, we are required to import the necessary packages into our system
memory. The importation and loading code is as follows:

# import the required packages
import cv2
import os
import numpy as np
# load the image into system memory
image = cv2.imread('C:/Users/Shivek/Pictures/Nature.jpg', flags=cv2.IMREAD_COLOR)
cv2.imshow('Analytics Vidhya Computer Vision- Nature', image)
cv2.waitKey()
cv2.destroyAllWindows()

The main points to note in the above code blocks are as follows:

  • We imported the necessary python packages into our system memory.
  • We made use of the imread() method to facilitate the process of loading data (the image) from the Hard Disk Drive into our system RAM.
  • We load the image in default/standard format, i.e., color format.

Output to the above code blocks will be seen as follows:

The image looks good! However, on the vast plain, we can see, there
exists only one tree. Let us fix this using OpenCV and NumPy techniques.

Cropping An Image

The technique of cropping involves making physical adjustments to an image using digital means. Cropping is useful in times of isolating the required portion of an image from the rest, or for bringing the attention of the viewer to a particular area of the image.

As we already know, OpenCV represents an image as a NumPy array comprising integers that represent the pixels and intensity- hence, by indexing and slicing portions of the NumPy array, we are essentially isolating specific pixels thereby isolating specific portions of the image itself, thus allowing us to effectively crop the image.

Since cropping an image requires indexing and slicing, it is important that we know the shape of our matrix, i.e., NumPy array. This is because we will use the index values to perform this task.

print(image.shape)
print("Rows of Pixels: %d Rows"%(image.shape[0]))
print("Columns of Pixels: %d Columns"%(image.shape[1]))
print("Color Channels: %d Color Channels"%(image.shape[2]))

Output to the above lines of code will show as follows:

From the above image, one is able to see that our image has the
following properties:

  • 459 Rows of Pixels.
  • 736 Columns of Pixels.
  • 3 Color Channels- By Default BGR is used, not RGB. You will find that the Red and Blue color channels have been swapped.

As we have seen previously, our downloaded image shows a geographical plain, which has a single tree. The image looks good, but we will attempt to populate the plain with a few more trees.

Add more trees to the plain.

It is good to keep in mind that we are working with arrays- although it is an image- it is in the form of an array. With an array, we are able to , and . Essentially, what we aim to do is , and thereafter .

To explain the above methodology, allow me to provide you with a small-scale example.

Example of our objective

Here is a small example to explain the process that we will be performing, to you.

We will import NumPy, and create an array of zeros with dimensions 6 rows and 6 columns representing the original image. We will print the array of zeros to the console.

import numpy as np
numpy_zeros = np.zeros(shape=(6, 6))
print(numpy_zeros)

Our NumPy array of zeros is as below:

Next, we will create an array of ones with dimensions 4 row and 4 columns representing the tree. We will print the array of ones to the console.

numpy_ones = np.ones(shape=(4, 4))
print(numpy_ones)

The output will be seen as follows:

Now, we will . We will select the portion of values using slicing techniques. It is good to know that the size of the array you are imputing values into, must be the same size as the array with the values to be imputed.

numpy_zeros[1:5, 1:5] = numpy_ones
print(numpy_zeros)

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

And we have successfully manipulated our array. The process of cropping works in a similar way- I do hope that you have a better understanding of the process. Let’s scale it up!

Coming Back To OpenCV

So our objective is to find the pixel array that makes up the tree, isolate this pixel array, and make portions of the original array be equal to the tree pixel array. It sounds difficult, but it is not and is very interesting.

We begin by finding the matrix that comprises the tree as below:

tree = image[320:393, 570:665]
cv2.imshow('Nature Tree', tree)
cv2.waitKey()
cv2.destroyAllWindows()

Let us display the output of the above pixels to the screen- Output will be seen as follows:

  • The image is as it should be.
  • The gray-colored area is visible because the image is small.
  • There is no title bar visible due to the size of the image.
  • If you would like to see the image in a full window, you may maximize the window and upon which doing, you will be presented with the below output:

Let us view the shape of the pixel array:

print('Shape Of Tree: ', tree.shape)
print("Rows of Pixels: %d Rows"%(tree.shape[0]))
print("Columns of Pixels: %d Columns"%(tree.shape[1]))
print("Color Channels: %d Color Channels"%(tree.shape[2]))

The output to the above code block will be seen as follows:

Now that we have the pixel array of the tree, as well as the size of the array, we can set array portions in the original image to be equal to the tree
pixel array- Remember that both pixel arrays need to be the same size.

image[333:406, 0:95] = tree
image[333:406, 95:190] = tree[:, ::-1, :]
image[323:396, 190:285] = tree[:, ::-1, :]
image[323:396, 285:380] = tree[:, :, :]
image[312:385, 380:475] = tree[:, ::-1, :]
image[315:388, 475:570] = tree

Now let us display the original image to the screen. We expect to find more trees in our image:

cv2.imshow('Cropped Trees', image)
 cv2.waitKey()
 cv2.destroyAllWindows()

Output to the above code block will show as follows:

And thus, we have successfully cropped the tree from our image and added more trees to the original image, using a method of Copying And Pasting.

This concludes my article on I do hope that you found this article enlightening and interesting and have new takeaways of the OpenCV package in Python.

Please feel free to connect with me on LinkedInIf you would like to see all articles that I have composed for Analytics Vidhya, please navigate to my Analytics Vidhya Profile.

Thank you for your time.

I would like to thank the reader for taking the
time to read my article up to this point, as from my view, it is one of the most detailed and
time-consuming articles that I have written. I hope you have learned a new
concept about OpenCV Operations- .

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