图像课程,开发板-->淘宝店铺:胡狼FPGA
咨询微信:MyWork666888
QQ交流群:543928922
前言
一、基于直方图的算法
1.1直方图均衡化的增强算法
该算法通过对图像的直方图进行均衡化,使图像的灰度分布更加均匀,从而增强图像的亮度和对比度。但是该算法容易出现过度增强和噪声增加的问题。
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread("250114358f54e0bd46e0d0590d031e1.png")
B,G,R = cv2.split(img) #get single 8-bits channel
b=cv2.equalizeHist(B)
g=cv2.equalizeHist(G)
r=cv2.equalizeHist(R)
equal_img=cv2.merge((b,g,r)) #merge it back
hist_b=cv2.calcHist([equal_img],[0],None,[256],[0,256])
hist_B=cv2.calcHist([img],[0],None,[256],[0,256])
plt.subplot(1,2,1)
plt.plot(hist_B,'b')
plt.title('原图B通道的直方图',fontdict={'family':'KaiTi', 'size':10})
plt.subplot(1,2,2)
plt.title('均衡化后B通道的直方图',fontdict={'family':'KaiTi', 'size':10})
plt.plot(hist_b,'b')
plt.show()
cv2.imshow("orj",img)
cv2.imshow("equal_img",equal_img)
cv2.waitKey(0)
1.2直方图规定化的增强算法
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
img1 = np.array(Image.open('11.png'))
img2 = np.array(Image.open('13.png'))
plt.subplot(2,2,1)
plt.imshow(img1, cmap='gray')
plt.axis('off')
plt.title('原图像',fontdict={'family':'KaiTi', 'size':10})
plt.subplot(2,2,2)
plt.imshow(img2, cmap='gray')
plt.axis('off')
plt.title('参考图像',fontdict={'family':'KaiTi', 'size':10})
hist1, bins1 = np.histogram(img1.flatten(), 256, [0,256])
hist2, bins2 = np.histogram(img2.flatten(), 256, [0,256])
hist1 = hist1 / float(np.sum(hist1))
hist2 = hist2 / float(np.sum(hist2))
cdf1 = hist1.cumsum()
cdf2 = hist2.cumsum()
cdf1 = cdf1 / float(cdf1[-1])
cdf2 = cdf2 / float(cdf2[-1])
img3 = np.zeros_like(img1)
lut = np.interp(cdf1, cdf2, np.arange(0, 256))
for i in range(256):
img3[img1 == i] = lut[i]
plt.subplot(2,2,3)
plt.imshow(img3, cmap='gray')
plt.axis('off')
plt.title('规定化后的图像',fontdict={'family':'KaiTi', 'size':10})
plt.show()
二、基于图像变换的算法
import numpy as np
import matplotlib.pyplot as plt
import cv2
img = cv2.imread('11.png')
img = img.astype(np.float32) / 255.0
def gamma_correction(img, gamma):
return np.power(img, gamma)
gamma = 0.5
img_gamma = gamma_correction(img, gamma)
plt.subplot(1,2,1)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('原图像',fontdict={'family':'KaiTi', 'size':10})
plt.subplot(1,2,2)
plt.imshow(cv2.cvtColor(img_gamma, cv2.COLOR_BGR2RGB))
plt.title('伽马变换后图像',fontdict={'family':'KaiTi', 'size':10})
plt.show()
γ不同的选择直接影响着增强效果,所以很容易想到使用自适应伽马变换的方法
import cv2
import numpy as np
import matplotlib.pyplot as plt
def adaptive_gamma_correction(img, gamma=1.0, eps=1e-7):
img_float = img.astype(np.float32) / 255.0
img_max = np.max(img_float)
img_norm = img_float / img_max
img_log = np.log(img_norm + eps)
img_mean = np.exp(np.mean(img_log))
gamma_new = np.log(0.5) / np.log(img_mean)
gamma_corr = np.power(img_norm, gamma_new)
gamma_corr = np.uint8(gamma_corr * 255.0)
return gamma_corr
# 读取输入图片
img = cv2.imread('11.png')
# 进行自适应伽马校正
gamma_corr = adaptive_gamma_correction(img)
# 显示输入和输出图片
plt.subplot(121)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('原图像',fontdict={'family':'KaiTi', 'size':10})
plt.axis('off')
plt.subplot(122)
plt.imshow(gamma_corr, cmap='gray')
plt.title('自适应伽马变换后图像',fontdict={'family':'KaiTi', 'size':10})
plt.axis('off')
plt.show()
三、 基于Retinex理论的增强算法
3.1 单尺度Retinex算法 (SSR算法)
import cv2
import torch
from torchvision import datasets,transforms
import numpy as np
from PIL import Image, ImageFilter
# S = I * R
# S:输入图片
# I:光照量;需要对图像进行高斯模糊得到
# R:增强后图片
torch.set_default_tensor_type('torch.cuda.FloatTensor')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
transform = transforms.ToTensor()
GaussianBlur = transforms.GaussianBlur(3,sigma=(0.1,0.1)) #高斯模糊
to_pil = transforms.ToPILImage() #将Tensor转换成PIL格式
img_path = 'F:\Picture\低照度图片'
out_path = 'F:\SCI-main\传统增强算法\测试结果\SSR测试结果/'
img = datasets.ImageFolder(img_path,transform = transform)
def SSR(image):
img_S = image.cuda()
ln_S = torch.log((img_S + 0.001) / 255.0)
img_I = GaussianBlur(img_S)
ln_I = torch.log((img_I + 0.001) / 255.0)
ln_R = ln_S - ln_I * ln_S # 这里ln_S起到调节作用,不加的话效果很差
R = cv2.normalize(ln_R.cpu().numpy(), None, 0, 1, cv2.NORM_MINMAX)
final_image = to_pil(torch.tensor(R))
return final_image
for i in range(len(img.imgs)):
image = SSR(img[i][0])
image.save(out_path + str(i) + '.png')
ImageFolder使用方法:我们需要将所有图像按照文件夹保存,例如所有猫的图像放入到cat文件夹中,所有狗的图像放入到dog文件夹中,该函数就会自动识别类别,将图像所在的目录名称作为label。因此这里的输入图片路径结构如下:
3.2 多尺度Retinex算法(MSR算法)
import cv2
import torch
from torchvision import datasets,transforms
import numpy as np
from PIL import Image, ImageFilter
# S = I * R
# S:输入图片
# I:光照量;需要对图像进行高斯模糊得到
# R:增强后图片
torch.set_default_tensor_type('torch.cuda.FloatTensor')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
transform = transforms.ToTensor()
GaussianBlur1 = transforms.GaussianBlur(3,sigma=(15,15)) #高斯模糊
GaussianBlur2 = transforms.GaussianBlur(3,sigma=(80,80)) #高斯模糊
GaussianBlur3 = transforms.GaussianBlur(3,sigma=(200,200)) #高斯模糊
to_pil = transforms.ToPILImage() #将Tensor转换成PIL格式
img_path = 'F:\Picture\低照度图片'
out_path = 'F:\SCI-main\传统增强算法\测试结果\MSR测试结果/'
img = datasets.ImageFolder(img_path,transform = transform)
def SSR(image,GaussianBlur):
img_S = image.cuda()
ln_S = torch.log((img_S + 0.001) / 255.0)
img_I = GaussianBlur(img_S)
ln_I = torch.log((img_I + 0.001) / 255.0)
ln_R = ln_S - ln_I * ln_S # 这里ln_S起到调节作用,不加的话效果很差
R = cv2.normalize(ln_R.cpu().numpy(), None, 0, 1, cv2.NORM_MINMAX)
return R
def MSR(image):
img1 = SSR(image,GaussianBlur1)
img2 = SSR(image,GaussianBlur2)
img3 = SSR(image,GaussianBlur3)
img = (img1 + img2 + img3) / 3
final_image = to_pil(torch.tensor(img))
return final_image
for i in range(len(img.imgs)):
image = MSR(img[i][0])
image.save(out_path + str(i) + '.png')
3.3 MSRCR算法(Multi-Scale Retinex with Color Restoration)
使用数学表达式表示为:
代码:在前面的代码基础上,将输出R乘上系数即可。
import cv2
import torch
from torchvision import datasets,transforms
import numpy as np
from PIL import Image, ImageFilter
# S = I * R
# S:输入图片
# I:光照量;需要对图像进行高斯模糊得到
# R:增强后图片
torch.set_default_tensor_type('torch.cuda.FloatTensor')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
transform = transforms.ToTensor()
GaussianBlur1 = transforms.GaussianBlur(3,sigma=(15,15)) #高斯模糊
GaussianBlur2 = transforms.GaussianBlur(3,sigma=(80,80)) #高斯模糊
GaussianBlur3 = transforms.GaussianBlur(3,sigma=(200,200)) #高斯模糊
to_pil = transforms.ToPILImage() #将Tensor转换成PIL格式
img_path = 'F:\Picture\低照度图片'
out_path = 'F:\SCI-main\传统增强算法\测试结果\MSRCR测试结果/'
img = datasets.ImageFolder(img_path,transform = transform)
def SSR(image,GaussianBlur):
img_S = image.cuda()
ln_S = torch.log((img_S + 0.001) / 255.0)
img_I = GaussianBlur(img_S)
ln_I = torch.log((img_I + 0.001) / 255.0)
ln_R = ln_S - ln_I * ln_S # 这里ln_S起到调节作用,不加的话效果很差
R = cv2.normalize(ln_R.cpu().numpy(), None, 0, 1, cv2.NORM_MINMAX)
return R
def MSR(image):
img1 = SSR(image,GaussianBlur1)
img2 = SSR(image,GaussianBlur2)
img3 = SSR(image,GaussianBlur3)
img = (img1 + img2 + img3) / 3
# final_image = to_pil(torch.tensor(img))
# return final_image
return torch.tensor(img)
def MSRCR(image,alpha = 0.225,lambd = 0.2):
img = MSR(image)
image_sum = torch.sum(image.cuda())
for i in range(3):
img[i,:,:] = alpha * torch.log10((lambd * image[i,:,:].cuda())/image_sum) * img[i,:,:]
final_image = to_pil(torch.tensor(img))
return final_image
for i in range(len(img.imgs)):
image = MSRCR(img[i][0])
image.save(out_path + str(i) + '.png')
总结
*******往期精彩文章列表********
基于Zynq的图像处理入门课程