0

Regarding the imutils library related to OpenCV, I have the following questions:

  1. What is the function of contours.sort_contours()?

  2. (cnts, _) = contours.sort_contours(cnts) In the above statement, what is the meaning of (cnts, _), especially '_'

Note: cnts is a variable containing the contours of the recognized image My English is not very good, sorry! Thank you all for your answers and help!

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
mtttr
  • 3
  • 2

1 Answers1

0

The imutils library is a convenience library written to make basic image processing tasks easier (https://github.com/PyImageSearch/imutils).

One such task is sorting and labelling contours in images. Based on documentation provided here, there is a separate contours.py file hosting sort_contours() and label_contour functions. (You need to remember that these functions can be used only after finding contours using OpenCV)

Now to answer your questions:

1. What is the function of contours.sort_contours() ?

  • I will refer you to a sample code here. In line 35, the author accesses the function sort_contour() from the file contours.py in this way: contours.sort_contours().
  • The function accepts two parameters: the contours and the sorting arrangements ("left-to-right", "right-to-left", "top-to-bottom", "bottom-to-top")
  • The function returns a list of the sorted contours and its corresponding bounding boxes

2. Understanding (cnts, _) = contours.sort_contours(cnts)

  • As stated previously, the function accepts contours as input cnts.
  • The same variable cnts is used to store the sorted contours.
  • Since the function returns dual output (sorted contours and its bounding boxes), the second variable is assigned _, which stores the bounding boxes. _ is mostly used a variable when it will not be needed for further processing.
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • Can I understand it this way: The contours.sort_contours() function returns: sorted contours and a list of values corresponding to their bounding boxes. These two arguments are passed to (cnts, _), where cnts receives the contour and _ receives the corresponding list value. In addition, can the cnts and _ here be similarly understood as the value and key of the dictionary type Thank you again for your answer, thank you very much, and my English is not very good, thank you for reading and answering! ! ! – mtttr Apr 27 '22 at 16:27
  • @mtttr Your understanding is correct. Also the relation between `cnts` and `_` can be seen as a dictionary type. Because the first bounding box coordinates in `_` will correspond to the first contour in `cnts` – Jeru Luke Apr 27 '22 at 16:43
  • Thank you very much for your answer, much appreciated! ! ! In addition, in the routine you showed me, what does this paragraph mean and how do I understand it? ** for (i, c) in enumerate(cnts): orig = contours.label_contour(orig, c, i, color=(240, 0, 159))** – mtttr Apr 27 '22 at 17:02
  • Thanks again for your answer,@Jeru Luke, sorry my computer is about to run out of power, I may not be able to reply to your comment today, glad to ask you again tomorrow. Good night and wish you a happy life! – mtttr Apr 27 '22 at 17:11
  • @mtttr `for (i, c) in enumerate(cnts): orig = contours.label_contour(orig, c, i, color=(240, 0, 159))`, this code refers to drawing identified contours on the original image `orig`. The `i` refers to the numeric label assigned to that contour and it is displayed in the `orig` image as well – Jeru Luke Apr 27 '22 at 17:49
  • 1
    Thank you very much for answering my question, @Jeru Luke.I will understand your answer well, thank you very much! – mtttr Apr 28 '22 at 02:49