Pythonを使って黄色い線の輪郭検出
環境
・Ubuntu18.04
・Python2.7
・使用する画像
コード
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('yellow_line.jpeg', 1)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, img_binary = cv2.threshold(img_gray,
140, 255,
cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(img_binary,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
contours = list(filter(lambda x: cv2.contourArea(x) > 100, contours))
img_contour = cv2.drawContours(img, contours, -1, (0, 255, 0), 5)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.imshow(cv2.cvtColor(img_contour, cv2.COLOR_BGR2RGB))
ax1.axis('off')
plt.show()
plt.close()
結果
綺麗に黄色の線のみ囲うことができた。
参考サイト
https://watlab-blog.com/2020/03/19/find-contours/
https://pystyle.info/opencv-find-contours/
一言
コードの解説等は気が向いたら行っていきます。