1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonを使って黄色い線の輪郭検出

Last updated at Posted at 2021-07-08

Pythonを使って黄色い線の輪郭検出

環境

・Ubuntu18.04
・Python2.7
・使用する画像

yellow_line.jpeg

コード

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()

結果

綺麗に黄色の線のみ囲うことができた。

Figure_1.png

参考サイト

https://watlab-blog.com/2020/03/19/find-contours/
https://pystyle.info/opencv-find-contours/

一言

コードの解説等は気が向いたら行っていきます。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?