Home>

I want to plot data,

import numpy as np
import matplotlib.pyplot as plt
import glob
files = glob.glob ("./ pu1/*")
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
    hsv = cv2.cvtColor (img, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split (hsv)
    v = v [10<v]
    value = v [v<225] .mean ()
Code


I want to create a scatter plot with x = ratio, y = value in this code.
I don't want to draw a broken line, but

Thank you for everyone.

  • Answer # 1

    import numpy as np
    import matplotlib.pyplot as plt
    import glob
    xs = []
    ys = []
    files = glob.glob ("./ pu1/*")
    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