DeepFlowest

[오픈 소스] 코드 리뷰 & 분석 본문

기타

[오픈 소스] 코드 리뷰 & 분석

Orange57 2020. 5. 2. 00:21
728x90
반응형
SMALL

main 함수1

- processed_img : Pictures폴더에서 가져온 사진들 (getFixedImagesPeaks 함수에서 전처리 한)

- background : (getFixedImagesPeaks 함수에서 전처리 한) bg.jpg

- diffThreshHoldConst = 0.8

SSIM : 두 개의 그레이 스케일 이미지 사이에서 SSIM (Structural Similarity Index)을 계산

    - score : 두 개의 입력 화상들 간의 구조적 유사성 인덱스를 나타낸다. 이 값은 [-1, 1] 범위에 속할 수  있으며 값 하나는 "완전 일치"
    - diffThreshold : 차이 이미지는 현재 [0, 1] 범위의 부동 소수점 데이터 유형으로 표현

def getPeaks(processedImage, background, diffThreshHoldConst, originalImage, cutOffFreq, isVideoFrame=True, index = -1 ,
             name="Camera", drawImage=True):
    pickedPeaks = None
    humanCenter = None
    (score, diffThreshold) = ssim(processedImage, background, full=True)  # full=True : 전체 구조 유사성 이미지를 반환
    if (debugMode):
        cv2.imshow("2 Difference", diffThreshold)

    ############# Dilation + Erosion #############
    # Threshold
    if isVideoFrame:
        diffThreshold[diffThreshold < diffThreshHoldConst] = 0
        diffThreshold[diffThreshold > diffThreshHoldConst] = 1
        if (debugMode):
            cv2.imshow("2' Diff After Threshold", diffThreshold)
        # Erosion + Dilation. Erosion to fill holes with black
        dilationFilter = np.ones((5, 5), np.uint8)
        erosionVid = cv2.erode(diffThreshold, dilationFilter, iterations=4)
        dilationVid = cv2.dilate(erosionVid, dilationFilter, iterations=3)
        if debugMode:
            cv2.imshow("3 After Erosion+Dilation", dilationVid)

        processedImage = np.array(1 - dilationVid).astype('uint8')
        processedImage[processedImage == 1] = 255
    else:
        diffThreshold[diffThreshold < diffThreshHoldConst] = 255
        diffThreshold[diffThreshold != 255] = 0
        processedImage = np.array(diffThreshold).astype('uint8')

    _, contours, _ = cv2.findContours(processedImage, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    outer_contours = np.zeros((0, 0))
    if len(contours) > 0:

        outer_contours = getOuterContour(contours, originalImage)

        if outer_contours.shape[0] > 1:
            humanCenter = np.average(outer_contours, axis=0).reshape((1, 2)).astype('int')
            cv2.circle(originalImage, (humanCenter[0, 0], humanCenter[0, 1]), 5, (0, 255, 0), -1)
            distance = cdist(humanCenter, outer_contours, metric='euclidean')
            xAxis = np.linspace(0, (distance.shape[1]) - 1, distance.shape[1])
            # plt.plot(xAxis,distance)

            # filter order , cutoff , type
            b, a = signal.butter(1, cutOffFreq)
            try:
                output_signal = np.transpose(signal.filtfilt(b, a, distance)).flatten()
            except:
                return None,None

            #################### plot & Draw New Points ####################
            if len(distance) != 0:
                if (debugMode):
                    plt.plot(xAxis, output_signal)

                ## Local Maximum Array of True & Falses
                localMaximaTrueFalse = np.r_[True, output_signal[1:] > output_signal[:-1]] & np.r_[
                    output_signal[:-1] > output_signal[1:], True]
                ## Get Indices of Local Maxima Values
                LocalMaximaIndices = np.array(np.where(localMaximaTrueFalse))[0]

                if (debugMode):
                    for localMaximaIndex in LocalMaximaIndices:
                        plt.plot(xAxis[localMaximaIndex], output_signal[localMaximaIndex], marker='o')

                pickedPeaks = np.array(getKMeans(outer_contours[LocalMaximaIndices], k=4))
                if pickedPeaks.shape[0] >= 4:
                    pickedPeaks = getSkeletonParts(pickedPeaks)
                    # print("peaks = ", peaks)
                    for iPoint in range(pickedPeaks.shape[0]):
                        cv2.circle(originalImage, (int(pickedPeaks[iPoint, 0]), int(pickedPeaks[iPoint, 1])), 5, (0, 0, 255), -1)

                    if isVideoFrame:  #IF original video
                        # Head Body
                        cv2.line(originalImage, (int(pickedPeaks[3][0]), int(pickedPeaks[3][1])),
                                 (int(humanCenter[0][0]), int(humanCenter[0][1])), (255, 0, 0), 1)
                        # Left Body
                        cv2.line(originalImage, (int(pickedPeaks[1][0]), int(pickedPeaks[1][1])),
                                 (int(humanCenter[0][0]), int(humanCenter[0][1])), (255, 0, 0), 1)
                        # Right Body
                        cv2.line(originalImage, (int(pickedPeaks[0][0]), int(pickedPeaks[0][1])),
                                 (int(humanCenter[0][0]), int(humanCenter[0][1])), (255, 0, 0), 1)
                        # Leg Body
                        cv2.line(originalImage, (int(pickedPeaks[2][0]), int(pickedPeaks[2][1])),
                                 (int(humanCenter[0][0]), int(humanCenter[0][1])), (255, 0, 0), 1)
                    else:   # If showing frame
                        originalImage = cv2.resize(originalImage, (0,0), fx=0.25, fy=0.25)

                    if drawImage:
                        cv2.imshow("My Program"+str(isVideoFrame), np.fliplr(originalImage))

                    # if not isVideoFrame:
                    #     cv2.imwrite("Contoured "+str(index)+ ".png", originalImage)
                else:
                    return None
                # plt.draw()
                # plt.pause(0.001)
                # plt.gcf().clear()
                # cv2.imshow("6 Skeleton All Points", image)
                # print("Object center is ", humanCenter)
    return pickedPeaks, humanCenter
728x90
반응형
LIST

'기타' 카테고리의 다른 글

주피터 노트북 테마 변경하기  (0) 2021.03.19
좌표 추출 참고 사이트  (0) 2020.11.10
운영체제와 프로그래밍 언어  (0) 2020.06.23
Comments