def main():

    ##### Set threshold
    threshold = 14.21

    ##### Set alpha
    alpha = 0.888

    ##### Set Median_array
    medians = [3, 5, 7, 9, 11]

    ##### Kerenl for Erosion, Dilation
    close_kernel = np.ones((11, 11), np.uint8)

    ##### Set path
    input_path = './input_image'  # input path
    gt_path = './groundtruth'  # groundtruth path
    result_path = './result'  # result path

    ##### load input
    input = [
        img for img in sorted(os.listdir(input_path)) if img.endswith(".jpg")
    ]

    ##### first frame and first background
    frame_current = cv.imread(os.path.join(input_path, input[0]))
    frame_current_gray = cv.cvtColor(frame_current,
                                     cv.COLOR_BGR2GRAY).astype(np.float64)
    frame_prev_gray = frame_current_gray
    background_prev_gray = frame_current_gray

    ##### background substraction
    for image_idx in range(len(input)):

        ##### calculate foreground region
        background_current_gray = (
            (1 - alpha) * background_prev_gray) + (alpha * frame_prev_gray)

        diff = frame_current_gray - background_current_gray
        diff_abs = np.abs(diff).astype(np.float64)

        ##### make mask by applying threshold
        frame_diff = np.where(diff_abs > threshold, 1.0, 0.0)

        ##### apply mask to current frame
        current_gray_masked = np.multiply(frame_current_gray, frame_diff)
        current_gray_masked_mk2 = np.where(current_gray_masked > 0, 255.0, 0.0)

        ##### final result
        result = current_gray_masked_mk2.astype(np.uint8)

        for median_idx in range(len(medians)):
            result = cv.medianBlur(result, medians[median_idx])
            result = cv.morphologyEx(result,
                                     cv.MORPH_CLOSE,
                                     close_kernel,
                                     iterations=1)

        cv.imshow('result', result)

        ##### renew background
        frame_prev_gray = frame_current_gray
        background_prev_gray = background_current_gray

        ##### make result file
        ##### Please don't modify path
        cv.imwrite(
            os.path.join(result_path, 'result%06d.png' % (image_idx + 1)),
            result)

        ##### end of input
        if image_idx == len(input) - 1:
            break

        ##### read next frame
        frame_current = cv.imread(
            os.path.join(input_path, input[image_idx + 1]))
        frame_current_gray = cv.cvtColor(frame_current,
                                         cv.COLOR_BGR2GRAY).astype(np.float64)

        ##### If you want to stop, press ESC key
        k = cv.waitKey(30) & 0xff
        if k == 27:
            break

    ##### evaluation result
    eval.cal_result(gt_path, result_path)
def main():
    ##### Set threshold
    threshold1 = 8.5

    ##### Set path
    input_path = './input'  # input path
    gt_path = './groundtruth'  # groundtruth path
    result_path = './result'  # result path

    ##### load input
    input = [
        img for img in sorted(os.listdir(input_path)) if img.endswith(".jpg")
    ]

    ##### first frame and first background
    frame_current = cv.imread(os.path.join(input_path, input[0]))
    frame_current_gray = cv.cvtColor(frame_current,
                                     cv.COLOR_BGR2GRAY).astype(np.float64)
    frame_prev_gray = frame_current_gray

    # 모든 프레임에 대한 median을 계산하는 것은 오랜 시간이 걸려 샘플링 후 median을 계산
    frame_median = np.ndarray(shape=frame_current_gray.shape)

    # 계산을 위해 shape를 저장
    n = frame_current_gray.shape[0]
    m = frame_current_gray.shape[1]

    # median 값 계산을 위해 n*m개의 딕셔너리를 생성함
    # 딕셔너리의 key는 색깔, value는 counting 용도
    frame_median_dict = [[{} for j in range(m)] for i in range(n)]

    # 50 프레임 마다 하나의 샘플링을 통해 메디안을 계산
    for image_idx in range(0, len(input), 50):
        if image_idx >= len(input) - 1:
            break

        frame = cv.imread(os.path.join(input_path, input[image_idx + 1]))
        frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY).astype(np.float64)

        for i in range(n):
            for j in range(m):
                col = frame_gray[i][j]
                if col in frame_median_dict[i][j].keys():
                    frame_median_dict[i][j][
                        col] = frame_median_dict[i][j][col] + 1
                else:
                    frame_median_dict[i][j][col] = 1

    # keys를 정렬 후 딕셔너리를 keys로 순회하며 median_count에서 key에 해당하는 value만큼 빼줌
    # 초기 median_count가 median이 될 index이므로 0이거나 음수가 되면 그 값이 median
    for i in range(n):
        for j in range(m):
            median_count = int(int(len(input) / 2) / 50)
            keys = list(frame_median_dict[i][j].keys())
            keys.sort()
            for k in keys:
                median_count = median_count - frame_median_dict[i][j][k]
                if median_count <= 0:
                    frame_median[i][j] = k
                    break

    diff_temp = np.abs(frame_current_gray - frame_median).astype(np.float64)

    ##### background substraction
    for image_idx in range(len(input)):

        print(image_idx)

        ##### calculate foreground region
        diff = frame_current_gray - frame_median
        diff_abs = np.abs(diff).astype(np.float64)

        # diff_prev = frame_current_gray - frame_prev_gray
        # diff_abs2 = np.abs(diff_prev).astype(np.float64)

        ##### make mask by applying threshold
        frame_diff = np.where(diff_abs > threshold1, 1.0, 0.0)

        # r = 0.005
        # frame_diff = (1 - r) * frame_diff1 + r * frame_diff2

        ##### apply mask to current frame
        current_gray_masked = np.multiply(frame_current_gray, frame_diff)
        current_gray_masked_mk2 = np.where(current_gray_masked > 0, 255.0, 0.0)

        ##### result
        result = current_gray_masked_mk2.astype(np.uint8)

        result = cv.medianBlur(result, 13)
        kernel = np.ones((9, 9), np.uint8)
        result = cv.morphologyEx(result,
                                 cv.MORPH_OPEN,
                                 kernel=np.ones((9, 9), np.uint8))
        result = cv.morphologyEx(result,
                                 cv.MORPH_CLOSE,
                                 kernel=np.ones((9, 9), np.uint8))

        cv.imshow('result', result)

        ##### make result file
        ##### Please don't modify path
        cv.imwrite(
            os.path.join(result_path, 'result%06d.png' % (image_idx + 1)),
            result)

        ##### end of input
        if image_idx == len(input) - 1:
            break

        ##### read next frame
        frame_current = cv.imread(
            os.path.join(input_path, input[image_idx + 1]))
        frame_current_gray = cv.cvtColor(frame_current,
                                         cv.COLOR_BGR2GRAY).astype(np.float64)

        ##### If you want to stop, press ESC key
        k = cv.waitKey(30) & 0xff
        if k == 27:
            break

    ##### evaluation result
    eval.cal_result(gt_path, result_path)
def main():

    ##### Set threshold
    threshold = 60
    avg = 0
    total = 0

    ##### Set path
    input_path = './input_image'  # input path
    gt_path = './groundtruth'  # groundtruth path
    result_path = './result'  # result path

    ##### load input
    input = [
        img for img in sorted(os.listdir(input_path)) if img.endswith(".jpg")
    ]

    ##### first frame and first background
    frame_current = cv.imread(os.path.join(input_path, input[0]))
    frame_current_gray = cv.cvtColor(frame_current,
                                     cv.COLOR_BGR2GRAY).astype(np.float64)
    frame_prev_gray = frame_current_gray

    ##### background substraction
    for image_idx in range(len(input)):

        ##### calculate foreground region
        diff = 2.1 * frame_prev_gray - 2.15 * avg - 0.15 * frame_current_gray
        diff_abs = np.abs(diff).astype(np.float64)

        ##### make mask by applying threshold
        frame_diff = np.where(diff_abs > threshold, 1.0, 0.0)

        ##### apply mask to current frame
        current_gray_masked = np.multiply(frame_current_gray, frame_diff)
        current_gray_masked_mk2 = np.where(current_gray_masked > 0, 255.0, 0.0)

        ##### final result
        result = current_gray_masked_mk2.astype(np.uint8)
        result = cv.medianBlur(result, 9)
        result = cv.GaussianBlur(result, (3, 3), 0)
        cv.imshow('result', result)

        ##### renew background
        frame_prev_gray = frame_current_gray

        ##### make result file
        ##### Please don't modify path
        cv.imwrite(
            os.path.join(result_path, 'result%06d.png' % (image_idx + 1)),
            result)

        ##### end of input
        if image_idx == len(input) - 1:
            break

        ##### read next frame
        frame_current = cv.imread(
            os.path.join(input_path, input[image_idx + 1]))
        frame_current_gray = cv.cvtColor(frame_current,
                                         cv.COLOR_BGR2GRAY).astype(np.float64)
        frame_prev_gray = frame_current_gray

        #### calculate average
        total += frame_current_gray
        avg = total / (image_idx + 1)

        ##### If you want to stop, press ESC key
        k = cv.waitKey(30) & 0xff
        if k == 27:
            break

    ##### evaluation result
    eval.cal_result(gt_path, result_path)
Esempio n. 4
0
def main():

    ##### Set threshold
    threshold = 23
    
    ##### Set path
    input_path = './input_image'    # input path
    gt_path = './groundtruth'       # groundtruth path
    result_path = './result'        # result path

    ##### load input
    input = [img for img in sorted(os.listdir(input_path)) if img.endswith(".jpg")]

    ##### first frame and first background
    frame_current = cv.imread(os.path.join(input_path, input[0]))
    frame_current_gray = cv.cvtColor(frame_current, cv.COLOR_BGR2GRAY).astype(np.float64)
    bg = cv.imread(os.path.join(input_path, input[470]))
    bg_gray = cv.cvtColor(frame_current, cv.COLOR_BGR2GRAY).astype(np.float64)
    
    ##### background substraction
    for image_idx in range(len(input)):
        
        ##### calculate foreground region
        diff = frame_current_gray - bg_gray
        diff_abs = np.abs(diff).astype(np.float64)
        
        ##### make mask by applying threshold
        frame_diff = np.where(diff_abs > threshold, 1.0, 0.0)
        
        ##### apply mask to current frame
        current_gray_masked = np.multiply(frame_current_gray, frame_diff)
        current_gray_masked_mk2 = np.where(current_gray_masked > 0, 255.0, 0.0)
        
        ##### result
        result = current_gray_masked_mk2.astype(np.uint8)
        
        ##### non-road clearance from result frame
        for y in range(0, 320):
          for x in range(0, 240):
            if y<=-x+210:
              result[x][y] = 0.0
            if y>=270 :
              result[x][y] = 0.0
        
        ##### make result file
        ##### Please don't modify path
        cv.imwrite(os.path.join(result_path, 'result%06d.png' % (image_idx + 1)), result)
        
        ##### end of input
        if image_idx == len(input) - 1:
            break

        ##### read next frame
        frame_current = cv.imread(os.path.join(input_path, input[image_idx + 1]))
        frame_current_gray = cv.cvtColor(frame_current, cv.COLOR_BGR2GRAY).astype(np.float64)

        ##### If you want to stop, press ESC key
        k = cv.waitKey(30) & 0xff
        if k == 27:
            break

    ##### evaluation result
    eval.cal_result(gt_path, result_path)
Esempio n. 5
0
def main():

    ##### Set parameters
    threshold = 28

    ##### Set path
    input_path = './input_resize'  # input path
    gt_path = './groundtruth_resize'  # groundtruth path
    bg_result_path = './background'
    result_path = './result'  # result path

    ##### load input
    input = [
        img for img in sorted(os.listdir(input_path)) if img.endswith(".jpg")
    ]

    ##### 이전 결과 지우기
    for img in os.scandir(result_path):
        os.remove(img.path)

    ##### first frame and first background
    frame_current = cv.imread(os.path.join(input_path, input[0]))
    frame_current_gray = cv.cvtColor(frame_current,
                                     cv.COLOR_BGR2GRAY).astype(np.float64)
    frame_bg_gray = frame_current_gray

    print(frame_current.shape)
    height = frame_current.shape[0]
    width = frame_current.shape[1]

    ##### 배경 modeling에 사용할 이전 frame들을 저장하는 np array
    bg_list = np.zeros((0, height, width))

    for image_idx in range(len(input)):
        print(image_idx)

        ##### calculate foreground region
        diff = frame_current_gray - frame_bg_gray
        diff_abs = np.abs(diff).astype(np.float64)

        ##### make mask by applying threshold
        ##### diff가 threshold 이상이면 1.0, 아니면 0.0을 저장한 mask(nparray)
        frame_diff = np.where(diff_abs > threshold, 1.0, 0.0)

        ##### apply mask to current frame
        # current_gray_masked       : frame_current_gray에서 움직이는 부분만 남긴거
        # current_gray_masked_mk2   : current_gray_masked를 binary(흰/검) 이미지로 변환
        current_gray_masked = np.multiply(frame_current_gray, frame_diff)
        current_gray_masked_mk2 = np.where(current_gray_masked > 0, 255.0, 0.0)

        ##### final result
        result = current_gray_masked_mk2.astype(np.uint8)

        ##### result에서 노이즈 제거 및 opening
        # kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
        # result = cv.medianBlur(result,13)
        # result = cv.erode(result,kernel)
        # result = cv.medianBlur(result,11)
        # result = cv.dilate(result,kernel)
        # result = cv.dilate(result,kernel)

        ##### show background & result
        bg = frame_bg_gray.astype(np.uint8)
        cv.imshow('result', result)
        cv.imshow('background', bg)

        ##### renew background
        ############################################
        frame_reshape = np.reshape(frame_current_gray, (1, height, width))
        bg_list = np.append(bg_list, frame_reshape, axis=0)

        # 500 frame이 넘어가면 최근 500개의 frame만 사용해서 bg modeling
        if len(bg_list) > 300:
            bg_list = bg_list[len(bg_list) - 300:]

        # 이전 frame들의 중간값을 background로 설정
        frame_bg_gray = np.median(bg_list, axis=0)
        ############################################

        ##### make result file
        ##### Please don't modify path
        cv.imwrite(
            os.path.join(result_path, 'result%06d.png' % (image_idx + 1)),
            result)
        cv.imwrite(
            os.path.join(bg_result_path, 'bg%06d.png' % (image_idx + 1)), bg)

        ##### end of input
        if image_idx == len(input) - 1:
            break

        ##### read next frame
        frame_current = cv.imread(
            os.path.join(input_path, input[image_idx + 1]))
        frame_current_gray = cv.cvtColor(frame_current,
                                         cv.COLOR_BGR2GRAY).astype(np.float64)

        ##### If you want to stop, press ESC key
        k = cv.waitKey(30) & 0xff
        if k == 27:
            print("ESC로 종료됨")
            break

    ##### evaluation result
    eval.cal_result(gt_path, result_path)
Esempio n. 6
0
def main(th, gaussian_size, median_size):

    ##### Set threshold
    threshold = th

    ##### Set path
    input_path = './input_image'  # input path
    gt_path = './groundtruth'  # groundtruth path
    result_path = './result'  # result path

    ##### load input
    input = [
        img for img in sorted(os.listdir(input_path)) if img.endswith(".jpg")
    ]

    ##### first frame and first background
    frame_current = cv.imread(os.path.join(input_path, input[0]))
    frame_current_gray = cv.cvtColor(frame_current,
                                     cv.COLOR_BGR2GRAY).astype(np.float64)
    frame_current_gray = denoise(frame_current_gray, gaussian_size)
    frame_prev_gray = frame_current_gray

    ##### background substraction
    for image_idx in range(len(input)):

        ##### calculate foreground region
        diff = frame_current_gray - frame_prev_gray
        diff_abs = np.abs(diff).astype(np.float64)

        ##### make mask by applying threshold
        frame_diff = np.where(diff_abs > threshold, 1.0, 0.0)

        ##### apply mask to current frame
        current_gray_masked = np.multiply(frame_current_gray, frame_diff)
        current_gray_masked_mk2 = np.where(current_gray_masked > 0, 255.0, 0.0)

        ##### result
        result = current_gray_masked_mk2.astype(np.uint8)

        ##### Copy the image
        im_floodfill = result.copy()

        ##### Mask used to flood filling
        ##### the size needs to be 2 pixels than the image
        h, w = result.shape[:2]
        mask = np.zeros((h + 2, w + 2), np.uint8)

        ##### Floodfill from point (0, 0)
        cv.floodFill(im_floodfill, mask, (0, 0), 255)

        ##### Invert floodfilled image
        im_floodfill_inv = cv.bitwise_not(im_floodfill)

        ##### Combine the two images to get the foreground
        im_out = result | im_floodfill_inv

        ##### apply morphology for filling inside of the objects
        kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (9, 9))
        im_out = cv.morphologyEx(im_out, cv.MORPH_CLOSE, kernel)

        ##### post processing
        im_out = signal.medfilt(im_out, median_size)
        im_out = cv.GaussianBlur(im_out, (gaussian_size, gaussian_size), 0)
        th, im_out = cv.threshold(im_out, 200, 255, cv.THRESH_BINARY)

        ##### show final image
        cv.imshow('result', im_out)

        ##### renew background
        frame_prev_gray = frame_current_gray

        ##### make result file
        ##### Please don't modify path
        cv.imwrite(
            os.path.join(result_path, 'result%06d.png' % (image_idx + 1)),
            im_out)

        ##### end of input
        if image_idx == len(input) - 1:
            break

        ##### read next frame
        frame_current = cv.imread(
            os.path.join(input_path, input[image_idx + 1]))
        frame_current_gray = cv.cvtColor(frame_current,
                                         cv.COLOR_BGR2GRAY).astype(np.float64)
        frame_current_gray = denoise(frame_current_gray, gaussian_size)

        ##### If you want to stop, press ESC key
        k = cv.waitKey(30) & 0xff
        if k == 27:
            break

    ##### evaluation result
    eval.cal_result(gt_path, result_path)