1895

#include <stdio.h>
#include <stdlib.h>

// 오름차순 정렬
int cmp (const void *a, const void *b){
    return *(int *)a - *(int *)b;
}
int main(void){
    // 이미지 행열 입력받기
    int row = 0, col = 0;
    scanf("%d %d", &row, &col);

    // 이미지 픽셀값 입력 받기 
    int RC_product = row*col;
    int *arr = (int *)malloc(sizeof(int)*RC_product);
    for(int i = 0; i < RC_product; i++){
        scanf("%d", &arr[i]);
    }

    // 비교할 T 값 입력
    int t = 0;
    scanf("%d", &t);

    // 필터링 된 이미지 메모리 할당 하기
    int filter_product = (row-2)*(col-2);
    int *filter_arr = (int *)malloc(sizeof(int)*filter_product);
    
    // 이미지 필터링 하기
    int x = 0, y = 0;
    for(int i = 0; i < filter_product; i++){
        int filter[9] = {0};

        // 3x3 필터로 이미지 필터링하기 
        int f_x = x, f_y = y;
        for(int j = 0; j < 9; j++){
            filter[j] = arr[(f_x*col)+f_y];
            f_y++;

            if(f_y == (3+y)){
                f_x ++;
                f_y = y;
            }
        }

        // 3x3 픽셀값 정렬 후 중앙값 찾아서 저장
        qsort(filter, 9, sizeof(int), cmp);
        filter_arr[i] = filter[4];

        // y 방향으로 한 칸씩 이동
        y++;
        // y 방향으로 필터링된 이미지의 크기 열과 같아지면
        // y 는 0으로 초기화
        // x 는 +1
        if(y == (col-2)){
            x ++;
            y = 0;
        }
    }

    // 정렬하고 t값 이상이면 count 하기 
    qsort(filter_arr, filter_product, sizeof(int), cmp);
    int count = 0;
    for(int i = filter_product-1; i >= 0; i--){
        if(filter_arr[i] >= t){
            count++;
        }
    }

    // 출력
    printf("%d", count);
    free(arr);
    free(filter_arr);
    return 0;
}

+ Recent posts