ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Opencv 윤곽 검출
    Search: Python Python 2021. 4. 12. 19:14

    Opencv 윤곽 검출

    findContours

     

     

    import cv2
    import matplotlib.pyplot as plt
    import numpy as np
    #import imutils
    
    # 사진을 불러와서, 적정 크기로 줄인다.
    img = cv2.imread("Rsc/Flower_CampanulaLactifiora.jpg")
    img = cv2.resize(img, (300,169))
    plt.subplot(2,2,1)
    plt.title("origin")
    plt.imshow(img)
    
    # 큰 것들을 구분하기 좋게 처리한다.
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (7,7), 0)
    plt.subplot(2,2,2)
    plt.title("abs")
    plt.imshow(gray)
    
    # 이진화
    im2 = cv2.threshold(gray, 140, 240, cv2.THRESH_BINARY)[1]
    plt.subplot(2,2,3)
    plt.title("bin")
    plt.imshow(im2, cmap="gray")
    
    # 윤곽 검출
    cv2VersionMajor = cv2.__version__.split(".")[0]
    if int(cv2VersionMajor) >= 4: contours, hierarchy = cv2.findContours(im2, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    else: img_temp, contours, hierarchy = cv2.findContours(im2, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    
    for pt in contours:
         for p in pt:
             x,y,w,h = cv2.boundingRect(pt)
             cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0), 2)
    
    plt.subplot(2,2,4)
    plt.title("rst")
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    
    plt.show()
    

     

     

    'Python' 카테고리의 다른 글

    Class (클래스)  (0) 2021.11.11
    Flask - Android환경에 Java와 연동  (0) 2021.10.25
    Python popen  (0) 2021.06.23
    Python Sandbox  (0) 2021.06.23
    Opencv boundingRect  (0) 2021.04.12
    Python ScikitLearn 손글씨 숫자 인식 2  (0) 2021.04.12
    Python Random 난수 생성기  (0) 2021.04.12
    Python ScikitLearn 손글씨 숫자 인식 1  (0) 2021.04.12

    댓글