图像美画
Posted stling
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图像美画相关的知识,希望对你有一定的参考价值。
彩色图片直方图
#三色通道分离 import cv2 import numpy as np # 1 image 2 [0] 3 mask None 4 256 5 0-255 hist = cv2.calcHist([image],[0],None,[256],[0.0,255.0]) cv2.calcHist(images, channels, mask, histSize, ranges, hist=None, accumulate=None) # minMaxLoc(src[, mask]) -> minVal, maxVal, minLoc, maxLoc minV,maxV,minL,maxL = cv2.minMaxLoc(hist) for h in range(256): intenNormal = int(hist[h]*256/maxV) cv2.line(histImg,(h,256),(h,256-intenNormal),color)
# 源码实现
# 统计每一个色阶出现的概率 使用matplotlib作图
均衡化
#灰度 cv2.equalizeHist() #equalizeHist(src[, dst]) -> dst #彩色 三通道分离 #分别均衡化 cv2.equalizeHist()
# 源码实现
# 本质:统计每个像素灰度 出现的概率 0-255 count[i] = count[i]/(height*width)
# 计算累计概率 sum = sum+count[i] count[i] = sum1
# 计算映射表 map1[i] = np.uint16(count[i]*255)
import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread(‘image0.jpg‘,1) imgInfo = img.shape height = imgInfo[0] width = imgInfo[1] gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) cv2.imshow(‘src‘,gray) count = np.zeros(256,np.float) for i in range(0,height): for j in range(0,width): pixel = gray[i,j] index = int(pixel) count[index] = count[index]+1 for i in range(0,255): count[i] = count[i]/(height*width) #计算累计概率 sum1 = float(0) for i in range(0,256): sum1 = sum1+count[i] count[i] = sum1 #print(count) # 计算映射表 map1 = np.zeros(256,np.uint16) for i in range(0,256): map1[i] = np.uint16(count[i]*255) # 映射 for i in range(0,height): for j in range(0,width): pixel = gray[i,j] gray[i,j] = map1[pixel] cv2.imshow(‘dst‘,gray) cv2.waitKey(0)
YUV 直方图均衡化
cv2.cvtColor(img,cv2.COLOR_BGR2YCrCb) channelYUV = cv2.split(imgYUV) channelYUV[0] = cv2.equalizeHist(channelYUV[0]) channels = cv2.merge(channelYUV) result = cv2.cvtColor(channels,cv2.COLOR_YCrCb2BGR)
修补
cv2.inpaint(img,paint,3,cv2.INPAINT_TELEA) # inpaint(src, inpaintMask, inpaintRadius, flags[, dst]) -> dst
亮度增强
加像素值
超过255按255计
磨皮美白
#双边滤波 cv2.bilateralFilter(img,15,35,35)
高斯滤波
cv2.GaussianBlur(img,(5,5),1.5)
# 源码
import cv2 import numpy as np img = cv2.imread(‘image11.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(‘dst‘,dst) cv2.waitKey(0)
中值滤波
import cv2 import numpy as np img = cv2.imread(‘image11.jpg‘,1) imgInfo = img.shape height = imgInfo[0] width = imgInfo[1] img = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) cv2.imshow(‘src‘,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.waitKey(0)
以上是关于图像美画的主要内容,如果未能解决你的问题,请参考以下文章