import cv2
import numpy as np
img = cv2.imread('image0.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# newP = gray0-gray1+150 (浮雕效果计算公式:相邻像素的像素值之差,加上一个恒定值)。公式中,+150:为了增强图片浮雕灰度等级;相邻像素的像素值之差:为了突出灰度的突变
dst = np.zeros((height,width,1),np.uint8) #1:表明每个像素点由1中颜色来构成
for i in range(0,height):
for j in range(0,width-1): #记得减1
grayP0 = int(gray[i,j]) #当前像素值,怕超过255,先转换为Int
grayP1 = int(gray[i,j+1]) #下一个像素值,因为这边+1,所以上面的width要-1,否则会造成矩阵越界
newP = grayP0-grayP1+150 #计算新的像素值
if newP > 255:
newP = 255
if newP < 0:
newP = 0
dst[i,j] = newP
cv2.imshow('dst',dst)
cv2.waitKey(0)
image.png
网友评论