Using OpenCV's dnn::NMSBoxes() Function in Software Development (2024)

Abstract: This article discusses the implementation of OpenCV's dnn::NMSBoxes() function in software development, addressing environment restrictions that prevent manual function implementation.

2024-06-24 by Try Catch Debug

Using OpenCV's cv::dnn::NMSBoxes() Function for Object Detection in Software Development

OpenCV is an open-source computer vision and machine learning software library. One of its powerful features is the Deep Neural Network (DNN) module, which allows developers to use pre-trained deep learning models for object detection tasks. The Non-Maximum Suppression (NMS) algorithm is an essential step in object detection to eliminate redundant bounding boxes and select the most probable object. In this article, we will explore how to use OpenCV's cv::dnn::NMSBoxes() function for object detection in software development.

Prerequisites

Before diving into the cv::dnn::NMSBoxes() function, it is essential to have a basic understanding of the following concepts:

  • Deep learning and neural networks
  • Convolutional Neural Networks (CNNs) for object detection
  • OpenCV and its DNN module

Object Detection Overview

Object detection is the process of identifying objects in an image or video and drawing bounding boxes around them. Modern object detection techniques involve deep learning models such as Faster R-CNN, YOLO, and SSD. These models use CNNs to extract features from images and predict the presence and location of objects.

Non-Maximum Suppression (NMS)

NMS is an algorithm used to eliminate redundant bounding boxes in object detection. When multiple bounding boxes overlap, NMS selects the most probable object by choosing the bounding box with the highest confidence score and discarding the others. The cv::dnn::NMSBoxes() function in OpenCV implements the NMS algorithm for object detection.

Using cv::dnn::NMSBoxes() Function

To use the cv::dnn::NMSBoxes() function, you need to follow these steps:

  1. Load the pre-trained deep learning model using the OpenCV DNN module.
  2. Perform object detection on the input image or video frame using the loaded model.
  3. Apply the cv::dnn::NMSBoxes() function to eliminate redundant bounding boxes.

Step 1: Loading the Model

First, you need to load the pre-trained deep learning model using the OpenCV DNN module. Here's an example of how to load a YOLOv3 model:

#include <opencv2/dnn.hpp>#include <opencv2/imgproc.hpp>#include <opencv2/highgui.hpp>using namespace cv;using namespace dnn;int main() {// Load the YOLOv3 modelNet net = readNet("yolov3.weights", "yolov3.cfg");// Load the class namesvector<string> classNames;ifstream classNamesFile("coco.names");string className;while (getline(classNamesFile, className)) {classNames.push\_back(className);}}

Step 2: Performing Object Detection

Next, you need to perform object detection on the input image or video frame using the loaded model. Here's an example of how to perform object detection using YOLOv3:

// Perform object detectionMat frame = imread("input.jpg");Mat blob = blobFromImage(frame, 1 / 255.0, Size(416, 416), Scalar(0, 0, 0), true, false);net.setInput(blob);Mat detections = net.forward("yolo\_82");// Process the detectionsvector<Mat> detectionsMat(detections.size[2]);for (int i = 0; i < detections.size[2]; i++) {detectionsMat[i] = detections.row(i).colRange(4, detections.cols);}vector<Rect> boxes;vector<int> classIds;vector<float> confidences;for (int i = 0; i < detectionsMat.size(); i++) {// Scan through the bounding boxes output from the networkMat scores = detectionsMat[i];for (int j = 0; j < scores.rows; j++) {float confidence = scores.at<float>(j, 4);if (confidence > 0.5) {int centerX = static\_cast<int>(scores.at<float>(j, 0) \* frame.cols);int centerY = static\_cast<int>(scores.at<float>(j, 1) \* frame.rows);int width = static\_cast<int>(scores.at<float>(j, 2) \* frame.cols);int height = static\_cast<int>(scores.at<float>(j, 3) \* frame.rows);int left = centerX - width / 2;int top = centerY - height / 2;boxes.push\_back(Rect(left, top, width, height));classIds.push\_back(i);confidences.push\_back(confidence);}}}

Step 3: Applying cv::dnn::NMSBoxes() Function

Finally, you need to apply the cv::dnn::NMSBoxes() function to eliminate redundant bounding boxes. Here's an example of how to use the cv::dnn::NMSBoxes() function:

// Apply Non-Maximum Suppression (NMS)float thresh = 0.45;vector<int> indices;NMSBoxes(boxes, confidences, thresh, 0, indices);// Draw the bounding boxes on the input imagefor (int i = 0; i < indices.size(); i++) {int idx = indices[i];Rect box = boxes[idx];Scalar color = Scalar(255, 0, 0);rectangle(frame, box, color, 2);// Draw the class name and confidence scoreint baseLine = 0;string label = format("%.2f", confidences[idx]);Size labelSize = getTextSize(label, FONT\_HERSHEY\_SIMPLEX, 0.5, 1, &baseLine);rectangle(frame, Point(box.x, box.y - labelSize.height),Point(box.x + labelSize.width, box.y),Scalar(255, 255, 255), FILLED);putText(frame, label, Point(box.x, box.y),FONT\_HERSHEY\_SIMPLEX, 0.5, Scalar(0, 0, 0));}

In this article, we explored how to use OpenCV's cv::dnn::NMSBoxes() function for object detection in software development. We covered the following key concepts:

  • Deep learning and neural networks
  • Convolutional Neural Networks (CNNs) for object detection
  • OpenCV and its DNN module
  • Non-Maximum Suppression (NMS) algorithm
  • Using cv::dnn::NMSBoxes() function for object detection

References

Using OpenCV's dnn::NMSBoxes() Function in Software Development (2024)

FAQs

What is the use of OpenCV DNN? ›

The OpenCV DNN module only supports deep learning inference on images and videos. It does not support fine-tuning and training. There are cases where using OpenCV's DNN module will give you faster inference results for the CPU. By using OpenCV's DNN module for inference the final code is a lot compact and simpler.

How to install OpenCV DNN? ›

First, make sure CUDA is installed. I installed it through Ubuntu's package manager, which is very convenient. For OpenCV's DNN module to use a GPU, we also need to install cuDNN. This will be important later if CMake can't find the CUDA location.

What is DNN and how it works? ›

(2020), “Deep Neural Network (DNN) is a powerful class of ML that stack several layers of neural network models together”. It consists of many layers, which connect neurons to solve a complex mathematical function. The input is processed by one layer, and it generates outputs for the next layer.

What is OpenCV mainly used for? ›

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products.

How OpenCV is used in deep learning? ›

It is deployed for the detection of items, faces, Diseases, lesions, Number plates, and even handwriting in various images and videos. With help of OpenCV basics in Deep Learning, we deploy vector space and execute mathematical operations on these features to identify visual patterns and their various features.

What is the OpenCV module used for? ›

Vision Capabilities of OpenCV

OpenCV is probably the most versatile computer vision tool used in a broad field of computer vision tasks. These tasks range from image recognition and 2D or 3D analysis to motion tracking, facial recognition, and more.

What is the use of deep neural network DNN in object detection with its applications? ›

It builds on carefully designed representations and kinematically inspired part decompositions of objects, expressed as a graphical model. Using dis- criminative learning of graphical models allows for building high-precision part-based models for variety of object classes.

What is DNN usage? ›

Deep neural networks are a type of artificial neural network with multiple hidden layers, which makes them more complex and resource-intensive compared to conventional neural networks. They are used for various applications and work best with GPU-based architectures for faster training times.

What is OpenCV in deep learning? ›

OpenCV stands for Open Source Computer Vision. It is a vast open-source library utilized in fields such as computer vision, machine learning, and image processing.

Why is OpenCV useful? ›

OpenCV is packed with algorithms that help with everything from recognizing objects to tracking the movement of objects and even creating 3D models. It's incredibly popular, with a huge community of over 47,000 users and more than 18 million downloads.

Top Articles
Perfect Vanilla Frosting - The Best Vanilla Buttercream Recipe!
Best Chicken Vol Au Vents Recipe | Simple. Tasty. Good.
The Advantages of Secure Single Sign-on on the BenQ Board
compose - Format data into multiple strings
El Patron Mexican Restaurant New Ellenton Menu
Meet Scores Online 2022
Dirty South Swag Review | BatDigest.com
Orange County's diverse vegan Mexican food movement gains momentum
Samsung 9C8
Double Helicath Clan Boss
Jocko Joint Warfare Review
8Kun Hypnosis
FREE Houses! All You Have to Do Is Move Them. - CIRCA Old Houses
Top Scorers Transfermarkt
Hill & Moin Top Workers Compensation Lawyer
'A Cure for Wellness', Explained
Wall Street Journal Currency Exchange Rates Historical
Practice Assist.conduit.optum
Asoiaf Spacebattles
Lynette Mettey Feet
New Haven Music Festival
Descargar AI Video Editor - Size Reducer para PC - LDPlayer
Praxis für Psychotherapie und Coaching Rhein-Neckar
Wolf Of Wallstreet 123 Movies
So sehen die 130 neuen Doppelstockzüge fürs Land aus
Spaghetti Models | Cyclocane
Creigs List Maine
Kentucky Lottery Scratch Offs Remaining
Post Crescent Obituary
Gsmst Graduation 2023
Drug Stores Open 24Hrs Near Me
Espn College Basketball Scores
phoenix health/wellness services - craigslist
Find Words Containing Specific Letters | WordFinder®
Hca Florida Middleburg Emergency Reviews
Fortnite Fap Hero
Spiral Roll Unblocked Games Premium
How To Pause Tamagotchi Gen 2
Ret Paladin Phase 2 Bis Wotlk
How to Learn Brazilian Jiu‐Jitsu: 16 Tips for Beginners
They Cloned Tyrone Showtimes Near Showbiz Cinemas - Kingwood
Small Party Hall Near Me
Lily Spa Roanoke Rapids Reviews
Sim7 Bus Time
What Is TAA Trade Agreements Act Compliance Trade Agreement Act Certification
Shiny Flowers Belinda
Indian River County FL.
Katopunk Pegging
Tillamook Headlight Herald Obituaries
Understanding DeFi The Roles, Tools, Risks, and Rewards of -- Alexandra Damsker -- 2024 -- O'Reilly Media -- 9781098120764 -- 79accdb00af9d0f41d97f44fa7970ff1 -- Annas Archive - Biblioteconomia
Mcknet Workday
How to Screenshot on Cash App: A Complete Guide
Latest Posts
Article information

Author: Dan Stracke

Last Updated:

Views: 5674

Rating: 4.2 / 5 (63 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.