[컴퓨터 비전] R,G,B 히스토그램 만들기

In9_9yu·2020년 9월 23일
1

컴퓨터 비전

목록 보기
2/2

목표 : 분리한 RGB 채널을 이용하여 히스토그램 만들기

[컴퓨터 비전] 이미지 읽기 및 R,G,B 채널 분리하기에서 R,G,B 채널을 분리하는 것까지 했다. 모양은 다음과 같다.

[[229 229 229 ... 221 221 221]
 [229 229 229 ... 221 221 221]
 [229 229 229 ... 221 221 221]
 ...
 [246 246 246 ... 245 245 245]
 [246 246 246 ... 245 245 245]
 [246 246 246 ... 245 245 245]]

1. 화소값 저장하기

R,G,B 채널 각각 0~255의 값 중 하나를 가지고 있을 텐데, 이를 저장하기 위해서 저장소를 만들어주자. (아래에는 Numpy를 사용하는 방법과, 사용하지 않는 방법 두 개 다 써놨는데, 이후는 numpy만 쓰는 것으로 할 것이다.)

Numpy를 사용하지 않고

def make_historgam (img) :

    height , width , num = img.shape

    red_bin= [0]*256
    green_bin = [0]*256
    blue_bin = [0]*256

    red, green, blue = cv2.split(img)

    for i in range(height):
        for j in range(width):
            red_bin[red[i][j]] += 1
            green_bin[green[i][j]] += 1
            blue_bin[blue[i][j]] += 1
            
    return red_bin,green_bin,blue_bin        
    
make_historgam(img)

Numpy 사용하기

def make_historgam (img) :

    height , width , num = img.shape

    red_bin= np.zeros(256,np.int32)
    green_bin = np.zeros(256,np.int32)
    blue_bin = np.zeros(256,np.int32)

    red, green, blue = cv2.split(img)

    for i in range(height):
        for j in range(width):
            red_bin[red[i][j]] += 1
            green_bin[green[i][j]] += 1
            blue_bin[blue[i][j]] += 1

    print(red_bin)
    print(green_bin)
    print(blue_bin)
    
    return red_bin,green_bin,blue_bin

make_historgam(img)

결과는 다음과 같이 나온다. (red_bin)

[     0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0
      1      3      5      7     14     12     18     21     52     75
     92    134    142    183    168    176    172    213    206    233
    326    354    319    344    457    441    385    353    388    403
    408    486    561    600    719    786    797    824    831    953
    902    997    977   1118   1108   1443   1213   1272   1300   1210
   1353   1486   1479   1677   1758   1904   1678   1807   1748   1615
   1686   1732   1817   1732   1739   1830   1903   1803   1927   1977
   2001   2047   2126   2062   2005   2010   2110   2172   2443   2548
   2555   2758   2792   2786   2774   2853   2889   2862   3002   3037
   3147   3109   3210   3151   3193   3517   3270   3165   3282   3052
   2972   2936   3053   2893   2948   3084   2943   2795   2772   2699
   2714   2592   2587   2503   2552   2475   2533   2600   2454   2535
   2475   2558   2504   2528   2441   2496   2364   2511   2482   2475
   2536   2570   2689   2684   2846   2910   2902   3026   3119   3220
   3413   3478   3630   4090   4062   4186   4303   4392   4713   4901
   5076   5300   5606   5865   6185   6414   6473   6588   6956   7238
   7522   7730   7913   8282   8688   8923   9236   9555   9636  10115
  10421  10645  10925  11475  11511  11896  12198  12239  12099  12493
  12847  14518  18928  24053  33118  30156  30128  44704  62184  66665
  79687 194354  79309  83756 116247  73871 134896 103015  94754 120609
  93475  84509  76551  72178 111070 135174 105772  38968  11000   9938
   9361   8789   8308   7674   6764  38966]

2. 히스토그램 그리기

plt를 이용하여 히스토그램을 그려보자.


def draw_histogram (img):
    red_bin ,green_bin,blue_bin = make_historgam(img)

    # plot 크기 / 범위 설정
    plt.figure(figsize=(21,7))
    plt.xlim([0,256])

    #red
    plt.subplot(131)
    plt.plot(red_bin,color="r")

    #green
    plt.subplot(132)
    plt.plot(green_bin,color="g")

    #blue
    plt.subplot(133)
    plt.plot(blue_bin,color="b")

    plt.show()

draw_histogram(img)

다음과 같이 코드를 작성하고 실행하면 아래의 표가 나타난다.

빨간 딸기 사진을 써서 red 채널의 내용이 압도적으로 많을 줄 알았는데? 또 그렇지도 않네.

그래도 오늘의 목표는 달성!

profile
FE 임니다

0개의 댓글