728x90
# Task 1 : 이미지 피라미드
# cv2.pyrUp(src, dst=None, dstsize=None, borderType=None)
# cv2.pyrDown(src, dst=None, dstsize=None, borderType=None)
src = cv2.imread('apple.jpg')
dstUp = cv2.pyrUp(src)
dstDown = cv2.pyrDown(src)

cv2.imshow('src',src)
cv2.imshow('dstUp',dstUp)
cv2.imshow('dstDown',dstDown)
cv2.waitKey()
cv2.destroyAllWindows()


# Task2 : 윤곽선 검출
# cv2.findContours(image, mode, method, contours=None, hierarchy=None, offset=None)

src = cv2.imread('hand.jpg') #3채널 영상 받아서
src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) # 그레이스케일 따로 만들어주고
_, th = cv2.threshold(src_gray, 127,255, cv2.THRESH_BINARY) # 그거 이진화시켜서 마스크 딴다음에
contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # 윤곽선 ,계층 뽑아줌
dst = src.copy() # 출력용 영상 하나 복사해주고 

for cnt in contours:
    #윤곽선 그리기 
    # cv2.drawContours(image, contours, contourIdx, color, thickness=None, lineType=None, hierarchy=None, maxLevel=None, offset=None)
    cv2.drawContours(dst, cnt, -1, (255,0,0), 1, cv2.LINE_AA) #파란색
    
    #면적 따오기 
    # cv2.arcLength(cnt, closed=T/F)
    area = cv2.arcLength(cnt, True)
    print(area)

    #외곽선 근사화시켜서 모양 잡기
    # cv2.approxPolyDP(cnt, epsilon, closed, approxCurve=None)
    epsilon = 0.02 * area # 면적x0.02가 국룰이라고 함 
    approx = cv2.approxPolyDP(cnt, epsilon, True)
    print(len(approx))    
    cv2.drawContours(dst, [approx], 0,(0,255,0),1,cv2.LINE_AA) #초록색

    # 무게중심 따와서 그리기
    # cv2.moments(cnt, binaryImage=None)
    # cv2.circle(도화지, 중심점, 반지름, 색, 두께, 라인타입)
    moment = cv2.moments(cnt)
    cx = int(moment['m10']/moment['m00']) # x중심좌표
    cy = int(moment['m01']/moment['m00']) # y중심좌표
    cv2.circle(dst, (cx,cy), 5,(0,0,0), -1, 3)#무게중심 그리기 #검은색

    #바운딩박스 그리기
    # cv2.boundingRect(cnt) -> x,y, w,h
    # cv2.rectangle(도화지, 시작점, 끝점, 색, 두께, 라인타입)
    x, y, w, h = cv2.boundingRect(cnt)
    cv2.rectangle(dst, (x,y), (x+w, y+h), (0,0,255), 1, cv2.LINE_AA) # 빨간색 얇은거

    # 객체 모양에 맞게 회전된 박스 그리기----이건 그냥 외우자 
    rect = cv2.minAreaRect(cnt)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    cv2.drawContours(dst, [box], 0,(0,255,255),2)

    # convexHull 끝에쪽 테두리 잇기
    # cv2.convexHull(points, hull=None, clockwise=None, returnPoints=None(convexity defects 구할 때 False))
    hull =cv2.convexHull(cnt)
    cv2.drawContours(dst, [hull],0,(255,0,255),4) # 보라색

    #convexity defect 이건 convexhull 그리는거랑 같이 쓸 수가 없음
    # cv2.convexityDefects(contour, convexhull, convexityDefects=None)

    hull = cv2.convexHull(cnt, returnPoints=False)
    defect = cv2.convexityDefects(cnt, hull)
    for i in range (defect.shape[0]): # defect[0]번째개수만큼 점이 있는데
        s ,e ,f, d = defect[i,0] # 시작, 끝, 중심, 안쓰는값 은i,0번째임
        start = tuple(cnt[s][0]) # 시작점은 [s][0]
        end = tuple(cnt[e][0]) #끝점은[e][0]
        far = tuple(cnt[f][0]) #중심점은[f][0]

        cv2.line(dst, start, end ,[0,255,0], 5) # 선 초록색
        cv2.circle(dst, far, 5, [0,0,255],-1) # 점 빨간색

cv2.imshow('src',src)
cv2.imshow('th',th)
cv2.imshow('src',src)
cv2.imshow('dst',dst)
cv2.waitKey()
cv2.destroyAllWindows()

728x90

+ Recent posts