我正在尝试对图片进行抗锯齿处理,并尝试了几种方法,包括使用 open CV、使用 matplotlib 以及使用 PIL。但还是摆脱不了那种生涩的感觉。
这是抗锯齿的图片。 我尝试的第一个方法是使用opencv。
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('LENS_MODEL.png')
kernel = np.ones((5,5),np.float32)/25
dst = cv.filter2D(img,-1,kernel)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()
第二种方法我用matplotlib尝试过,但仍然无法摆脱抖动,图像质量更差。
fig, axs = plt.subplots(2, 2, figsize=(5, 6), constrained_layout=True)
axs[0, 0].imshow(img, interpolation='nearest')
for ax, interp in zip(axs.flat[1:],
['nearest', 'antialiased', 'antialiased']):
ax.imshow(img, interpolation=interp,
cmap='RdBu_r')
ax.set_title(f"interpolation='{interp}'")
plt.show()
有什么建议我如何使用图像抗锯齿来消除图像中的这种尖锐过渡?我也尝试过 PIL 但似乎仍然没有任何改进...
这是一个代码示例,用于实现已接受答案中所做的事情
def apply_antialiasing(image, blur_strength=10):
# apply gaussian blur. control blur "strength" with the blur_strength parameter
blurred = cv.GaussianBlur(image, (0,0), sigmaX=blur_strength, sigmaY=blur_strength, borderType = cv.BORDER_DEFAULT)
# high contrast (just binarize your image in place)
blurred[blurred <= 127] = 0
blurred[blurred > 127] = 255
# aa is an 8 bits numpy array
return blurred.astype('uint8')
你用它:
# load your image
aa = apply_antialiasing(your_image)
# display aa as an image here
希望这对某人有帮助。