OpenCV+Tensorflow 人工智能图像处理(二十六)—— 油画特效

#1、转灰度, 2、分成小方块,统计灰度值, 3,、把0-255分成几个等级,将2映射到此范围内
#4、找小方块中灰度等级最多的像素等级,并求其均值
#5、平均值替换原来像素值
import cv2
import numpy as np
img = cv2.imread('car.jpg', 1)
cv2.imshow('src',img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  #灰度化
dst = np.zeros((height, width, 3), np.uint8)
for i in range(4, height-4):
    for j in range(4, width-4):
        array1 = np.zeros(8, np.uint8) #8个灰度等级像素个数
        for m in range(-4, 4):       #8*8小方块
            for n in range(-4, 4):
                p1 = int(gray[i+m, j+n]/32)   #8*32=256,灰度等级为32
                array1[p1] = array1[p1] + 1
        currentMax = array1[0]
        l = 0   #哪个段
        for k in range(0, 8):
            if currentMax= (l * 32) and gray[i+m, j+n] <= (l+1)*32:
                    (b,g,r) = img[i+m,j+n]
        dst[i, j] = (b, g, r)
cv2.imshow('dst',dst)
cv2.waitKey(0)

 

你可能感兴趣的:(python)