Opencv 马赛克和毛玻璃效果与图片融合的实现

1.马赛克效果

马赛克的基本原理就是,用某一个区域的某一个像素点替代这个区域所有的像素点,从而导致图片出现模糊的效果,如下:

import cv2

# 马赛克效果

img = cv2.imread(filename='../anqila21.jpg',flags=1)

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

# 毛玻璃效果

img = cv2.imread(filename='../anqila21.jpg',flags=1)

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

img_1 = cv2.imread(filename='../anqila21.jpg',flags=1)

img_2 = cv2.imread(filename='../huanghe.jpg',flags=1)

imgInfo = img_1.shape

height = imgInfo[0]

width = imgInfo[1]

# roi

roi = 0.5

dst = cv2.addWeighted(img_1,roi,img_2,(1-roi),0)

cv2.imshow('dst',dst)

cv2.waitKey(0)

算法实现

import cv2

import numpy as np

img_1 = cv2.imread(filename='../anqila21.jpg',flags=1)

img_2 = cv2.imread(filename='../huanghe.jpg',flags=1)

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)

以上就是Opencv 马赛克和毛玻璃效果与图片融合的实现的详细内容,更多关于Opencv 的资料请关注其它相关文章!

以上是 Opencv 马赛克和毛玻璃效果与图片融合的实现 的全部内容, 来源链接: utcz.com/p/247712.html

回到顶部