2017년 9월 9일 토요일

[python] OpenCV 픽셀단위 처리 (그레이영상만들기)

영상 처리: 픽셀단위처리 - 그레이영상 만들기
--------------------
import numpy as np
import cv2

img = cv2.imread('demo.jpg')    #img는 numpy array
print(img.shape, img.dtype)

cv2.imshow('', img)
print("press anykey on the image")
cv2.waitKey(0)

height = img.shape[0]
width = img.shape[1]

gray = np.zeros((height, width, 3), dtype=np.uint8)

print("converting...", end='')
for h in range(height):
    for w in range(width):
        b = img[h, w, 0].astype(np.float)
        g = img[h, w, 1].astype(np.float)
        r = img[h, w, 2].astype(np.float)
     
        intensity = (b + g + r) / 3
        #intensity = intensity.astype(np.uint8)
     
        gray[h, w, 0] = intensity
        gray[h, w, 1] = intensity
        gray[h, w, 2] = intensity

print("done.")    
cv2.imshow('', gray)
print("press anykey on the image")
cv2.waitKey(0)
cv2.destroyAllWindows()

--------------------
b = img[:,:,0].astype(np.float)
g = img[:,:,1].astype(np.float)
r = img[:,:,2].astype(np.float)
gray = ((b+g+r)/3.0).astype(np.uint8)
--------------------

댓글 없음:

댓글 쓰기