자율주행_차선검출_numpy이용

#자율주행 #차선검출 #Sobel #Laplacian #OpenCV #파이톤 #Python

사용한 사진은 pixabay에서 다운로드하여 테스트를 하였습니다.

소스 코드는 opencV 강의를 참고하여 인터넷 검색을 하면서 작성하였는데 내용에는 일부 오류가 있을 수 있습니다.

import cv2

import numpy as np

def mask_filter(image, mask):

rows,cols=image.shape[:2]

dst=np.zeros(rows,cols),np.float32)

x_cen,y_cen=mask.shape[1]//2,mask.shape[0]//2

for i in range (y_cen,rows-y_cen):

for jin range (x_cen, cols-x_cen):

sum=0.0

for u in range (mask.shape[0]):

for vin range (mask.shape[1]):

y,x=i+u-y_cen,j+v-x_cen

sum=sum+image[y,x]*mask[u,v]

dst[i,j]=sum

return dst

#image0=cv2.imread(“roadtest.jpg”)

image0=cv2.imread(“roadtest2.jpg”)

gray=cv2.cvtColor (image0,cv2.COLOR_BGR2GRAY)

image=cv2. Gaussian Blur (gray, (3,3), 0)

Laplacian=[0,1,0,

1,-4,1,

0,1,0]

sobel_y=[-1,-2,-1,

0, 0, 0,

1, 2, 1]

sobel_x=[1,0,-1,

2, 0,-2,

1, 0,-1]

mask1=np.array(Laplacian,np.float32).reshape(3,3)

mask2=np.array(sobel_x,np.float32).reshape(3,3)

mask3=np.array(sobel_y,np.float32).reshape(3,3)

Laplacian=mask_filter (image,mask1)

sobel_x=mask_filter(image,mask2)

sobel_y=mask_filter(image,mask3)

cv2.imshow(“image”, image)

Laplacian_img=cv2.convertScaleAbs(Laplacian)

cv2.imshow(“Laplacian”, Laplacian_img)

cv2.imwrite(“Laplacian.jpg”, Laplacian_img)

sobel_x_img=cv2.convertScaleAbs(sobel_x)

cv2.imshow(“sobel_x”, sobel_x_img)

cv2.imwrite(“soble_x.jpg”, sobel_x_img)

sobel_y_img=cv2.convertScaleAbs(sobel_y)

cv2.imshow(“sobel_y”,sobel_y_img)

cv2.imwrite(“sobel_y.jpg”, sobel_y_img)

cv2.waitKey( )

Laplacian

Sobely

Sobel x

새로운 영상에 대해서도 테스트를 해보겠습니다.

sobely

sobel x

직접 numpy코드로 처리하므로 결과를 확인하기까지 시간이 오래 걸린다.

다음 시간에는 open CV 함수를 사용하여 동작 시간을 단축해 보자.

수학적인 이해는 아래 사이트에서 컨볼루션에 대한 이해와 sobel 필터에 대한 이해를 더했다.

An Implementation of Sobel Edge Detection by Sean Sodha Introduction Edge Detection is when we use matrix math to calculate areas of different intensities of animage. Areas where are extreme differences in the pixel usually indicate www.projectrhea.org

error: Content is protected !!