一 灰度图片反转
pixel 255-pixel
# 灰度颜色反转 底片效果
import cv2
import numpy as np
img=cv2.imread('image.jpg',1) # 0 灰度图 1 彩色图
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dst=np.zeros((height,width,1),np.uint8)
for i in range(0,height):
for j in range(0,width):
grayPixel=gray[i,j]
dst[i,j]=255-grayPixel
cv2.imshow('dst',dst)
cv2.waitKey(0)
二。彩色图片反转
import cv2
import numpy as np
img=cv2.imread('image.jpg',1) # 0 灰度图 1 彩色图
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
dst=np.zeros((height,width,3),np.uint8)
for i in range(0,height):
for j in range(0,width):
(b,g,r)=img[i,j]
dst[i,j]=(255-b,255-g,255-r)
cv2.imshow('dst',dst)
cv2.waitKey(0)
三 马赛克
# 马赛克
import cv2
import numpy as np
img=cv2.imread('image.jpg',1) # 0 灰度图 1 彩色图
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
for m in range(50,200):#马赛克范围
for n in range(100,200):
# pixel -> 10*10
if m%10==0 and n%10==0: # 每隔十个像素的矩形框选取一个像素
for i in range(0,10):
for j in range(0,10):
(b,g,r)=img[m,n]
img[i+m,j+n]=(b,g,r)
cv2.imshow('dst',img)
cv2.waitKey(0)
四 毛玻璃
#毛玻璃
import cv2
import numpy as np
import random
img=cv2.imread('image.jpg',1) # 0 灰度图 1 彩色图
imginfo=img.shape
height=imginfo[0]
width=imginfo[1]
dst=np.zeros((height,width,3),np.uint8)
mn=8
for m in range(0,height-mn):
for n in range(0,width-mn):
index=int(random.random()*8) # 随机数 0-8
(b,g,r)=img[m+index,n+index]
dst[m,n]=(b,g,r)
cv2.imshow('dst',img)
cv2.waitKey(0)
五 图片融合
注意事项 两个图片的大小不能相差一倍
#图片融合
import cv2
import numpy as np
img0=cv2.imread('image0.jpg',1)
img1=cv2.imread('image1.jpg',1)
imginfo=img0.shape
height=imginfo[0]
width=imginfo[1]
# roi 敏感区域
roih=int(height/2)
roiw=int(width/2)
img0roi=img0[0:roih,0:roiw]
img1roi=img1[0:roih,0:roiw]
#dst
dst=np.zeros((roih,roiw,3),np.uint8)
dst=cv2.addWeighted(img0roi,0.5,img1roi,0.5,0) # addWeighted 权重相加 1 src1 2 a 3 src2 4 1-a
cv2.imshow('dst',dst)
cv2.waitKey(0)