Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Contour Discovery by C++ OpenCV

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how C++ OpenCV implements contour discovery. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Contour discovery (find contour)

An outline is a list of points that represent a curve in an image in some way. This representation can vary according to the actual situation. There are many ways to represent a curve.

Contour discovery is a method to find object contours based on image edge extraction. Therefore, the threshold selection of edge extraction will affect the result of the final contour discovery.

In OpenCV, the outline is represented by a STL-style vector template object, where each element in vector encodes the position information of the next point on the curve.

Introduction to related API

FindContours (found profile)

DrawContours (draw outline)

FindContours (

InputOutputArray image, / / input 8-bit single-channel "binary" image

OutputArrayOfArrays contours, / / all found contour images, including the vector of points's vectors

OutputArray hierarchy, / / (optional) topology information, the contour discovery method is based on the topology of the image.

Int mode, / / Contour search mode

Int method, / / approximate method

Point offset = Point () / / (optional) offset of all points, no displacement by default

);

How is the parameter topology information extracted from int mode? there are four ways to extract it.

Cv::RETR_EXTERNAL: indicates that only the outermost contours are extracted

Cv::RETR_LIST: indicates that all contours are extracted and put into the list

Cv::RETR_CCOMP: indicates that all contours are extracted and organized into a two-tier structure, in which the top outline is the outer contour and the second outline is the outline of the "hole"

Cv::RETR_TREE: represents a complete hierarchy in which all contours are extracted and organized into nesting contours.

The int method parameter is the way to see how the outline is rendered. There are three options:

Cv::CHAIN_APPROX_NONE: converts the coding of all points in the outline to points

Cv::CHAIN_APPROX_SIMPLE: compress horizontal, vertical, and diagonal line segments, leaving only their endpoints

Cv::CHAIN_APPROX_TC89_L1 or cv::CHAIN_APPROX_TC89_KCOS: a style in applying Teh-Chin chain approximation algorithm

DrawContours (

InputOutputArray image, / / input image for drawing

InputArrayOfArrays contours, / / all found contour images, vector of point vectors

Int contourIdx, / / Index of contours to be drawn (- 1 means "all")

Const Scalar& color, / / Color of the outline

Int thickness = 1, / / width of the contour line

Int lineType = 8, / / neighborhood pattern of contours ('4' neighborhood or'8' neighborhood)

InputArray hierarchy = noArray (), / / optional (obtained from findContours)

Int maxLevel = INT_MAX, / / maximum drop in profile

Point offset = Point () / / (optional) offset of all points

)

Code demonstration

Create a new project opencv-0023, configure properties (VS2017 configure OpenCV common properties), and then write # include and main methods in the source file

First, we define thresholds and methods.

Then add two trackbar to the main method

Drawocuntours method

Running effect

Thank you for reading! This is the end of the article on "how C++ OpenCV realizes contour discovery". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report