opencv图像美化

直方图均衡化-灰度

import cv2
import numpy as np
img = cv2.imread('image2.jpg',1)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # 图片灰度化
cv2.imshow('gray',gray)
cv2.imwrite('gray2.jpg',gray)
hist = cv2.equalizeHist(gray) # api 完成直方图均衡化
cv2.imshow('hist',hist)
cv2.imwrite('hist2.jpg',hist)
cv2.waitKey(0)

直方图均衡化-彩色

import cv2
import numpy as np

img = cv2.imread('image2.jpg',1)
cv2.imshow('src',img)
(b,g,r) = cv2.split(img) # 通道分解
# 图片单通道处理
bH = cv2.equalizeHist(b)
gH = cv2.equalizeHist(g)
rH = cv2.equalizeHist(r)
result = cv2.merge((bH,gH,rH))# 通道合成
cv2.imshow('dst',result)
cv2.imwrite('dst2.jpg',result)
cv2.waitKey(0)

直方图均衡化-YUV

import cv2
import numpy as np
img = cv2.imread('image2.jpg',1)
imgYUV = cv2.cvtColor(img,cv2.COLOR_BGR2YCrCb) #
cv2.imshow('src',img)
channelYUV = cv2.split(imgYUV) # 图片分解
channelYUV[0] = cv2.equalizeHist(channelYUV[0]) # 直方图均衡化
channels = cv2.merge(channelYUV) # 合成
result = cv2.cvtColor(channels,cv2.COLOR_YCrCb2BGR)
cv2.imshow('result2',result)
cv2.imwrite('result2.jpg',result)
cv2.waitKey(0)

图片修补

import cv2
import numpy as np

#制作一张损坏的图片

img = cv2.imread('image2.jpg',1) # 读取图片文件, 1:彩色, 0:灰色
for i in range(1,100): # 总共一百个像素点
img[500+i,500] = (255,255,255) # 写入标准的白色
cv2.imwrite('damaged.jpg',img)

#修补损坏的图片

img = cv2.imread('damaged.jpg',1)
cv2.imshow('src',img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
paint = np.zeros((height,width,1),np.uint8)
# 描绘图片坏的数组;进行修补
for i in range(500,600):
paint[i,500] = 255
cv2.imshow('paint',paint)
#1 src 2 mask
imgDst = cv2.inpaint(img,paint,3,cv2.INPAINT_TELEA)

cv2.imshow('image',imgDst)
cv2.imwrite('imgDst.jpg',imgDst)
cv2.waitKey(0)

亮度增强

import cv2
import numpy as np
img = cv2.imread('image2.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
cv2.imshow('src',img)
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]
bb = int(b)+40
gg = int(g)+40
rr = int(r)+40
if bb>255:
bb = 255
if gg>255:
gg = 255
if rr>255:
rr = 255
dst[i,j] = (bb,gg,rr)
cv2.imshow('dst3',dst)
cv2.imwrite('dst3.jpg',dst)

高斯滤波

import cv2
import numpy as np
img = cv2.imread('image1.jpg',1)
cv2.imshow('src',img)
dst = cv2.GaussianBlur(img,(5,5),1.5)
cv2.imwrite('dst4.jpg',dst)
cv2.imshow('dst4',dst)
cv2.waitKey(0)

均值滤波

# 均值 6*6 1 。 * 【6*6】/36 = mean -》P
import cv2
import numpy as np

img = cv2.imread('image1.jpg', 1)
#cv2.imshow('src', img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dst = np.zeros((height, width, 3), np.uint8)
for i in range(3, height - 3):
for j in range(3, width - 3):
sum_b = int(0)
sum_g = int(0)
sum_r = int(0)
for m in range(-3, 3): # -3 -2 -1 0 1 2
for n in range(-3, 3):
(b, g, r) = img[i + m, j + n]
sum_b = sum_b + int(b)
sum_g = sum_g + int(g)
sum_r = sum_r + int(r)

b = np.uint8(sum_b / 36)
g = np.uint8(sum_g / 36)
r = np.uint8(sum_r / 36)
dst[i, j] = (b, g, r)
cv2.imshow('dst5', dst)
cv2.imwrite('dst5.jpg', dst)

中值滤波

import cv2
import numpy as np
img = cv2.imread('image1.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
img = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
#cv2.imshow('src',img)
cv2.imwrite('src6.jpg',img)
dst = np.zeros((height,width,3),np.uint8)
collect = np.zeros(9,np.uint8)
for i in range(1,height-1):
for j in range(1,width-1):
k = 0
for m in range(-1,2):
for n in range(-1,2):
gray = img[i+m,j+n]
collect[k] = gray
k = k+1
# 0 1 2 3 4 5 6 7 8
# 1
for k in range(0,9):
p1 = collect[k]
for t in range(k+1,9):
if p1<collect[t]:
mid = collect[t]
collect[t] = p1
p1 = mid
dst[i,j] = collect[4]
#cv2.imshow('dst',dst)
cv2.imwrite('dst6.jpg',dst)

皮肤磨皮美白-双边滤波

import cv2
img = cv2.imread('image3.png',1)
#cv2.imshow('src',img)
dst = cv2.bilateralFilter(img,15,35,35) # 滤波函数
#cv2.imshow('dst',dst)
cv2.imwrite('dst7.png',dst)
------ 本文结束 🎉🎉 谢谢观看 ------
0%