直方圖均衡化
直方圖均衡化是通過調(diào)整圖像的灰階分布,使得在0~255灰階上的分布更加均衡,提高了圖像的對比度,達到改善圖像主觀視覺效果的目的。對比度較低的圖像適合使用直方圖均衡化方法來增強圖像細節(jié)
#encoding:utf-8
#
#彩色圖像直方圖
#
from matplotlib import pyplot as plt
import numpy as np
import cv2
src = cv2.imread(r"C:\Users\admin\Desktop\deblur\blur\blur1.jpg")
shape = src.shape
height = shape[0]
weight = shape[1]
# cv2.namedWindow('Original', cv2.WINDOW_NORMAL)
# cv2.resizeWindow('Original', int(weight/4), int(height/4))
# cv2.imshow("Original",src)
# cv2.waitKey(0)
chans = cv2.split(src)
colors = ("b","g","r")
plt.figure()
plt.title("Flattened Color Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
for (chan,color) in zip(chans,colors):
# images,channels,mask,histSize(num_bin),ranges(pix_ranges)
hist = cv2.calcHist([chan],[0],None,[256],[0,256])
plt.plot(hist,color = color)
plt.xlim([0,256])
# plt.show()
plt.savefig(r'C:\Users\admin\Desktop\deblur\blur\hist.jpg')
#
# 灰度圖像直方圖均衡化
#
image = cv2.imread(r"C:\Users\admin\Desktop\deblur\blur\blur1.jpg",0)#讀取灰度圖像
eq = cv2.equalizeHist(image)#灰度圖像直方圖均衡化
# cv2.namedWindow('Histogram Equalization', cv2.WINDOW_NORMAL)
# cv2.resizeWindow('Histogram Equalization', int(weight/2), int(height/4))
# np.hstack() 在水平方向上平鋪
# cv2.imshow("Histogram Equalization", np.hstack([image, eq]))
# cv2.waitKey(0)
#
# 彩色圖像直方圖均衡化
#
chans[0] = cv2.equalizeHist(chans[0])
chans[1] = cv2.equalizeHist(chans[1])
chans[2] = cv2.equalizeHist(chans[2])
eh = cv2.merge(chans)
cv2.namedWindow('Color Histogram Equalization', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Color Histogram Equalization', int(weight/2), int(height/4))
cv2.imshow("Color Histogram Equalization", np.hstack([src, eh]))
cv2.waitKey(0)
plt.figure()
plt.title("EH Flattened Color Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
for (chan,color) in zip(chans,colors):
# images,channels,mask,histSize(num_bin),ranges(pix_ranges)
hist = cv2.calcHist([chan],[0],None,[256],[0,256])
plt.plot(hist,color = color)
plt.xlim([0,256])
# plt.show()
plt.savefig(r'C:\Users\admin\Desktop\deblur\blur\ehhist.jpg')
拉普拉斯增強
拉普拉斯算子可以增強局部的圖像對比度,但對圖像適應(yīng)性較弱,難以用統(tǒng)一的拉普拉斯卷積核對所有的圖像進行增強
import numpy as np
import cv2
from PIL import Image
from matplotlib import pyplot as plt
ori_gray = cv2.imread(r"C:\Users\admin\Desktop\deblur\blur\blur1.jpg",0) # 讀取灰度圖像
# ori = np.array(ori)
ori_gray = np.array(ori_gray)
weight = ori_gray.shape[0]
height = ori_gray.shape[1]
# laplation kernel
h = np.array(([0,-1,0],[-1,5,-1],[0,-1,0]), dtype="float32")
filteredImg = cv2.filter2D(ori_gray,-1,h)
cv2.namedWindow('Laplation_Result', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Laplation_Result', int(weight/4), int(height/4))
cv2.imshow("Laplation_Result", np.hstack([ori_gray, filteredImg]))
cv2.waitKey(0)
log變換
對數(shù)曲線在像素值較低的區(qū)域斜率大,在像素值較高的區(qū)域斜率較小,逐像素操作,處理速度較慢

圖片.png
import cv2
import math
import numpy as np
def logTransform(c, img):
# 3通道RGB
'''h,w,d = img.shape[0],img.shape[1],img.shape[2]
new_img = np.zeros((h,w,d))
for i in range(h):
for j in range(w):
for k in range(d):
new_img[i,j,k] = c*(math.log(1.0+img[i,j,k]))'''
# 灰度圖專屬
h, w = img.shape[0], img.shape[1]
new_img = np.zeros((h, w)).astype(np.uint8)
for i in range(h):
for j in range(w):
new_img[i, j] = c * (math.log(1.0 + img[i, j]))
new_img = cv2.normalize(new_img, new_img, 0, 255, cv2.NORM_MINMAX)
return new_img
# 替換為你的圖片路徑
img = cv2.imread(r"C:\Users\admin\Desktop\deblur\blur\blur2.jpg",0)
weight = img.shape[0]
height = img.shape[1]
log_img = logTransform(0.8, img)
cv2.namedWindow('log_Result', cv2.WINDOW_NORMAL)
cv2.resizeWindow('log_Result', int(weight/2), int(height/4))
cv2.imshow("log_Result", np.hstack([img, log_img]))
# cv2.imwrite(r'C:\Users\xxx\Desktop\Fourier spectrum2.jpg', log_img)
cv2.waitKey(0)
伽馬變換

圖片.png
γ值以1為分界,值越小,對圖像低灰度部分的擴展作用就越強,值越大,對圖像高灰度部分的擴展作用就越強

20190203144454775.png
import math
import numpy as np
import cv2
def gammaTranform(c,gamma,image):
h,w,d = image.shape[0],image.shape[1],image.shape[2]
new_img = np.zeros((h,w,d),dtype=np.float32)
for i in range(h):
for j in range(w):
new_img[i,j,0] = c*math.pow(image[i, j, 0], gamma)
new_img[i,j,1] = c*math.pow(image[i, j, 1], gamma)
new_img[i,j,2] = c*math.pow(image[i, j, 2], gamma)
cv2.normalize(new_img,new_img,0,255,cv2.NORM_MINMAX)
new_img = cv2.convertScaleAbs(new_img)
return new_img
img = cv2.imread(r"C:\Users\admin\Desktop\deblur\blur\blur2.jpg",1)
weight = img.shape[0]
height = img.shape[1]
new_img = gammaTranform(1,2.5,img)
cv2.namedWindow('gama_Result', cv2.WINDOW_NORMAL)
cv2.resizeWindow('gama_Result', int(weight/2), int(height/4))
cv2.imshow('gama_Result', np.hstack([img, new_img]))
# cv2.imwrite(r'C:\Users\xxx\Desktop\gray_2.5.jpg',new_img)
cv2.waitKey(0)
參考原文鏈接:
https://blog.csdn.net/dcrmg/article/details/53677739
https: // blog.csdn.net / shawncheer / article / details / 50812705