Python OpenCV Draw Rectangular Shape and Extract Objects Project

We offer you a brighter future with FREE online courses - Start Now!!

When working with images, it is often necessary to extract specific objects or regions of interest from the image. One way to do this is by drawing a rectangle around the object and using that rectangle to crop the image. This technique can be used for a variety of applications, such as object detection, image segmentation, and feature extraction.

About Draw Rectangular Shape and Extract Objects

In this project, we will show you how to draw a rectangle on an image using Python and OpenCV. We will also demonstrate how to extract the object within the rectangle and save it as a separate image.

Prerequisites For drawing rectangular Shapes and Extract Objects Using Python OpenCV

It is important to have a solid understanding of the Python programming language and the OpenCV library. Apart from this, you should have the following system requirements.

  • Python 3.7 (64-bit) and above
  • Any Python editor (VS code, Pycharm)

Download Python OpenCV Draw rectangular shapes and extract objects project.

Please download the source code of Python OpenCV Draw Rectangular Shape and extract objects project from the following link: Python OpenCV Draw a rectangular shape and extract objects project code.

Installation

Open Windows cmd as administrator

1. To install the opencv library, run the command from the cmd.

pip install opencv-python

Let’s Implement

1. We need to import some libraries that will be used in our implementation.

import cv2

2. Initialize an empty list to store the coordinates of the rectangle.

cord_rect = []

3. The code provides a function that allows the user to draw rectangular shapes on an image using the mouse. When the left mouse button is clicked down and released, the function records the positions and uses them to draw a rectangle on the image. Additionally, the function calls another function to extract the rectangular object from the original image.

def mouse_callback(event, x, y, flags, param):
    global cord_rect
    if event == cv2.EVENT_LBUTTONDOWN:
        cord_rect = [(x, y)]
    elif event == cv2.EVENT_LBUTTONUP:
        cord_rect.append((x, y))
        cv2.rectangle(image, cord_rect[0], cord_rect[1], (0, 0, 255), 2)
        cv2.imshow('ProjectGurukul', image)
        extract_object()

4. The extract_object() function extracts a rectangular object from an image using the coordinates of the rectangle drawn on the image using the mouse. It displays the extracted object and saves it as a new image, which can be used for other image-processing tasks.

def extract_object():
    if len(cord_rect) == 2:
        x1, y1 = cord_rect[0]
        x2, y2 = cord_rect[1]
        object = image[y1:y2, x1:x2]
        cv2.imshow('Extracted_Object_by_ProjectGurukul', object)
        cv2.imwrite('C:/Users/yoges/OneDrive/Desktop/dataflair/Draw rectangular shape and extract objects - Yogesh\images/extracted_object.jpg', object)

5. The cv2.imread() function is used to read an image from a specified file path.

image = cv2.imread('C:/Users/yoges/OneDrive/Desktop/dataflair/Draw rectangular shape and extract objects - Yogesh/fruit.jpg')

6. The code uses the cv2.namedWindow() function to create a window with the name ‘ProjectGurukul’, which will be used to display the image.

cv2.namedWindow('ProjectGurukul')

7. The ‘ProjectGurukul’ window is set to call the mouse_callback() function when the user clicks or releases the left mouse button.

cv2.setMouseCallback('ProjectGurukul', mouse_callback)

8. The image is displayed in a window with the name ‘ProjectGurukul’, which was created using the cv2.namedWindow() function

cv2.imshow('ProjectGurukul', image)

9. cv2.waitKey(0) waits for a keyboard event until a key is pressed, and then the program continues to run. cv2.destroyAllWindows() is called at the end of the program to destroy all windows created by the program.

cv2.waitKey(0)
cv2.destroyAllWindows()

Python OpenCV Draw Rectangular Shape And Extract Objects Output:

opencv

opencv project

Conclusion

In this project, we saw how to draw rectangular shapes using OpenCV and extract objects from an image. We first used the cv2.rectangle() function to draw a rectangle on an image and extract the object within the rectangle. We also covered how to display the extracted object and save it as a new image. Drawing rectangular shapes and extracting objects is a common task in computer vision and image processing. By using OpenCV, we can easily accomplish this task and perform further analysis on the extracted objects.

Leave a Reply

Your email address will not be published. Required fields are marked *