NLP_자연언어처리, Colab

[Colab]Canny Edge Detector_hough transform_Edge 탐색 코드

vhxpffltm 2020. 4. 7. 21:42
반응형
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 
import cv2 
import numpy as np
from google.colab.patches import cv2_imshow
from google.colab import drive
import sys
#colab에서 cv2 이미지를 위한 라인
 
drive.mount('/content/gdrive')
 
sys.path.insert(0'/content/gdrive/My Drive/Colab Notebooks')
import edge
#로컬 파일을 구글 드라이브를 이용하여 colab에 import, 'ls [경로]' 로 파일확인가능
 
def grayscale(img): 
    return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
 
def canny(img, low_threshold, high_threshold): 
 
 
def region_of_interest(img, vertices, color3=(255,255,255), color1=255): 
 
    mask = np.zeros_like(img) 
    
    if len(img.shape) > 2
        color = color3
    else
        color = color1
    cv2.fillPoly(mask, vertices, color)
    #vertices에 의한 ROI 부분을 color로 채움 
    ROI_image = cv2.bitwise_and(img, mask)
    # 이미지와 color로 채워진 ROI를 합침
    return ROI_image
 
def draw_lines(img, lines, color=[00255], thickness=2): 
    for line in lines:
        for x1,y1,x2,y2 in line:
    #선을 그림
 
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, α=1, β=1., λ=0.): 
    return cv2.addWeighted(initial_img, α, img, β, λ)
    #두 이미지 오버랩
 
image = cv2.imread('road.jpg'
height, width = image.shape[:2
 
gray_img = grayscale(image) 
        
canny_img = canny(gray_img, 70210
 
vertices = np.array([[(50,height),(width/2-45, height/2+60), (width/2+45, height/2+60), (width-50,height)]], dtype=np.int32)
ROI_img = region_of_interest(canny_img, vertices) 
 
hough_img = hough_lines(ROI_img, 11 * np.pi/1801510500
#일직선상의 직선을 위해 마지막 인자값(선분 사이의 거리) 를 크게 줌 
 
result = weighted_img(hough_img, image) 
cv2_imshow(result)
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

 

 

 

자세한 내용은 출처에 있다. 

마지막의 hough_lines의 값들을 변화시키면서 결과를 확인해 보면 좋다.

 

 

 

Referecne

https://m.blog.naver.com/windowsub0406/220894645729

반응형