如何对 python 中的许多彩色图像进行直方图均衡化?
How to do Histogram Equalization for many colored images in python?
我有许多 .png
扩展格式的彩色图像,它们的默认名称为 right.1.png
、right.2.png
、...和 right.n.png
,还有 wrong.1.png
, wrong.2.png
... 和 wrong.n.png
。我想通过对所有图像使用任何库 numpy、cv2 等在 python 中进行直方图均衡。我应该使用默认名称将均衡后的图像保存在当前工作目录中名为 NEWFOLDER
的新文件夹中。
我尝试按照以下方法 link 处理灰度图像,但未能成功
任何处理此问题的建议
使用 opencv 进行直方图均衡化非常简单。 Here's 文档。
# Histogram equalization
def hist_equal(file_name):
# Read image file
img = cv2.imread(DIR_PATH + file_name, 0)
# Apply histogram equalization
equ = cv2.equalizeHist(img)
# Save file to new directory
cv2.imwrite(NEW_DIR_PATH + file_name, equ)
要遍历目录中的文件,您可以使用 Python 中的 os
库。
# Iterate through each image file
for file_name in os.listdir(DIR_PATH):
hist_equal(file_name)
我有许多 .png
扩展格式的彩色图像,它们的默认名称为 right.1.png
、right.2.png
、...和 right.n.png
,还有 wrong.1.png
, wrong.2.png
... 和 wrong.n.png
。我想通过对所有图像使用任何库 numpy、cv2 等在 python 中进行直方图均衡。我应该使用默认名称将均衡后的图像保存在当前工作目录中名为 NEWFOLDER
的新文件夹中。
我尝试按照以下方法 link 处理灰度图像,但未能成功
使用 opencv 进行直方图均衡化非常简单。 Here's 文档。
# Histogram equalization
def hist_equal(file_name):
# Read image file
img = cv2.imread(DIR_PATH + file_name, 0)
# Apply histogram equalization
equ = cv2.equalizeHist(img)
# Save file to new directory
cv2.imwrite(NEW_DIR_PATH + file_name, equ)
要遍历目录中的文件,您可以使用 Python 中的 os
库。
# Iterate through each image file
for file_name in os.listdir(DIR_PATH):
hist_equal(file_name)