Selective Search using Region proposals

We can use ‘selectivesearch.selective_search’ function to perform Selective Search on the image

Installing packages

pip install selectivesearch

Importing libraries

Python3

# importing necessary libraries
import cv2
import selectivesearch
import matplotlib.pyplot as plt

                    

Loading image

Python3

# Load the image
image_path = 'elephant.jpg'
image = cv2.imread(image_path)

                    

This code will load the image using openCV’s ‘imread’ function and stores it in the variable ‘image’.

Image Dimensions

Python3

# Print the dimensions of the loaded image
print(image.shape)
# Retrieve the width of the image
image.shape[1]
# Calculate the scaling factor for the image's height
(new_height / (image.shape[0]))

                    

Here we are loading image and printing its dimensions, including height, width. Additionally it calculates the scaling factor for a new height based on a specified value.

Resize image to reduce computation time

Python3

# Calculate a new height
import matplotlib.pyplot as plt
new_height = int(image.shape[1] / 4)
# Calculate a new width
new_width = int(image.shape[0] / 2)
# Resize the image to the new dimensions
resized_image = cv2.resize(image, (new_width, new_height))
 
# Display the resized image using Matplotlib
plt.imshow(resized_image)
plt.show()

                    

Output:

This code calculates new dimensions for an image based on specific proportions, resize the image accordingly, and then displays the resized image using matplotlib, resulting in a smaller representation of the original image.

Applying Search Selective on resized image

Python3

# applying selective search
img_lbl, regions = selectivesearch.selective_search(
    resized_image, scale=500, sigma=0.9, min_size=10)

                    

“selectivesearch.selective_search” is a function provided by the selective search algorithm for object detection and image segmentation. This function performs a region proposal process on an input image to identify and generate potential regions of interests that may contain objects.
This code applies selective search algorithm to the resized image, aiming to generate region proposals for object detection. ‘Scale’ controls the trade off between the number of generated regions and their quality. Large values result in fewer regions but potentially higher quality. ‘Sigma’ value for gaussian function is used in smoothing the image. ‘min_size’ is the minimum size of a region proposal. Regions smaller than this are discarded.
After this, it performs the selective search and returns two main outputs i.e., ‘img_lbl’ contains the image labels and ‘regions’ contains the regions of interest generated by the algorithm. These regions represent potential objects in the image.

Calculating the Region

Python3

# Initialize an empty set to store selected region proposals
candidates = set()
 
# Iterate over all the regions detected by Selective Search
for r in regions:
    # Check if the current region's rectangle is already in candidates
    if r['rect'] in candidates:
        continue  # Skip this region if it's a duplicate
 
    # Check if the size of the region is less than 200 pixels
    if r['size'] < 200:
        continue  # Skip this region if it's too small
 
    # Extract the coordinates and dimensions of the region's rectangle
    x, y, w, h = r['rect']
 
    # Avoid division by zero by checking if height or width is zero
    if h == 0 or w == 0:
        continue  # Skip this region if it has zero height or width
 
    # Check the aspect ratio of the region (width / height and height / width)
    if w / h > 1.2 or h / w > 1.2:
        continue  # Skip this region if its aspect ratio is not within a range
 
    # If all conditions are met, add the region's rectangle to candidates
    candidates.add(r['rect'])

                    

This code iterates through the regions generated by selective search, filtering and selecting regions based on specific criteria. It adds the valid regions to the candidates set. After the loop, it iterates over the detected regions.

Bounding Box Scaling

Python3

# Convert the selected bounding boxes to the original image size
candidates_scaled = [(int(x * (image.shape[1] / new_width)),
                      int(y * (image.shape[0] / new_height)),
                      int(w * (image.shape[1] / new_width)),
                      int(h * (image.shape[0] / new_height)))
                     for x, y, w, h in candidates]
 
return candidates_scaled

                    

Here , in this code, it scales bounding boxes from a resized image back to their original dimensions, ensuring that regions of interest are accurately represented in the original image. It uses ratios between original and resized image dimensions for the scaling.

Search Selective for Object detection

Python3

# Load the image
image_path = 'elephant.jpg'
image = cv2.imread(image_path)
 
# Get selective search object proposals
proposals = selective_search(image)
 
# Draw the proposals on the image
output_image = image.copy()
for (x, y, w, h) in proposals:
    cv2.rectangle(output_image, (x, y), (x + w, y + h), (0, 255, 0), 20)
 
# Display the result 
plt.imshow(output_image)
plt.show()

                    

Output:

This code loads an image, applies selective search to generate object proposals, and then draws bounding boxes around these proposals on the image. Finally, it displays the image with the drawn bounding boxes.

Another Example

Python3

# Load the image from the specified path
image_path = 'dog.jpg'
image = cv2.imread(image_path)
 
# Get region proposals using Selective Search
proposals = selective_search(image)
 
# Create a copy of the original image to draw bounding boxes on
output_image = image.copy()
 
# Iterate through each region proposal and draw bounding boxes
for (x, y, w, h) in proposals:
    # Define the coordinates and dimensions of the bounding box
    x1, y1 = x, y  # Top-left corner
    x2, y2 = x + w, y + # Bottom-right corner
 
    # Draw a green bounding box around the region proposal on the output image
    cv2.rectangle(output_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
 
# Display the result image with bounding boxes
plt.imshow(output_image)
plt.show()

                    

Output:

Time complexity for this code is divided into different segments: Resizing of image, selective search, Iteration through different regions, scaling, drawing and showing up the results.

  1. Resizing the image: O(H * W) where H and W are height and width respectively .
  2. Selective Search Complexity: This time complexity is typically influenced by factors like the number of pixels, the scale parameter, and the complexity of region merging.
  3. Iteration through regions: O(N) where N is number of regions.
  4. Scaling: O(B), where B is the number of bounding boxes.
  5. Drawing and showing up the result: O(B), where B is the number of bounding boxes.

Overall complexity would be the sum of all the complexities of sub categories i.e. O(H * W) + O(Selective Search) + O(N) + O(B) + O(B)


Application of Selective Search Algorithm in Object Detection

Selective search plays an important role in R-CNN(Region – based Convolutional Neural Network) family of the object detection models. It primary function is to efficiently generate a diverse set of region proposals, which are potential bounding boxes likely to contain objects within an image. This significantly reduces the computational burden by narrowing down the regions of interest for subsequent analysis. The generated region proposals are used as input to the R-CNN architecture, where they undergo feature extraction, classification and localization. High recall is a key benefit, ensuring that most potential object regions are included, even if some are false positives. R-CNN models then refine these proposals for accurate object localization. Ultimately, this collaboration between Selective Search and R-CNN model enhances both the efficiency and accuracy of object detection, making them suitable for real-time applications and complex scenes.



OpenCV Selective Search For Object Detection

OpenCV is a Python library that is used to study images and video streams. It basically extracts the pixels from the images and videos (stream of image) so as to study the objects and thus obtain what they contain. It contains low-level image processing and high-level algorithms for object detection, feature matching etc.

In this article, we will dive into a computer vision technique i.e. selective search for Object Detection in OpenCV.

Table of Content

  • Object Detection
  • Selective Search
  • Implementation of SelectiveSearch using python OpenCV
  • Selective Search using Region proposals

Similar Reads

Object Detection

...

Selective Search

Object Detection is a fundamental computer vision task that involves identifying and localizing objects or specific features within an image or a video stream. It plays a crucial role in numerous applications, including autonomous driving, surveillance, robotics, and image analysis. Object detection goes beyond mere object recognition by providing both the classification of objects into predefined categories or classes and the precise localization of where these objects exist within the image....

Implementation of SelectiveSearch using python OpenCV

Selective Search is a region-based technique extensively used for object detection tasks within computer vision. It aims to generate a varied set of region proposals from an input image, where each region proposal representing a potential object or object portion.These region proposals are subsequently used as candidate regions by object detection algorithms to categorize and localize objects within an image....

Selective Search using Region proposals

Let’s Manually build the Region for SelectiveSearch...