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
-
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_contours
seems to be writing the code that gets the coordinates, so I can't understand what I don't know.
Related articles
- python 3x - how to display webcam started with python opencv in html
- python - i got an error in opencv
- python opencv
- python - opencv videocapture cannot be used
- image composition using python opencv
- python - data and timing used for feature selection
- python - opencv color range
- python opencv face recognition multiple person image
- python - opencv circle center color
- python - [opencv] i can't get the frame of the video with read (), and the playback of the video stops halfway
- python - [opencv] i want to display the area of each object at the position adjacent to the object
- python - detect whole body and upper body with opencv
- python - machine learning feature extraction of time series data
- i want to run opencv on python
- python - i don't see the error in the face cropping program with opencv
- python opencv recording time
- python opencv color index
- python contour detection¢roid calculation
- python - i want to create a contour image
- python - image cannot be read by opencv
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.
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)