1. 程式人生 > >opencv學習13——毛玻璃效果

opencv學習13——毛玻璃效果

一、

影象毛玻璃效果

1.馬賽克是指定範圍內的統一填充,毛玻璃是指定範圍內的隨機取值

2.np.random.random(),隨機產生[0,1]內的數

二、

import cv2
import numpy as np

img = cv2.imread('image01.jpg', 1)
imgHeight, imgWidth, imgDeep = img.shape

dst = np.zeros(img.shape, np.uint8)
basicRangeSize = [5,5]

for i in range(imgHeight):
    for j in range(imgWidth):
        randomHeight = int(np.random.random() * basicRangeSize[0])
        randomWidth = int(np.random.random() * basicRangeSize[1])
        if i < imgHeight - basicRangeSize[0] and j < imgWidth - basicRangeSize[1]:
            dst[i, j] = img[i + randomHeight, j + randomWidth]
        # 防止資料下標溢位
        elif i < basicRangeSize[0]:
            dst[i, j] = img[i + randomHeight, j]
        elif j < basicRangeSize[1]:
            dst[i, j] = img[i, j + randomWidth]
        else:
            dst[i, j] = img[i - randomHeight, j - randomWidth]


cv2.imshow('',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()