In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use OpenCV to sort object contours based on python. I hope you will get something after reading this article. Let's discuss it together.
1 introduction
In the process of image processing, we often encounter some operations related to the outline of the object, such as finding the perimeter and area of the outline of the object. We can easily get the outline of each target by directly using the findContours function of Opencv, but after visualization, the order is out of order, as shown on the left side of the following figure:
This section intends to sort the contours of objects, which can be sorted from top to bottom or from left to right to achieve the visual results on the right side of the image above.
2 Chestnut 2.1 read the image
First of all, let's read the image and get its edge detection image, the code is as follows:
Image = cv2.imread (args ['image']) accumEdged = np.zeros (image.shape [: 2], dtype='uint8') for chan in cv2.split (image): chan = cv2.medianBlur (chan, 11) edged = cv2.Canny (chan, 50,200) accumEdged = cv2.bitwise_or (accumEdged, edged) cv2.imshow (' edge map', accumEdged)
The running results are as follows:
The left side is the original image, and the right side is the edge detection map.
2.2 get Contour
The API for finding image contours in opencv-python is: findContours function, which receives binary images as input and can output outer contours, inner and outer contours of objects, and so on.
The code is as follows:
Cnts = cv2.findContours (accumEdged.copy (), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = grab_contours (cnts) cnts = sorted (cnts, key=cv2.contourArea, reverse=True) [: 5] orig = image.copy () # unsortedfor (I, c) in enumerate (cnts): orig = draw_contour (orig, c, I) cv2.imshow ('Unsorted', orig) cv2.imwrite (". / Unsorted.jpg", orig)
The running results are as follows:
It is important to note that in the OpenCV2.X version, the function findContours returns two values
The function is declared as follows:
Contours, hierarchy = cv2.findContours (binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
However, in versions above OpenCV3, the function is declared in the following form:
Image, contours, hierarchy = cv2.findContours (binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
So in order to adapt to the two modes, we implement the function grab_contours to select the corresponding subscript position of the return outline according to different versions.
The code is as follows:
Def grab_contours (cnts): # if the length the contours tuple returned by cv2.findContours # is'2' then we are using either OpenCV v2.4, v4-beta, or # v4-official if len (cnts) = = 2: cnts = cnts [0] # if the length of the contours tuple is'3' then we are using # either OpenCV v3, v4-pre, or v4-alpha elif len (cnts) = = 3: cnts = cnts [1] return cnts2.3 profile sort
Through the above steps, we get the contours of all the objects in the image, and then we define the function sort_contours to sort the contours, which accepts method parameters to sort the contours in different order, such as from left to right or from right to left.
The code is as follows:
Def sort_contours (cnts Method='left-to-right'): # initialize the reverse flag and sort index reverse = False I = 0 # handle if sort in reverse if method= = 'right-to-left' or method= =' bottom-to-top': reverse = True # handle if sort against y rather than x of the bounding box if method= = 'bottom-to-top' or method= =' top-to-bottom': I = 1 boundingBoxes = [cv2.boundingRect (c) for c in cnts] (cnts BoundingBoxes) = zip (* sorted (zip (cnts, boundingBoxes), key=lambda b: B [1] [I], reverse=reverse)) return (cnts, boundingBoxes)
The core idea of the above code is to find out the outer rectangle of each outline, and then sort the outline by sorting the outer frame according to x or y coordinates.
The calling code is as follows:
# sorted (cnts, boundingboxes) = sort_contours (cnts, method=args ['method']) for (I, c) in enumerate (cnts): image = draw_contour (image, c, I) cv2.imshow (' Sorted', image) cv2.waitKey (0)
The running results are as follows:
2.4 other results
Using the above code, we can also sort from left to right, as follows:
After reading this article, I believe you have a certain understanding of "how to sort object contours based on python using OpenCV". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.