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 use Python+OpenCV to realize simple Image Edge Contour Detection

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use Python+OpenCV to achieve simple image edge contour detection", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to use Python+OpenCV to achieve simple image edge contour detection"!

Function basis and Tripartite Library

The third-party library used in this article is Opencv4.3

Import third-party libraries

Import cv2 as cv

Because the Opencv display image is troublesome and inconsistent with matlab or matplob, considering that the foundation is weak and may not understand the image display mechanism of this library, I first define an img_show function to display the image more conveniently.

Def img_show (pic,name):''this function img_show () is used to call the related function of OpenCV to display the image name is the name of the display image window (name is a string) pic is the displayed image (pic is the picture entered by opencv imread)' 'cv.imshow (pic,name) cv.waitkey (0) cv.destroyAllWindows () cv.threshold (pic,thresh,maxvalue,model)

This function is used for the operation of different thresholds in a single channel of an image, and is generally used to binarize the image, which will be helpful to the gradient calculation of edge detection.

Pic is the image to be processed, and because it is processed in a single channel, pic is generally converted to grayscale image.

Thresh is the operation threshold, and those higher than this threshold will be unified into 0 or maxvalue according to different model.

Model is the operation method. Generally, only cv.THRESH_BINARY_INV and cv.THRESH_BINARY are needed.

... THRESH_BINARY_INV sets the value greater than thresh to 0

... THRESH_BINARY sets the value greater than thresh to 255,

The image has two return values, the first return value is the threshold, the threshold value, and the second is the matrix of the binary image.

Cv.findContours (picture to be processed, model (extraction mode), method (extraction method))

This function is used to extract the contour points of pic. When pic is a binary image, the function extraction will be more accurate.

Model generally uses cv.RETR_EXTERNAL and cv.RETR_TREE for extraction mode.

... Extrnal is extracted in the way of outer contour.

... Tree extracts all contours of the inner and outer layers of the image.

Method is the extraction method, including cv.CHAIN_APPROX_NONE and cv.CHAIN_APPROX_SIMPLE.

... NONE is to connect the extracted contours by lines.

... SIMPLE compresses lines and beveled edges, marking only the vertices of the outline

There are two return values for this function, one is the edge point (in the form of a list), and the other is hierarchical information

Contours,hierarchy = cv.findContours (pic,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_NONE)

What this code means is

Based on pic this image

Use a mode that describes the outer outline

The contour is extracted by linking each point.

Finally, we get the outline list set contours and the hierarchical relation hierarchy.

Note: there are many sets of contours in contours, for example, contours [0]\ contours [1]\ contours [2] is three contours, maybe only one is the target contour, and the others are noise contours.

Cv.drawContours (canvas, outline collection, index, color, weight)

This function is used to draw a specified outline (index judgment) or all contours (- 1) on a specified canvas with lines of specified color thickness.

Canvas: draw the outline on the canvas, usually instead of going to the copy picture, otherwise it will pollute the original picture.

Contour set: the contours mentioned above, which contains all the contours of the target and noise contours

Index: a certain outline in the selected outline set, if you know the number of the target outline, you can write directly, if you don't know, write-1, you can draw all the contours.

Color: in tuple form, (255j0pc0) is red, and so on

Thickness: the thickness of the outline is 1 ~ any integer, too large to overwrite the original image

Cv.boundingRect (Image)

This function is used to find the matrix points of the detected function.

Image: the detected image is usually the outline of the incoming target, that is, contours [index], and index is the target number.

This function returns four values: xrecoery yrecoverwrecoh

Where x _ ray y refers to the minimum value on the x-axis and y-axis of the image (when the upper left corner is the origin), and the width and height of the image are all covered by the other references.

Code implementation # Import opencvimport cv2 as cv# definition opencv image display function def img_show (pic,name): cv.imshow (pic,name) cv.waitKey (0) cv.destroyAllWindows () # Color mode read picture eagle_o = cv.imread ('eagle.png',1) # picture converted to grayscale image eagle = cv.cvtColor (eagle_o,cv.COLOR_BGR2GRAY) # convert image to binary ret,eagle_2v = cv.threshold (eagle,125255 Cv.THRESH_BINARY_INV) # ret is the threshold Eagl_2v is a binary graph # based on the pattern of outer contours used in binary images, contours are extracted by the method of connecting contours at all points contours,hierarchy = cv.findContours (eagle_2v,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_NONE) # draw all contours on copy img = cv.drawContours (eagle_o.copy (), contours,-1, (255Power25), 5) # to obtain the minimum matrix of the target image Here 29 is the outline of the target x _ cv.rectangle (contours [29]) # draw the target box img = cv.rectangle (eagle_o, (x _ reagy), (x _ goal',img), (255pr _ y _ 0), 7) img_show ('goal',img) to achieve the effect

At this point, I believe you have a deeper understanding of "how to use Python+OpenCV to achieve simple image edge contour detection". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report