图像增强算法

图像增强

图像增强算法概述

图像增强的目的是改善图像的视觉效果或使图像更适合于人或机器的分析处理。通过图像增强可以减少图像噪声,提高目标与背景的对比度,亦可以强调或抑制图像中的某些细节。例如,消除照片中的划痕,改善光照不均匀的图像,突出目标的边缘等。根据处理的空间可以将图像增强分为空域法和频域法,前者直接在图像的空间域(或图像空间)中对像素进行处理,后者在图像的变换域(即频域)内简介处理,然后经逆变换获得增强图像。空域增强可以分为点处理和区处理,频域增强可以分为低通滤波,高通滤波,带通滤波和同态滤波。本文主要介绍的是空域法。

直方图均衡化和规定化

直方图均衡化基本思想

直方图均衡化的基本思想是把原始图像的直方图变换为均匀分布的形式,从而增强图像灰度的变化范围,以达到增强图像对比度的效果。经过均衡化处理的图像,其灰度级出现的概率相同,此时图像的熵最大,图像所包含的信息量最大。
均衡化的详细原理

直方图规定化基本思想

直方图均衡化能够增强整个图像的对比度,但增强效果不易控制,处理得到的是全局均衡化的直方图。然后实际应用中可能希望将直方图变换为某个特定的形状(规定的直方图),从而有选择地增强某个灰度范围内的对比度,这种方法就称为直方图规定化。直方图规定化可以借助直方图均衡化来实现。

任意两幅灰度图像,显示它们的直方图,并将其中一幅的直方图传递给另一个图像,显示传递后的图像及其直方图。

python代码实现
实验内容

任意两幅灰度图像,显示它们的直方图,并将其中一幅的直方图传递给另一个图像,显示传递后的图像及其直方图。

直方图概率密度
def histNormalCalculate(src):
    m,n = np.shape(src)
    #直方图
    hist = np.zeros(256,dtype=np.float32)

    for i in range(m):
        for j in range(n):
            hist[src[i,j]] += 1
    return hist/(m*n)
直方图累计概率
#计算直方图累计概率
def histCalculate(src):
    m,n = np.shape(src)
    #直方图
    hist = np.zeros(256,dtype=np.float32)
    #累积直方图
    cumhist = np.zeros(256,dtype=np.float32)
    #累积直方图概率
    cumhistPro = np.zeros(256,dtype=np.float32)

    for i in range(m):
        for j in range(n):
            hist[src[i,j]] += 1
    cumhist[0] = hist[0]
    for i in range(1,256):
        cumhist[i] = cumhist[i-1]+hist[i]
    cumhistPro = cumhist/(m*n)

    return cumhistPro
直方图规定化
#直方图规定化
def histNormalize(img1,img2):
    #计算带匹配直方图
    img1Pro = histCalculate(img1)
    #计算参考直方图
    img2Pro = histCalculate(img2)

    correspondValue = np.zeros(256,dtype=np.uint8)
    #直方图规定化
    for i in range(256):
        diff = np.abs(img1Pro[i]-img2Pro[i])
        matchValue = i
        for j in range(256):
            if np.abs(img1Pro[i] - img2Pro[j]) < diff:
                diff = np.abs(img1Pro[i]-img2Pro[j])
                matchValue = j
        correspondValue[i] = matchValue
    imgOut = cv2.LUT(img1,correspondValue)
    return imgOut
结果绘图
img1 = cv2.imread('1.jpg',0)
img2 = cv2.imread('2.jpg',0)
#img2 = cv2.resize(img2,(img1.shape[1],img1.shape[0]))
#print(img1.shape,img2.shape)
plt.subplot(2,3,1)
plt.imshow(img1,cmap='gray')
plt.title('img1'),plt.xticks([]),plt.yticks([])
plt.subplot(2,3,2)
plt.imshow(img2,cmap='gray')
plt.title('img2'),plt.xticks([]),plt.yticks([])

imgOut = histNormalize(img1,img2)
plt.subplot(2,3,3)
plt.imshow(imgOut,cmap='gray')
plt.title('imgOut'),plt.xticks([]),plt.yticks([])

hist1 = histNormalCalculate(img1)
plt.subplot(2,3,4)
plt.plot(hist1,'r')
plt.title('img1 hist ratio')

hist2 = histNormalCalculate(img2)
plt.subplot(2,3,5)
plt.plot(hist2,'b')
plt.title('img2 hist ratio')

hist3 = histNormalCalculate(imgOut)
plt.subplot(2,3,6)
plt.plot(hist3,'g')
plt.title('imgOut hist ratio')
plt.show()
结果展示

res

图像锐化

图像锐化原理

图像锐化的目的是使模糊的图像变清晰,增强图像的边缘等细节。图像锐化增强边缘的同时会增强噪声,因此一般先去除噪声或减轻噪声,再进行锐化处理。图像锐化可以在空间域或者频率域通过高通滤波实现,即减轻或消除低频分量而不影响高频分量。空间域高通滤波主要用模板卷积来实现。
图像锐化的方法主要有拉普拉斯算子,LOG算子,Sobel算子等,具体的原理可以见博客这里通过一个实验来运用相关的算法。

python代码实现
实验内容

用多种图像增强方法完成如下人体骨骼核扫描图的增强任务:

  • a原图;
  • b图a的拉普拉斯变换;
  • c图(a)和图(b)相加得到的锐化图像;
  • d图(a)的Sobel处理;
  • e用5X5的均值滤波平滑的Sobel图像;
  • f由©和(e)相乘形成的掩蔽图像;
  • g由(a)和(f)求和得到的锐化图像;
  • h对(g)应用幂律变换得到的最后图像。
代码
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('gu.jpg',0)

laplacian = cv2.Laplacian(img,cv2.CV_64F)
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)

sobelx = np.uint8(np.absolute(sobelx))
sobely = np.uint8(np.absolute(sobely))
sobelCom = cv2.bitwise_or(sobelx,sobely)

C = img+laplacian
E = cv2.blur(sobelCom,(5,5))
F = C*E

mina = np.min(F)
maxa = np.max(F)
#print(mina,maxa)
F = np.uint8(255*(F-mina)/(maxa-mina))
G = img+F
H = cv2.pow(G/255.0,0.5)

plt.subplot(2,4,1),plt.imshow(img,cmap='gray')
plt.title('A = original'),plt.xticks([]),plt.yticks([])

plt.subplot(2,4,2),plt.imshow(laplacian,cmap='gray')
plt.title('B = laplacian'),plt.xticks([]),plt.yticks([])

plt.subplot(2,4,3),plt.imshow(C,cmap='gray')
plt.title('C = add a and b'),plt.xticks([]),plt.yticks([])

plt.subplot(2,4,4),plt.imshow(sobelCom,cmap='gray')
plt.title('D = sobel'),plt.xticks([]),plt.yticks([])

plt.subplot(2,4,5),plt.imshow(E,cmap='gray')
plt.title('E = blur sobel'),plt.xticks([]),plt.yticks([])

plt.subplot(2,4,6),plt.imshow(F,cmap='gray')
plt.title('F = C*E'),plt.xticks([]),plt.yticks([])

plt.subplot(2,4,7),plt.imshow(G,cmap='gray')
plt.title('G = add a and f'),plt.xticks([]),plt.yticks([])

plt.subplot(2,4,8),plt.imshow(H,cmap='gray')
plt.title('H = mi lv'),plt.xticks([]),plt.yticks([])

plt.show()
实验结果

在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值