Home>

I want to know the coordinates of feature points after contour extraction.

import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
img = cv2.imread ("./ pu1/1124_2_0.jpg")
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)
def draw_contours (ax, img, contours):
    ax.imshow (img)
    ax.set_axis_off ()
    for i, cnt in enumerate (contours):
        if cv2.contourArea (cnt)<1900:
            continue
        else:
            cnt = cnt.squeeze (axis = 1)
            ax.add_patch (Polygon (cnt, color = "b", fill = None, lw = 2))
            ax.plot (cnt [:, 0], cnt [:, 1], "ro", mew = 0, ms = 4)
            ax.text (cnt [0] [0], cnt [0] [1], i, color = "orange", size = "20")
            print (contours)

fig, ax = plt.subplots (figsize = (8, 8))
draw_contours (ax, img, contours)
plt.savefig ('./ rinnkaku/.jpg')
Code


With this code, the following image can be obtained from the original image.

The coordinates of the outline of the small area are also extracted.
I want to know the coordinate group only at the largest contour (here 2).

Thank you for everyone.

python version is 3.7.3,
The version of opencv is 4.1.1.

  • Answer # 1

      

    I want to know the coordinate group only at the largest contour (here 2).

    cv2.contourArea () can be used to obtain the contour area, so specify this function as the key argument of max () to obtain the contour with the largest area.

    target_contour = max (contours, key = lambda x: cv2.contourArea (x))

    The outline obtained by this is a numpy array of (number of points, 1, 2), and this is the coordinates that make up the points of the outline. (The upper left corner of the image is the origin (0, 0) and the unit is the pixel image coordinate system)

    import cv2
    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.patches import Polygon
    def draw_contours (ax, img, contours):
        ax.imshow (img)
        ax.set_axis_off ()
        for i, cnt in enumerate (contours):
            cnt = cnt.squeeze (axis = 1)
            ax.add_patch (Polygon (cnt, color = "b", fill = None, lw = 2))
            ax.plot (cnt [:, 0], cnt [:, 1], "ro", mew = 0, ms = 4)
            ax.text (cnt [0] [0], cnt [0] [1], i, color = "orange", size = "20")
    img = cv2.imread ("sample.jpg")
    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
    )
    # Extract the contour with the largest area.
    target_contour = max (contours, key = lambda x: cv2.contourArea (x))
    fig, ax = plt.subplots (figsize = (8, 8))
    draw_contours (ax, img, [target_contour])
    # Save the points that make up the contour to CSV.
    np.savetxt (
        "target_contour.csv", target_contour.squeeze (axis = 1), delimiter = ",", header = "X, Y"
    )

  • Answer # 2

    1. I don't want to extract the outline of a small area (in this case, 0-7 outline).

    Check the area using contourArea and try again.

    2.I want to know the coordinates of the red dot.

    draw_contoursseems to be writing the code that gets the coordinates, so I can't understand what I don't know.