xs = []
ys = []
files = glob.glob ("./ sisaku-tra/*")
for i, f in enumerate (files):
    img = cv2.imread (f)
    gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY)
    ret, binary = cv2.threshold (gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    kernel = cv2.getStructuringElement (cv2.MORPH_ELLIPSE, (5, 5))
    binary = cv2.dilate (binary, kernel)
    contours, hierarchy = cv2.findContours (binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    target_contour = max (contours, key = lambda x: cv2.contourArea (x))
    area = cv2.contourArea (target_contour)
    retval = cv2.arcLength (target_contour, True)
    ratio = area/retval
    xs.append (ratio)
    hsv = cv2.cvtColor (img, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split (hsv)
    v = v [10<v]
    value = v [v<225] .mean ()
    ys.append (value)
plt.scatter (xs, ys, s = 10)
for i, (x, y) in enumerate (zip (xs, ys)):
    plt.annotate (str (i), (x, y), size = 5)
plt.savefig ('plt1.jpg')

The above code gives a scatter plot like below

The order and point numbers stored in the sisaku-tra file are different.
Why is this?

Thank you for everyone.

  • Answer # 1

    The order of the files obtained by

    glob.globis clear and nothing is guaranteed.


    Whether or not the results are sorted depends on the file system.


    glob --- Unix-style pathname pattern expansion — Python 3.8.0 documentation

    You say it will behave differently depending on the environment, it will return in the proper order, but that's it.

    I can't figure out the situation, for example"There is no guarantee that you will get a list of files in that order even if they are neatly arranged in Explorer". I think there is a misunderstanding around that.

Related articles