2020/08/02
就是被选中的区域
"""
ROI选区操作
"""
face = src[40:200, 90:300] # 选区
cv.imshow("face", face)
gray = cv.cvtColor(face, cv.COLOR_BGR2GRAY) # 变成灰色
backface = cv.cvtColor(gray, cv.COLOR_GRAY2BGR) # 灰色还原
src[40:200, 90:300] = backface # 重新放回选区
cv.imshow("face", src)
通过numpy,指定宽高
图像[高度范围,宽度范围]
face = src[40:200, 90:300]
"""
泛洪填充之填充彩色图像
"""
def fill_color_demo(image):
copyImg = image.copy()
h, w = image.shape[:2]
mask = np.zeros([h+2, w+2], np.uint8)
cv.floodFill(copyImg, mask, (30, 30), (0, 255, 255), (100, 100, 100), (50, 50, 50),cv.FLOODFILL_FIXED_RANGE)
# (30,30)-(100,100,100)得填充最低的像素,(30,30)+(50,50,50)得填充最高的像素
cv.imshow("fill_color_demo", copyImg)
"""
泛洪填充之R值填充
"""
def fill_binary():
image = np.zeros([400, 400, 3], np.uint8) #创建图
image[100:300, 100:300, :] = 255 #选区
cv.imshow("fill_binary", image)
mask = np.ones([402, 402, 1], np.uint8) #造mask
mask[101:301, 101:301] = 0
cv.floodFill(image, mask, (200, 200), (0, 0, 255), cv.flOODFILL_MASK_ONLY)#采用floodFill时,只有mask不为1的时候才会被填充
cv.imshow("fill_binary", image)
基于离散卷积
卷积原理:
"""
均值模糊
"""
def blur_demo(image):
dst = cv.blur(image, (1, 3))#1行3列
cv.imshow("blur_demo", dst)
"""
高斯模糊
"""
def clamp(pv): #clamp函数作用:确保在0-255之间
if pv > 255:
return 255
if pv < 0:
return 0
else:
return pv
def gaussian_noise(image):
h, w, c = image.shape
for row in range(h): # 获取随机数,其实range = range(0,h,1)
for col in range(w):
s = np.random.normal(0, 20, 3) # 每次产生三个
b = image[row, col, 0] # bule
g = image[row, col, 1] # green
r = image[row, col, 2] # rad
image[row, col, 0] = clamp(b + s[0])
image[row, col, 1] = clamp(g + s[1])
image[row, col, 2] = clamp(r + s[2])
cv.imshow("gaussian_noise",image)
"""
cv中高斯模糊API
"""
dst = cv.GaussianBlur(src,(0,0),15)
cv.imshow("Gaussian Blur",src)
"""
中值模糊
"""
def median_blur_demo(image):
dst = cv.medianBlur(image, 5)
cv.imshow("median_blur_demo", dst)
定义5*5的
"""
自定义定义3*3的模糊
"""
def custom_blur_demo1(image):
kernel = np.ones([5, 5], np.float32)/25
dst = cv.filter2D(image, -1, kernel=kernel) #一般是-1
cv.imshow("custom_blur_demo", dst)
定义3*3的
"""
自定义定义3*3的模糊
"""
def custom_blur_demo2(image):
kernel = np.array([1, 1, 1], [1, 1, 1], [1, 1, 1], np.float32)/9
dst = cv.filter2D(image, -1, kernel=kernel)
cv.imshow("custom_blur_demo", dst)
锐化函数要符合的原则:
①基数
②加起来总和等于0,这时一般做边缘的梯度等,总和等于1一些增强等工作,使图像更有立体感
"""
锐化
"""
def custom_blur_demo3(image):
kernel = np.array([0, -1, 0], [-1, 5, -1], [0, -1, 0], np.float32)
dst = cv.filter2D(image, -1, kernel=kernel)
cv.imshow("custom_blur_demo", dst)
①均值模糊:去除随机噪深
②高斯模糊:去噪,和均值模糊相比,可以保护图像的特征就是轮廓
③中值模糊:去除椒盐噪深
④锐化:提高图像的对比度,突出细节,使图像更有立体感
双线性模糊,相当于美颜滤镜,高斯磨皮,超赞!!!
上图所示参数中,
d一般为0,让sigmaColo和sigmaSpacer反算d;
sigmaColor颜色差异一般取值大一些,目的是让小的差异模糊掉(去燥深);
sigmaSpace空间差异一般取小一点,目的是使整个和小一点,把主要的差异保留出来
"""
高斯的双边模糊
"""
def bi_demo(image):# 高斯的双边模糊
dst = cv.bilateralFilter(image, 0, 100, 15)
cv.imshow("bi_demo", dst)
处理完像油画
"""
均值迁移模糊
"""
def shift_demo(image):#均值迁移模糊,处理完像油画
dst = cv.pyrMeanShiftFiltering(image, 10, 50)
cv.imshow("shift_demo", dst)