代码地址:https://github.com/Chakid/ImageProcess
浮雕/雕刻算法实质是很简单地,即:对图像的每一个点进行卷积处理。假设原图像为X,处理后的图像为Y;浮雕算法核kernel矩阵定义为:[1 , 0, 0 ; 0, 0, 0; 0, 0, -1]. 那么,对于坐标为(i,j)点,浮雕效果图的算法为Y(i,j) = X(i+1,j+1)-X(i-1,j-1) + 128。当然,X,Y的取值均在0~255之间。雕刻算法核kernel矩阵定义为:[1, 0; 0, -1]。那么,对于坐标为(i,j)点,其浮雕效果图的算法为Y(i,j) = X(i,j) - X(i-1,j-1) + 128。当然,X,Y的取值均在0~255之间。
代码展示:
# coding:utf-8
'''
浮雕效果
'''
import cv2
import numpy as np
gray=cv2.imread('img/cxk.jpg',0)
imgInfo=gray.shape
height=imgInfo[0]
weight=imgInfo[1]
dst=np.zeros((height,weight,1),np.uint8)
for i in range(0,height):
for j in range(0,weight-1):
gray0=gray[i,j]
gray1=gray[i,j+1]
newp=gray0-gray1+150
if newp>255:
newp=255
else:
newp=0
dst[i,j]=newp
cv2.imshow('dst',dst)
cv2.waitKey(0)