opencv中图像处理油画特效

opencv中图像处理油画特效

import cv2
import numpy as np
import random
import math

img = cv2.imread(r"C:\Users\lenovo\Desktop\python\python_vision\image.jpg",1)
cv2.imshow("img1",img)
imgInfo = img.shape
height = imgInfo[0]
weight = imgInfo[1]

gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
dst = np.zeros((height,weight,3),np.uint8)

for i in range(4,height-4):
    for j in range(4,weight-4):
        arry = np.zeros(8,np.uint8)
        for m in range(-4,4):
            for n in range(-4,4):
                p1 = int(gray[i+m,j+n]/32)
                arry[p1] = arry[p1] + 1
        currentMax = arry[0]
        l = 0
        for k in range(0,8):
            if currentMax<arry[k]:
                currentMax = [k]
                l = k
        for m in range(-4,4):
            for n in range(-4,4):
                if gray[i+m,j+n] >=(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()

opencv中图像处理油画特效_第1张图片

油画特效的实现思路主要分以下五步:

1.将图片转换为灰色图片
2.将图片分割成小方块,例如77,1010等
3.将像素值划分成若干等级,例如划分成4个等级,则063为等级一,64127为等级二,128191为等级三,192255为等级四
4.将步骤2中每个小方块,计算其内各等级的像素点个数
5.使用统计出的平均值替换原像素值

你可能感兴趣的:(#,ros-机器视觉,opencv,计算机视觉,cv,图像识别)