计算机视觉：图片的马赛克、毛玻璃效果和图片的融合

1.马赛克效果

``````import cv2

# 马赛克效果
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
flag = 2   # 系数，系数越小，马赛克的效果越不明显
for m in range(26,height):
for n in range(140,880):
if m%flag==0 and n%flag==0:
for i in range(0,flag):
for j in range(0,flag):
(b,g,r) = img[m,n]
img[i+m,j+n] = (b,g,r)

cv2.imshow('dst',img)
cv2.waitKey(0)
``````

2. 毛玻璃效果

``````import cv2
import numpy as np
import random

# 毛玻璃效果
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dst = np.zeros((height,width,3),np.uint8)
for i in range(height-8):
for j in range(width-8):
index = int(random.random()*8)  # 生成0-8之间的随机数(不包括8)
dst[i, j] = img[i+index,j+index]
cv2.imshow('dst',dst)
cv2.waitKey(0)
``````

3. 图片的融合

``````import cv2

imgInfo = img_1.shape
height = imgInfo[0]
width = imgInfo[1]
# roi
roi = 0.5
cv2.imshow('dst',dst)
cv2.waitKey(0)
``````

算法实现

``````import cv2
import numpy as np

imgInfo = img_1.shape
height = imgInfo[0]
width = imgInfo[1]
# roi
roi = 0.5
dst = np.zeros((height,width,3),np.uint8)

for i in range(height):
for j in range(width):
(b1,g1,r1) = img_1[i,j]
(b2,g2,r2) = img_2[i,j]
dst[i,j] = (int(b1*roi+b2*(1-roi)),int(g1*roi+g2*(1-roi)),int(r1*roi+r2*(1-roi)))

cv2.imshow('dst',dst)
cv2.waitKey(0)

``````

THE END