티스토리 뷰
1. line_drive패키지 생성
catkin_create_pkg line_drive rospy tf geometry_msgs rviz xacro
2. 소스파일
line_find.py
#!/usr/bin/env python
import cv2, time
import numpy as np
cap = cv2.VideoCapture('track1.avi')
threshold_60 = 60
width_640 = 640
scan_width_200, scan_height_20 = 200, 20
lmid_200, rmid_440 = scan_width_200, width_640 - scan_width_200
area_width_20, area_height_10 = 20, 10
vertical_430 = 430
row_begin_5 = (scan_height_20 - area_height_10) // 2
row_end_15 = row_begin_5 + area_height_10
pixel_threshold_160 = 0.8 * area_width_20 * area_height_10
while True:
ret, frame = cap.read()
if not ret:
break
if cv2.waitKey(1) & 0xFF == 27:
break
roi = frame[vertical_430 : vertical_430 + scan_height_20, :]
frame = cv2.rectangle(frame, (0, vertical_430), (width_640 -1, vertical_430 + scan_height_20), (255, 0, 0), 3)
hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
lbound = np.array([0, 0, threshold_60], dtype=np.uint8)
ubound = np.array([131, 255, 255], dtype=np.uint8)
bin = cv2.inRange(hsv, lbound, ubound)
view = cv2.cvtColor(bin, cv2.COLOR_GRAY2GBR)
left, right = -1, -1
for l in (area_width_20, lmid_200):
area = bin[row_begin_5:row_end_15, 1-area_width_20:1]
if cv2.countNonZero(area) > pixel_threshold_160:
left = l
break
for r in (width_640 - area_width_20, rmid_440, -1):
area = bin[row_begin_5:row_end_15, r:r+area_width_20]
if cv2.countNonZero(area) > pixel_threshold_160:
right = r
break
if left != -1:
lsquare = cv2.rectangle(view, (left - area_width_20, row_begin_5), (left, row_end_15), (0, 255, 0), 3)
else:
print("Lost left line")
if right != -1:
rsquare = cv2.rectangle(view, (right, row_begin_5), (right+area_width_20, row_end_15), (0, 255, 0), 3)
else:
print("Lost right line")
cv2.imshow("origin", frame)
cv2.imshow("view", view)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lbound = np.array([0, 0, threshold_60], dtype=np.uint8)
ubound = np.array([131, 255, 255], dtype=np.uint8)
hsv = cv2.inRange(hsv, lbound, ubound)
cv2.imshow("hsv", hsv)
cap.release()
cv2.destroyAllWindows()
3. 실행
python line_find.py
'OpenCV' 카테고리의 다른 글
OpenCV - lidar_drive패키지 line_follow.py (0) | 2021.01.21 |
---|---|
Ubuntu16.04 ROS Kinetic OpenCV - rosbag 재생해서 영상처리 (0) | 2021.01.19 |
OpenCV python - 사각형그리기, 한 점 찾아내기, ROI, HSV, 동영상 재생 (0) | 2021.01.19 |
OpenCV - Canny Edge 함수(C++) (0) | 2021.01.19 |
OpenCV - Mat 메모리 공유 vs 복사 (0) | 2021.01.18 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- VMware
- 프로그래머스
- VirtualBox
- 백준알고리즘
- 초음파센서
- ROS
- python3
- subscriber
- C++
- 8자주행
- set backspace
- Ubuntu16.04
- 윈도우 복구
- 아두이노 IDE
- filesystem
- roslaunch
- 원격 통신
- Mount
- 코드리뷰
- Publisher
- 리눅스
- sensehat
- 윈도우
- HC-SR04
- 포트인식문제
- vue/cli
- 우분투
- Ubuntu20.04
- umount
- Python
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함