코딩 테스트 - 3

Kevin Lee·2020년 6월 29일
0

스트라드비젼 인턴

목록 보기
4/12

Test 5

테스트 5는 안드로이드 영상을 받아서 grayscale 영상으로 바꾸는 작업인데, openCV를 이용해서 frame을 BGR2GRAY로 cvtColor 해주면 간단하게 할 수 있다.

Test 6

Integral Image에 대한 코딩 이였는데, Sum of Integral Image 와 그냥 Sum of Image를 구현 하는 것 이었다.

우선 integral image는 쉽게 말해 픽셀의 값을 행과 열을 cumulative하게 더한 것이다.

Sum of integral image는 이렇게 integral image로 어떤 mask의 pixel의 합을 구할려면, 모든 픽셀을 traverse하면서 더할 필요 없이, mask의 점들에 위치 한 픽셀을 가지고 구할 수 있다.

이 방법은 constant한 시간으로 값을 구 하는 것이기 때문에 original image에서 구하는 것 보다 더 빠르다.

void integral_image(unsigned char* image, int width, int height, unsigned int* ii_image) 
{
    for(int i=0; i<width*height; i++){
        int row = i/width + 1, col = i%width + 1;
        int sum = 0;
        for(int j=0; j<row; j++){
            for(int k=0; k<col; k++)
                sum += image[width*j+k];
        }
        ii_image[i] = sum;
    }
}
int integral_image_sum_value(unsigned int* ii_image, int width, int height, int left, int top, int right, int bottom) 
{
    int topleft = width*(top-2) + (left-2);
    int topright = width*(top-2) + (right-1);
    int bottomleft = width*(bottom-1) + (left-2);
    int bottomright = width*(bottom-1) + (right -1);

    return ii_image[topleft] + ii_image[bottomright] - ii_image[topright] - ii_image[bottomleft];
}
int image_sum_value(unsigned char* image, int width, int height, int left, int top, int right, int bottom)
{
    int sum =0;
    for(int y = top-1; y < bottom; y++){
        for(int x = left-1 ; x < right; x++){
            sum += image[width*y+x];
        }
    }

    return sum;
   
}

integral image를 구할때 저렇게 3번 for문을 쓰지 않고 구할 수 있는 방법이 분명 있을 것이다 조금 더 연구를 해봐야겠다.

위 이미지에서도 볼 수 있듯이, bottomright 픽셀의과 topleft의 픽셀을 더하고 나머지 2 점의 픽셀 값을 빼면 그 안에 있는 모든 픽셀의 합을 구할 수 있다.

Wow!

이미지 출처

Integral Image - https://www.researchgate.net/figure/mage-conversion-to-summed-area-table-also-known-as-Integral-image_fig1_324554906

Sum of Integral Image - https://jangjy.tistory.com/32

profile
🧠🦾❤ https://github.com/to2915ny

0개의 댓글