2017년 9월 9일 토요일

[python] OpenCV 박스필터링 (직접구현)

import numpy as np
import cv2

img = cv2.imread('demo.jpg')
print(img.shape, img.dtype)

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

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

dst = np.zeros((height, width, 3), np.uint8)
filter = np.ones((5,5))/25

print("converting...", end='')
for h in range(2, height-2):
    for w in range(2, width-2):
        sb = 0.0
        sg = 0.0
        sr = 0.0
        for y in range(-2, 3):
            for x in range(-2, 3):
                sb += img[h+y, w+x, 0]*filter[y+2, x+2]
                sg += img[h+y, w+x, 1]*filter[y+2, x+2]
                sr += img[h+y, w+x, 2]*filter[y+2, x+2]
             
        dst[h, w, 0] = sb.astype(np.uint8)    
        dst[h, w, 1] = sg.astype(np.uint8)
        dst[h, w, 2] = sr.astype(np.uint8)

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

댓글 없음:

댓글 쓰기