Programming/Python
[Python3] 특정 색상의 선을 따라가기 위한 각도 구하기
Talking1258
2019. 10. 23. 16:17
간단한 자율주행 자동차를 만들기 위해 제작하였습니다.
목적: 어떤각도로 선을 따라가야 하는지 구하기 위함
제작년월: 2019년 9월 ~
공개일: 2019년 10월 23일
소프트웨어: Python 3.7.4
필요 모듈: Pillow, numpy
import PIL.Image as pilimg
import numpy as np
def scan(img1):
one = pilimg.open(img1)
pix = np.array(one)
a = []
for x in range(0, 854, 5):
for y in range(0, 480, 5):
if int(pix[y][x][0]) > 190 and int(pix[y][x][1]) < 130 and int(pix[y][x][2]) < 130 and not y == 480 and not x == 427:
a.append((y - 480) / (x - 427))
incli = (-1) * sum(a) / len(a)
if incli < 0:
result = 180 + abs(180 - (np.arctan2(incli, -1) * 180 / np.pi) * (-1))
else:
result = np.arctan2(incli, 1) * 180 / np.pi
return abs(round(180 - result))
print(scan('a.bmp'))