图像浮雕与雕刻效果
浮雕效果:
指像素与周围像素的差异确定像素,差异较大的像素值较大,在灰度图像中表现较亮,边缘凸起,形成浮雕形状。
最基本的浮雕效果是根据像素与周围八个像素之间的差异来确定方向。
调和浮雕是根据像素与左上、左下、右上、右下四个方向的差值来确定新像素。
import cv2
import numpy as np
img = cv2.imread('flower.png',0)
cv2.imshow('Orignal',img)
h,w = img.shape
res = np.zeros(img.shape,img.dtype)
##[i,j]处像素-[i 1,,j 1]处像素 150
for i in range(0,h-1):
for j in range(0, w-1):
gray = int(img[i,j]) -int(img[i 1,j 1]) 150
if gray>255:
gray = 255
if gray < 0:
gray = 0
res[i,j] = gray
cv2.imshow('Result',res)
cv2.waitKey(0)
调和浮雕:
#调和浮雕
import cv2
import numpy as np
img = cv2.imread('flower.png',0)
cv2.imshow('Orignal',img)
h,w = img.shape
res = np.zeros(img.shape,img.dtype)
##
for i in range(1,h-1):
for j in range(1, w-1):
gray0 = int(img[i,j])
gray1 = int(img[i-1, j-1])
gray2 = int(img[i-1, j 1])
gray3 = int(img[i 1, j-1])
gray4 = int(img[i 1, j 1])
gray = 4* gray0 - gray1 - gray2 -gray3 -gray4 128
if gray>255:
gray = 255
if gray < 0:
gray = 0
res[i,j] = gray
cv2.imshow('Result',res)
cv2.waitKey(0)
结果如下:
原图浮雕效果调和浮雕效果
右下浮雕立体效果明显,但调和浮雕轮廓明显。
雕刻效果import cv2
import numpy as np
img = cv2.imread('flower.png',0)
cv2.imshow('Orignal',img)
h,w = img.shape
res = np.zeros(img.shape,img.dtype)
##
for i in range(0,h-1):
for j in range(0, w-1):
gray = int(img[i 1,j 1]) - int(img[i,j]) 150
if gray>255:
gray = 255
if gray < 0:
gray = 0
res[i,j] = gray
cv2.imshow('Result',res)
cv2.waitKey(0)
效果如下: