Article Directory
1. Relief effect
计算公式:
The difference between the pixel values of adjacent pixels plus a constant value
import cv2
import numpy as np
img = cv2.imread('img.jpg', 1)
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
cv2.imshow('gray', gray)
dat = np.zeros((img.shape[0], img.shape[1], 1), np.uint8)
for i in range(img.shape[0]):
for j in range(img.shape[1] - 1):
newP = int(gray[i, j]) - int(gray[i, j + 1]) + 200
if newP > 255:
newP = 255
if newP < 0:
newP = 0
dat[i, j] = newP
cv2.imshow('dat', dat)
cv2.waitKey(0)
2. Color mapping
Enhance blue pixels and green pixels
import cv2
import numpy as np
img = cv2.imread('img.jpg', 1)
cv2.imshow('img', img)
dat = np.zeros(img.shape, np.uint8)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
(b, g, r) = img[i, j]
b = b * 1.5
g = g * 1.5
if b > 255:
b = 255
if g > 255:
g = 255
dat[i, j] = (b, g, r)
cv2.imshow('dat', dat)
cv2.waitKey(0)
3. Oil painting special effects
The basic steps:
1.转化为灰色图像
2.划分为若干个小方块,统计小方块中的灰度值
3.将灰度值划分为几个等级,如0-255划分为四个等级,每个等级的大小为64
4.找到每个方块中灰度等级最多的像素,并求均值
5.用该均值替代原来的像素值得到油画效果
The following is the realization of a small square 8*8
oil painting effect
import cv2
import numpy as np
img = cv2.imread('img4.png', 1)
cv2.imshow('img', img)
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
dat = np.zeros(img.shape, np.uint8)
for i in range(4, img.shape[0] - 4):
for j in range(4, img.shape[1] - 4):
array1 = np.zeros(8, np.uint8)
for m in range(-4, 4):
for n in range(-4, 4):
array1[int(gray[i + m, j + n] / 32)] += 1
countMax = array1[0]
l = 0
for k in range(8):
if countMax < array1[k]:
countMax = array1[k]
l = k
sum_b, sum_g, sum_r,cnt = 0,0,0,0
for m in range(-4, 4):
for n in range(-4, 4):
if int(gray[i + m, j + n] / 32) == l:
(b,g,r) = img[i + m, j + m]
sum_b += b
sum_g += g
sum_r += r
cnt += 1
dat[i, j] = (sum_b // cnt, sum_g // cnt, sum_r // cnt)
cv2.imshow('dat', dat)
cv2.waitKey(0)