数字图像处理二python实现

理论知识

图像反转

import cv2
import numpy as np
import matplotlib as plt

def reverseImg(imgPath):
    # 读取图像,cv2以BGR读取彩色图像
    img = cv2.imread(imgPath)

    # 转换成灰度图像
    greyImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 得到结果图像
    res = np.uint8(255 - greyImg)
    
    return (greyImg, res)

greyImg, res = reverseImg("cat.JPG")

# 显示图像
cv2.imshow("greyImg", greyImg)
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 保存图像
cv2.imwrite("catGreyImg.jpg", greyImg)
cv2.imwrite("catReverseImg.jpg", res)
cat.JPG
catReverseImg.jpg

对数变换

import cv2
import numpy as np
import matplotlib as plt

def logImg(c, filePath):
    img = cv2.imread(filePath)
    res = np.uint8(c * np.log(1.0 + img))
    return res

res = logImg(40, "cat.JPG")

cv2.imshow("original", cv2.imread("cat.JPG"))
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.imwrite("catLogImg.jpg", res)
catLogImg.jpg

伽马变换

import cv2
import numpy as np
import matplotlib as plt

def gammaImg(c, gamma, filePath):
    img = cv2.imread(filePath)
    greyImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    res = np.uint8(c * greyImg ** gamma)
    return res

res = gammaImg(1.0, 0.9, "cat.JPG")

cv2.imshow("greyImg", cv2.imread("catGreyImg.jpg"))
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.imwrite("catGammaImg.jpg", res)
catGammaImg.jpg

直方图均衡

# J(r,c)= 255⋅P [I(r,c)+1]
# 这里用cv2提供的函数实现

import cv2
import numpy as np
import matplotlib as plt
    
def histogramEqualizationImg(filePath):
    img = cv2.imread(filePath)
    b,g,r = cv2.split(img)
    bh = cv2.equalizeHist(b)
    gh = cv2.equalizeHist(g)
    rh = cv2.equalizeHist(r)
    
    # 合并
    res = cv2.merge((bh,gh,rh)) # 注意这里的括号
    return res

res = histogramEqualizationImg("cat.JPG")

cv2.imshow("original", cv2.imread("cat.JPG"))
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.imwrite("catHistImg.jpg", res)
catHistImg.jpg

直方图匹配

import cv2
import numpy
import matplotlib as plt

"""
统计一幅图像的直方图。
cv2.calcHist(images, channels, mask, histSize, ranges, [,hist,[,accumulate]])
images: 原图像 uint8 float32 [img]
channels: 需要使用中括号括起来,它会告诉函数我们要统计哪副图像的直方图。灰度图:[0] 彩色图像的色彩通道:[0],[1],[2]
mask:掩模图像 要统计整幅图像的直方图就设置为 None
histSize: BIN的数目 [256], BINS:如果想知道某个灰度范围内像素点的数目,就可以将[0, 256]进行分组,取每组的总和,每一个小组称为 BIN
ranges: 像素值的范围 通常为[0, 256]
"""

def histMatchOneChannel(inChannel, matchChannel):
    # 和单通道的np.histogram(inImg.ravel(), 256, [0, 256])效果相同
    # 但openCV比numpy的快很多
    
    # 计算两个图的直方图、cdf
    histIn = cv2.calcHist([inChannel], [0], None, [256], [0, 256])
    cdfIn = np.cumsum(histIn)
    histMatch = cv2.calcHist([matchChannel], [0], None, [256], [0, 256])
    cdfMatch = np.cumsum(histMatch)

    # 匹配
    matchMethod = np.zeros(256)
    greyIn = 0
    greyMatch = 0
    while greyIn < 256 and greyMatch < 256:
        if cdfIn[greyIn] < cdfMatch[greyMatch]:
            matchMethod[greyIn] = greyMatch
            greyIn = greyIn + 1
        elif cdfIn[greyIn] == cdfMatch[greyMatch]:
            matchMethod[greyIn] = greyMatch
            greyIn = greyIn + 1
            greyMatch = greyMatch + 1
        else:
            greyMatch = greyMatch + 1
    
    resChannel = matchMethod[inChannel]
    return resChannel
    
def histMatch(inFilePath, matchFilePath):
    inImg = cv2.imread(inFilePath)
    matchImg = cv2.imread(matchFilePath)
    
    b,g,r = cv2.split(inImg)
    bm,gm,rm = cv2.split(matchImg) 
    resb = histMatchOneChannel(b,bm)
    resg = histMatchOneChannel(g,gm)
    resr = histMatchOneChannel(r,rm)
    res = cv2.merge((resb,resg,resr))
    return res

res = histMatch("cat.JPG", "treecat.JPG") 
cv2.imshow("original", cv2.imread("cat.JPG"))
cv2.imshow("match", cv2.imread("treecat.JPG"))
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("catHistMatchTreecatImg.jpg", res)
cat.JPG
treecat.JPG
catHistMatchTreecatImg.jpg

你可能感兴趣的:(数字图像处理二python实现)