그레이처리, 가우시안 블러, 캐니 엣지, 허프변환 , ROI 를 사용해 직선과 곡선을 찾아낸다.
#!/usr/bin/env python
# -*- coding: cp949 -*-
import rospy
import cv2
import numpy as np
from cv_bridge import CvBridge, CvBridgeError
from sensor_msgs.msg import Image
from sensor_msgs.msg import CompressedImage
class Display_image():
def __init__(self):
self.selecting_sub_image = "raw"
if self.selecting_sub_image == "compressed":
self._sub = rospy.Subscriber('/usb_cam/image_raw/compressed', CompressedImage, self.callback, queue_size=1)
else:
self._sub = rospy.Subscriber('/usb_cam/image_raw', Image, self.callback, queue_size=1)
self.bridge = CvBridge()
def callback(self, image_msg):
if self.selecting_sub_image == "compressed":
#converting compressed image to opencv image
np_arr = np.fromstring(image_msg.data, np.uint8)
cv_img = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
elif self.selecting_sub_image == "raw":
cv_img = self.bridge.imgmsg_to_cv2(image_msg, "bgr8")
def gaussian_blur(img, kernel_size):
return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
def region_of_interest(img, vertices):
mask = np.zeros_like(cv_img)
cv2.fillPoly(mask,vertices, 255)
ROI_image = cv2.bitwise_and(img, mask)
return ROI_image
def draw_lines(img, lines, color=[0, 0, 255], thickness=2):
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(img, (x1, y1), (x2, y2), color, thickness)
def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap):
lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len, maxLineGap=max_line_gap)
line_img = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
draw_lines(line_img, lines)
return line_img
def weighted_img(img, initial_img, a=1, b=1., c=0.):
return cv2.addWeighted(initial_img, a, img, b, c)
height, width = cv_img.shape[:2]
r= 800.0/cv_img.shape[1]
dim = (800,int(cv_img.shape[0]*r))
#cv_img = cv2.resize(cv_img, dim, interpolation = cv2.INTER_AREA)
px = cv_img.shape[0]
px1 = cv_img.shape[1]
#print(px,px1)
#mask = np.zeros_like(cv_img)
#px = mask.shape[0]
#px1 = mask.shape[1]
#print(px,px1)
vertices = np.array([[(50,height),(width/2-45, height/2+60), (width/2+45, height/2+60), (width-50,height)]], dtype=np.int32)
#vertices = np.array([[10,500], [10,300], [300,200], [500,200], [800,300], [800,500],],np.int32)
ROI_img = region_of_interest(cv_img, [vertices])
cv_gray = cv2.cvtColor(ROI_img, cv2.COLOR_RGB2GRAY)
cv_blur = gaussian_blur(cv_gray, 3)
cv_edge = cv2.Canny(cv_blur, 10, 80)
#vertices = np.array([[(50,height),(width/2-45, height/2+60), (width/2+45, height/2+60), (width-50,height)]], dtype=np.int32)
hough_img = hough_lines(cv_edge, 1, 1 * np.pi/180, 30, 10, 20)
result = weighted_img(hough_img, cv_img)
cv2.imshow('cv_image', result), cv2.waitKey(1)
def main(self):
rospy.spin()
if __name__ == '__main__':
rospy.init_node('Display_image')
node = Display_image()
node.main()
'자율주행 > ROS기반 자율주행모형차 개발' 카테고리의 다른 글
(10) 카메라 세팅 및 Opencv 설치 (1) | 2019.03.05 |
---|---|
(9) 아두이노 펌웨어 (0) | 2019.03.05 |
opencv roi (0) | 2018.10.30 |
(8) motor_control 노드 작성 (0) | 2018.07.22 |
(7) 키보드 조작노드 작성 (keyboard teleoperation) (0) | 2018.07.16 |