cof*_*win 4 c++ python opencv image-processing computer-vision
我正在尝试恢复和增强几张照片的图像细节。我尝试通过使用简单的内核提高清晰度来cv2.filter2D()显示细节。
我尝试过边缘检测内核
[-1 -1 -1]
[-1 9 -1]
[-1 -1 -1]
Run Code Online (Sandbox Code Playgroud)
和锐化内核
[ 0 -1 0]
[-1 5 -1]
[ 0 -1 0]
Run Code Online (Sandbox Code Playgroud)
但结果看起来有颗粒感且不自然。为了消除噪音,我尝试了诸如cv2.medianBlur()和 之类的模糊技术cv2.GaussianBlur(),但结果并不那么好。图像背景模糊或较暗,导致特征难以区分。有没有更好的方法来显示更多细节,尤其是在背景中?对 Python 或 C++ 开放
输入图像
import numpy as np
import cv2
img = cv2.imread('people.jpg')
grayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# edge_kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
sharpen_kernel = np.array([[0,-1,0], [-1,5,-1], [0,-1,0]])
img = cv2.filter2D(grayscale, -1, sharpen_kernel)
# Smooth out image
# blur = cv2.medianBlur(img, 3)
blur = cv2.GaussianBlur(img, (3,3), 0)
cv2.imshow('img',img)
cv2.imwrite('img.png',img)
cv2.imshow('blur',blur)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
直方图均衡在这里可以通过扩展强度范围来提高灰度图像的对比度。这是输入图像直方图的可视化

from matplotlib import pyplot as plt
import cv2
# Load in image as grayscale
image = cv2.imread('1.jpg', 0)
plt.hist(image.ravel(), 256, [0,256])
Run Code Online (Sandbox Code Playgroud)
像素聚集在中间范围强度周围。为了增加图像的对比度,直方图均衡化在整个范围内拉伸强度值以获得更宽且更均匀的分布。您可以使用内置函数来做到这一点,cv2.equalizeHist()

equalize = cv2.equalizeHist(image)
plt.hist(equalize.ravel(), 256, [0,256])
Run Code Online (Sandbox Code Playgroud)
强度范围现在均匀分布。直方图均衡考虑了图像的全局对比度,并且当图像的直方图局限于特定区域时效果很好。这是结果
In some cases where there are intensity variations across a large region, CLAHE (Contrast Limited Adaptive Histogram Equalization) may be better. CLAHE is implemented in OpenCV as cv2.createCLAHE()

clahe = cv2.createCLAHE().apply(image)
plt.hist(clahe.ravel(), 256, [0,256])
Run Code Online (Sandbox Code Playgroud)
This variation divides pixels into small blocks before performing adaptive histogram equalization.
Here's a visualization between the two methods
