美文网首页
数字图像处理二python实现

数字图像处理二python实现

作者: 幺姨母 | 来源:发表于2020-03-31 01:49 被阅读0次

理论知识

图像反转

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

相关文章

  • OpenCV+Python 频域分析

    参考: opencv-python官方文档《刚萨雷斯数字图像处理(MATLAB版)》 图像处理中的傅里叶变换 二维...

  • python skimage图像处理(一)

    本文转自 python数字图像处理 基于python脚本语言开发的数字图片处理包,比如PIL,Pillow, op...

  • 《数字图像处理与分析》学习笔记 1

    第一章:图像处理的基本知识 1.1数字图像处理概述 【1、数字图像处理及其特点】 1.图像与数字图像 图像是自然界...

  • 数字图像处理(二) 数字图像处理基础

      本节主要目的是介绍本书所用到的数字图像处理的一些基本概念。来源于东北大学 魏颖教授的数字图像课程笔记。 图像的...

  • 教你如何用python数字图像处理做图像滤波效果!

    本文主要介绍python数字图像处理,图像简单滤波,图文并茂介绍skimage库中通过filters模块进行滤波操...

  • 数字图像处理-二

    变换域处理方法 一、主成分分析二、最小噪声分离三、缨帽变换线性变换 四、傅里叶变换五、小波变换频率域变换 六、颜色...

  • 开篇

    最近数字图像处理老师布置了结课作业,需要做一个图形化的软件处理图像。可以采用MATLAB或者Python。我最近一...

  • python skimage图像处理(三)

    本文转自 python数字图像处理 霍夫线变换 在图片处理中,霍夫变换主要是用来检测图片中的几何形状,包括直线、圆...

  • 图像处理常用术语

    基本术语 digital image:数字图像digital image processing:数字图像处理ima...

  • Python下的图像处理库,你选哪个?

    在进行数字图像处理时,我们经常需要对图像进行读取、保存、缩放、裁剪、旋转、颜色转换等基本操作。在使用python进...

网友评论

      本文标题:数字图像处理二python实现

      本文链接:https://www.haomeiwen.com/subject/kjwnuhtx.html