opencv图像美化

绘制图像直方图

import cv2
import numpy as np
def ImageHist(image, type):
    color = (255,255,255)
    windowName = "Gray"
    if type == 31:
        color = (255,0,0)
        windowName = "B Hist"
    elif type == 32:
        color = (0,255,0)
        windowName = "G Hist"
    elif type == 33:
        color = (0,0,255)
        windowName = "R Hist"
    # 1图像 2直方图通道 3mask模板(用来指定需要处理的区域和不与要处理的区域) 
    # 4直方图size即256个像素值 5像素值范围0-255
    #hist为一维数组,长度256,代表图片中0-255像素值的像素点个数
    hist = cv2.calcHist([image], [0], None, [256], [0,255])
    minV, maxV, minL, maxL = cv2.minMaxLoc(hist)
    histImg = np.zeros([256,256,3],np.uint8)
    for h in range(256):
        #将像素点个数归一化后再缩放到0-255
        intenNormal = int(hist[h]/maxV*255)
        cv2.line(histImg, (h,255), (h, 255-intenNormal), color)
    cv2.imshow(windowName, histImg)
    return histImg

img = cv2.imread("E:\\code\\conputer_visual\\data\\0.jpg", 1)
#将彩色图像分割出b,g,r三张图像
channels = cv2.split(img)
#遍历绘制三张纯色图像的直方图
for i in range(3):
    ImageHist(channels[i], 31+i)
cv2.waitKey()

在这里插入图片描述

matplot绘制灰度图直方图

import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("E:\\code\\conputer_visual\\data\\01.jpg", 1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
count = np.zeros(256,np.float)
for i in range(height):
    for j in range(width):
        pixel = gray[i,j]
        index = int(pixel)
        count[index] = count[index] + 1
for i in range(256):
    count[i] = count[i]/(height*width)
x = np.linspace(0,255,256)
y = count
plt.bar(x, y, 0.9, alpha=1, color="b")
plt.show()
cv2.waitKey()

在这里插入图片描述

matplot绘制彩色图直方图

import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("E:\\code\\conputer_visual\\data\\01.jpg", 1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]

count_b = np.zeros(256, np.float)
count_g = np.zeros(256, np.float)
count_r = np.zeros(256, np.float)
for i in range(height):
    for j in range(width):
        (b,g,r) = img[i,j]
        index_b = int(b)
        index_g = int(g)
        index_r = int(r)
        count_b[index_b] += 1
        count_g[index_g] += 1
        count_r[index_r] += 1
for i in range(256):
    count_b[i] = count_b[i]/(height*width)
    count_g[i] = count_g[i]/(height*width)
    count_r[i] = count_r[i]/(height*width)
x = np.linspace(0,255,256)
y1 = count_b
y2 = count_g
y3 = count_r
plt.figure()
plt.bar(x, y1, 0.9, alpha=1, color="b")
plt.figure()
plt.bar(x, y2, 0.9, alpha=1, color="g")
plt.figure()
plt.bar(x, y3, 0.9, alpha=1, color="r")
plt.show()

在这里插入图片描述

灰度图像直方图均衡化

  • opencv.equalizeHist实现
import cv2
import numpy as np
img = cv2.imread("E:\\code\\conputer_visual\\data\\0.jpg", 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("src", gray)
dst = cv2.equalizeHist(gray)
cv2.imshow("dst", dst)
cv2.waitKey()

在这里插入图片描述

  • opencv实现
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("E:\\code\\conputer_visual\\data\\01.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(height):
    for j in range(width):
        pixel = gray[i,j]
        index = int(pixel)
        count[index] = count[index] + 1
for i in range(256):
    count[i] = count[i]/(height*width)
#计算累计概率
sum1 = float(0)
for i in range(256):
    sum1 = sum1 + count[i]
    count[i] = sum1
#建立映射表
map1 = np.zeros(256, np.uint16)
for i in range(256):
    map1[i] = np.uint16(count[i]*255)
#映射
for i in range(height):
    for j in range(width):
        pixel = gray[i,j]
        gray[i,j] = map1[pixel]
cv2.imshow("dst", gray)
cv2.waitKey()

在这里插入图片描述

  • principle
    统计出0-255个像素值出现的概率,计算出累计概率。然后renew原图中的像素值为 原像素值*对应累计概率值

彩色图像直方图均衡化

  • opencv.equalizeHist实现
#彩色图均衡化
import cv2
import numpy as np
img = cv2.imread("E:\\code\\conputer_visual\\data\\0.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.waitKey()

在这里插入图片描述

  • opencv实现
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("E:\\code\\conputer_visual\\data\\01.jpg", 1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]

count_b = np.zeros(256, np.float)
count_g = np.zeros(256, np.float)
count_r = np.zeros(256, np.float)
for i in range(height):
    for j in range(width):
        (b,g,r) = img[i,j]
        index_b = int(b)
        index_g = int(g)
        index_r = int(r)
        count_b[index_b] += 1
        count_g[index_g] += 1
        count_r[index_r] += 1
for i in range(256):
    count_b[i] = count_b[i]/(height*width)
    count_g[i] = count_g[i]/(height*width)
    count_r[i] = count_r[i]/(height*width)
#计算累计概率
sum_b = float(0)
sum_g = float(0)
sum_r = float(0)
for i in range(256):
    sum_b = sum_b + count_b[i]
    sum_g = sum_g + count_b[i]
    sum_r = sum_r + count_b[i]
    count_b[i] = sum_b
    count_g[i] = sum_g
    count_r[i] = sum_r
#建立映射表
mapb = np.zeros(256, np.uint16)
mapg = np.zeros(256, np.uint16)
mapr = np.zeros(256, np.uint16)
for i in range(256):
    mapb[i] = np.uint16(count_b[i]*255)
    mapg[i] = np.uint16(count_g[i]*255)
    mapr[i] = np.uint16(count_r[i]*255)
#映射
dst = np.zeros((height,width,3), np.uint8)
for i in range(height):
    for j in range(width):
        (b,g,r) = img[i,j]
        b = mapb[b]
        g = mapg[g]
        r = mapr[r]
        dst[i,j] = (b,g,r)
cv2.imshow("src", img)
cv2.imshow("dst",  dst)
cv2.waitKey()

在这里插入图片描述

YUV直方图均衡化

#YUV直方图均衡化
import cv2
import numpy as np
img = cv2.imread("E:\\code\\conputer_visual\\data\\0.jpg", 1)
imgYUV = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
cv2.imshow("src", imgYUV)
channelYUV = cv2.split(imgYUV)
channelYUV[0] = cv2.equalizeHist(channelYUV[0])
channels = cv2.merge(channelYUV)
result = cv2.cvtColor(channels, cv2.COLOR_YCrCb2BGR)
cv2.imshow("dst", result)
cv2.waitKey()

在这里插入图片描述

图像修复

  • 图像破坏
import cv2
import numpy as np
img = cv2.imread("E:\\code\\conputer_visual\\data\\0.jpg", 1)
for i in range(110, 150):
    img[i,140] = (255,255,255)
    img[i,140+1] = (255,255,255)
    img[i,140-1] = (255,255,255)
for i in range(120,160):
    img[130, i] = (255,255,255)
    img[130+1, i] = (255,255,255)
    img[130-1, i] = (255,255,255)
cv2.imwrite("E:\\code\\conputer_visual\\data\\damaged.jpg", img)
cv2.imshow("image", img)
cv2.waitKey()

在这里插入图片描述

  • 图像修复
import cv2
import numpy as np
img = cv2.imread("E:\\code\\conputer_visual\\data\\damaged.jpg", 1)
cv2.imshow("src", img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
#paint用于标记破坏的区域
paint = np.zeros((height, width, 1), np.uint8)
for i in range(110, 150):
    paint[i,140] = 255
    paint[i,140+1] = 255
    paint[i,140-1] = 255
for i in range(120,160):
    paint[130, i] = 255
    paint[130+1, i] = 255
    paint[130-1, i] = 255
cv2.imshow("paint", paint)
#parameter3为修复像素点的领域半径为几个像素
imgDst = cv2.inpaint(img, paint, 3, cv2.INPAINT_TELEA)
cv2.imshow("dst", imgDst)
cv2.waitKey()

在这里插入图片描述

图像亮度增强

import cv2
import numpy as np
img = cv2.imread("E:\\code\\conputer_visual\\data\\01.jpg", 1)
imgInfo = img.shape
height, width = imgInfo[0], imgInfo[1]
cv2.imshow("src", img)
dst = np.zeros((height,width,3), np.uint8)
for i in range(height):
    for j in range(width):
        (b,g,r) = img[i,j]
        bb = int(b*1.2) + 10
        gg = int(g*1.3) + 15
        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("dst", dst)
cv2.waitKey()

在这里插入图片描述

磨皮美白

import cv2
img = cv2.imread("E:\\code\\conputer_visual\\data\\hand skin.jpg", 1)
dst = cv2.bilateralFilter(img, 15, 35, 35)
cv2.imshow("src", img)
cv2.imshow("dst", dst)
cv2.waitKey()

在这里插入图片描述

高斯滤波降噪

import cv2
import numpy as np
img = cv2.imread("E:\\code\\conputer_visual\\data\\001dots.png", 1)
cv2.imshow("src", img)
dst = cv2.GaussianBlur(img, (5,5), 1.5)
cv2.imshow("dst", dst)
cv2.waitKey()

在这里插入图片描述

均值滤波降噪

import cv2
import numpy as np
img = cv2.imread("E:\\code\\conputer_visual\\data\\001dots.png", 1)
cv2.imshow("src", img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dts = 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):
            for n in range(-3,3):
                (b,g,r) = img[i+m,j+n]
                sum_b += int(b)
                sum_g += int(g)
                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()

在这里插入图片描述

  • 原理
    将每个像素点的像素值更新为该像素点所在的6*6像素阵的像素均值

中值滤波降噪

import cv2
import numpy as np
img = cv2.imread("E:\\code\\conputer_visual\\data\\001dots.png", 1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("src", img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dst = np.zeros((height,width,1), 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 += 1
        collect = np.sort(collect)
        dst[i,j] = collect[4]
cv2.imshow("dst", dst)
cv2.waitKey()

在这里插入图片描述

  • 原理
    与均值滤波类似,将每个像素点的像素值更新为所在3*3像素阵的像素中值(排序后)

猜你喜欢

转载自blog.csdn.net/cyj5201314/article/details/114678157