add image test
|
@ -0,0 +1,153 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"id": "bc5d3202-7004-4a30-9688-d590b7ff40ff",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import cv2\n",
|
||||
"import numpy as np"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"id": "afcaafc9-bc9d-4b6e-a000-7daae79ca33c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def image_blur(image):\n",
|
||||
" if image is None:\n",
|
||||
" raise ValueError(\"Image is not loaded correctly.\")\n",
|
||||
" # 高斯模糊\n",
|
||||
" gaussian_blur = cv2.GaussianBlur(image, (15, 15), 0)\n",
|
||||
" # 均值模糊\n",
|
||||
" mean_blur = cv2.blur(image, (15, 15))\n",
|
||||
" # 中值模糊\n",
|
||||
" median_blur = cv2.medianBlur(image, 15)\n",
|
||||
" return gaussian_blur,mean_blur,median_blur"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"id": "34a40ed3-6050-4161-ba30-8156473f8078",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Error: Image not found or unable to load.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[ WARN:0@1179.199] global loadsave.cpp:268 findDecoder imread_('002.png'): can't open/read file: check file path/integrity\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"image_path = '002.png'\n",
|
||||
"image = cv2.imread(image_path)\n",
|
||||
"if image is None:\n",
|
||||
" raise ValueError(\"Error: Image not found or unable to load.\")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" # gaussian_blur, mean_blur, median_blur = image_blur(image)\n",
|
||||
" # cv2.imwrite('gaussian_blur.jpg', gaussian_blur)\n",
|
||||
" # cv2.imwrite('mean_blur.jpg', mean_blur)\n",
|
||||
" # cv2.imwrite('median_blur.jpg', median_blur)\n",
|
||||
"# gaussian_blur,mean_blur,median_blur=image_blur(image)\n",
|
||||
"# cv2.imwrite('123', image)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a745fe14-a960-4668-bd98-c17025619f1b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def brenner(image):\n",
|
||||
" gradient = np.zeros_like(image, dtype=np.float32)\n",
|
||||
" gradient[:-2, :] = image[2:, :] - image[:-2, :]\n",
|
||||
" return np.var(gradient)\n",
|
||||
"\n",
|
||||
"# 计算清晰度\n",
|
||||
"brenner_original = brenner(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY))\n",
|
||||
"brenner_gaussian = brenner(cv2.cvtColor(gaussian_blur, cv2.COLOR_BGR2GRAY))\n",
|
||||
"brenner_mean = brenner(cv2.cvtColor(mean_blur, cv2.COLOR_BGR2GRAY))\n",
|
||||
"brenner_median = brenner(cv2.cvtColor(median_blur, cv2.COLOR_BGR2GRAY))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"id": "45a1739c-20fe-4df8-b703-732c23ff3400",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[ WARN:0@1400.488] global loadsave.cpp:268 findDecoder imread_('002.png'): can't open/read file: check file path/integrity\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"ename": "ValueError",
|
||||
"evalue": "Error: Image not found or unable to load.",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
|
||||
"Cell \u001b[0;32mIn[25], line 6\u001b[0m\n\u001b[1;32m 4\u001b[0m image \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(image_path)\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m image \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m----> 6\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mError: Image not found or unable to load.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 7\u001b[0m cv2\u001b[38;5;241m.\u001b[39mimwrite(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m123.png\u001b[39m\u001b[38;5;124m'\u001b[39m, image)\n",
|
||||
"\u001b[0;31mValueError\u001b[0m: Error: Image not found or unable to load."
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import cv2\n",
|
||||
"\n",
|
||||
"image_path = '002.png'\n",
|
||||
"image = cv2.imread(image_path)\n",
|
||||
"if image is None:\n",
|
||||
" raise ValueError(\"Error: Image not found or unable to load.\")\n",
|
||||
"cv2.imwrite('123.png', image)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1f645692-aca6-4701-8382-99b993eef448",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "pmn_env",
|
||||
"language": "python",
|
||||
"name": "pmn_env"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.16"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
import cv2
|
||||
|
||||
image_path = '002.png'
|
||||
image = cv2.imread(image_path)
|
||||
if image is None:
|
||||
raise ValueError("Error: Image not found or unable to load.")
|
||||
cv2.imwrite('123.png', image)
|
|
@ -0,0 +1,185 @@
|
|||
import os
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
# 高斯模糊
|
||||
def image_blur(image,ksize):
|
||||
if image is None:
|
||||
raise ValueError("Image is not loaded correctly.")
|
||||
gaussian_blur = cv2.GaussianBlur(image, (ksize, ksize), 0)
|
||||
return gaussian_blur
|
||||
|
||||
# 椒盐噪声
|
||||
def add_salt_pepper(image, salt_prob=0.05, pepper_prob=0.05):
|
||||
noisy = image.copy()
|
||||
# 添加盐噪声
|
||||
salt = np.random.randint(0, 256, size=image.shape[:2])
|
||||
noisy[salt < salt_prob*255] = 255
|
||||
# 添加椒噪声
|
||||
pepper = np.random.randint(0, 256, size=image.shape[:2])
|
||||
noisy[pepper < pepper_prob*255] = 0
|
||||
return noisy
|
||||
|
||||
# Brenner 梯度函数计算
|
||||
def brenner(img):
|
||||
'''
|
||||
:param img: narray 二维灰度图像
|
||||
:return: int 图像越清晰越大
|
||||
'''
|
||||
return int(np.sum((img[:-2, :] - img[2:, :]) ** 2))
|
||||
|
||||
# Laplacian 梯度函数计算
|
||||
def Laplacian(img):
|
||||
'''
|
||||
:param img: narray 二维灰度图像
|
||||
:return: int 图像越清晰越大
|
||||
'''
|
||||
return int(cv2.Laplacian(img, cv2.CV_64F).var())
|
||||
|
||||
# SMD 梯度函数计算
|
||||
def SMD(img):
|
||||
'''
|
||||
:param img: narray 二维灰度图像
|
||||
:return: int 图像越清晰越大
|
||||
'''
|
||||
diff_x = np.abs(img[1:-1, :] - img[:-2, :])
|
||||
diff_y = np.abs(img[1:, :] - img[:-1, :])
|
||||
return int(np.sum(diff_x) + np.sum(diff_y))
|
||||
|
||||
# SMD2 函数计算
|
||||
def SMD2(img):
|
||||
'''
|
||||
:param img: narray 二维灰度图像
|
||||
:return: int 图像越清晰越大
|
||||
'''
|
||||
sobel_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
|
||||
sobel_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
|
||||
gradient = np.abs(sobel_x) + np.abs(sobel_y)
|
||||
return int(np.sum(gradient ** 2))
|
||||
|
||||
# 方差函数计算
|
||||
def variance(img):
|
||||
'''
|
||||
:param img: narray 二维灰度图像
|
||||
:return: int 图像越清晰越大
|
||||
'''
|
||||
u = np.mean(img)
|
||||
return int(np.sum((img - u) ** 2))
|
||||
|
||||
# Energy 函数计算
|
||||
def energy(img):
|
||||
'''
|
||||
:param img: narray 二维灰度图像
|
||||
:return: int 图像越清晰越大
|
||||
'''
|
||||
diff_x = img[1:, :-1] - img[:-1, :-1]
|
||||
diff_y = img[:-1, 1:] - img[:-1, :-1]
|
||||
return int(np.sum(diff_x ** 2 * diff_y ** 2))
|
||||
|
||||
# Vollath 函数计算
|
||||
def Vollath(img):
|
||||
'''
|
||||
:param img: narray 二维灰度图像
|
||||
:return: int 图像越清晰越大
|
||||
'''
|
||||
u = np.mean(img)
|
||||
return int(-img.size * (u ** 2) + np.sum(img[:-1, :] * img[1:, :]))
|
||||
|
||||
# Entropy 函数计算
|
||||
def entropy(img):
|
||||
'''
|
||||
:param img: narray 二维灰度图像
|
||||
:return: int 图像越清晰越大
|
||||
'''
|
||||
hist = np.bincount(img.flatten(), minlength=256)
|
||||
p = hist / img.size
|
||||
p = p[p > 0]
|
||||
return int(-np.sum(p * np.log(p)))
|
||||
|
||||
|
||||
|
||||
def image_test(filename,image,noisy_img,blur_image):
|
||||
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
noisy_gray = cv2.cvtColor(noisy_img, cv2.COLOR_BGR2GRAY)
|
||||
blur_gray = cv2.cvtColor(blur_image, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
# 定义一个格式化字符串,确保每个数值占据相同的宽度
|
||||
format_str = "{:<10} {:<15} {:<15} {:<15}"
|
||||
|
||||
# 打印标题
|
||||
print(format_str.format(filename, "Original", "Noisy", "Blur"))
|
||||
|
||||
# 打印每个指标的值
|
||||
print(format_str.format("Brenner", brenner(image_gray), brenner(noisy_gray), brenner(blur_gray)))
|
||||
print(format_str.format("Laplacian", Laplacian(image_gray), Laplacian(noisy_gray), Laplacian(blur_gray)))
|
||||
print(format_str.format("SMD", SMD(image_gray), SMD(noisy_gray), SMD(blur_gray)))
|
||||
print(format_str.format("SMD2", SMD2(image_gray), SMD2(noisy_gray), SMD2(blur_gray)))
|
||||
print(format_str.format("Variance", variance(image_gray), variance(noisy_gray), variance(blur_gray)))
|
||||
print(format_str.format("Energy", energy(image_gray), energy(noisy_gray), energy(blur_gray)))
|
||||
print(format_str.format("Vollath", Vollath(image_gray), Vollath(noisy_gray), Vollath(blur_gray)))
|
||||
print(format_str.format("Entropy", entropy(image_gray), entropy(noisy_gray), entropy(blur_gray)))
|
||||
|
||||
|
||||
# print(filename,'noisy','blur')
|
||||
# print('Brenner',brenner(image_gray),brenner(noisy_gray),brenner(blur_gray))
|
||||
# print('Laplacian',Laplacian(image_gray),Laplacian(noisy_gray),Laplacian(blur_gray))
|
||||
# print('SMD',SMD(image_gray), SMD(noisy_gray), SMD(blur_gray))
|
||||
# print('SMD2',SMD2(image_gray), SMD2(noisy_gray), SMD2(blur_gray))
|
||||
# print('Variance',variance(image_gray),variance(noisy_gray),variance(blur_gray))
|
||||
# print('Energy',energy(image_gray),energy(noisy_gray),energy(blur_gray))
|
||||
# print('Vollath',Vollath(image_gray),Vollath(noisy_gray),Vollath(blur_gray))
|
||||
# print('Entropy',entropy(image_gray),entropy(noisy_gray),entropy(blur_gray))
|
||||
|
||||
|
||||
|
||||
def process_images(input_dir, salty_dir,blur_dir):
|
||||
# 创建输出目录(如果不存在)
|
||||
if not os.path.exists(salty_dir):
|
||||
os.makedirs(salty_dir)
|
||||
if not os.path.exists(blur_dir):
|
||||
os.makedirs(blur_dir)
|
||||
|
||||
# 支持的图片格式
|
||||
img_exts = ('.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff')
|
||||
|
||||
# 遍历输入目录
|
||||
for filename in os.listdir(input_dir):
|
||||
if filename.lower().endswith(img_exts):
|
||||
# 读取图片
|
||||
img_path = os.path.join(input_dir, filename)
|
||||
img = cv2.imread(img_path)
|
||||
img = cv2.resize(img,(512, 512))
|
||||
|
||||
if img is None:
|
||||
raise ValueError("Error: Image not found or unable to load.")
|
||||
|
||||
if img is not None:
|
||||
|
||||
# 添加椒盐噪声
|
||||
noisy_img = add_salt_pepper(img)
|
||||
# 添加Gauss噪声
|
||||
blur_image = image_blur(img,ksize=25)
|
||||
|
||||
|
||||
|
||||
# 构造新文件名
|
||||
name, ext = os.path.splitext(filename)
|
||||
salty_filename = f"{name}_salty{ext}"
|
||||
blur_filename = f"{name}_blur{ext}"
|
||||
|
||||
image_test(filename,img,noisy_img,blur_image)
|
||||
print('\n')
|
||||
|
||||
|
||||
salty_path = os.path.join(salty_dir, salty_filename)
|
||||
cv2.imwrite(salty_path, noisy_img)
|
||||
blur_path = os.path.join(blur_dir, blur_filename)
|
||||
cv2.imwrite(blur_path, blur_image)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
input_dir = 'data_process/Clarity Test/images/source'
|
||||
salty_dir = 'data_process/Clarity Test/images/salty'
|
||||
blur_dir = 'data_process/Clarity Test/images/blur'
|
||||
process_images(input_dir, salty_dir,blur_dir)
|
||||
|
After Width: | Height: | Size: 302 KiB |
After Width: | Height: | Size: 40 KiB |
After Width: | Height: | Size: 283 KiB |
After Width: | Height: | Size: 540 KiB |
After Width: | Height: | Size: 234 KiB |
After Width: | Height: | Size: 516 KiB |
After Width: | Height: | Size: 1.3 MiB |
After Width: | Height: | Size: 79 KiB |
After Width: | Height: | Size: 433 KiB |
|
@ -4,11 +4,12 @@ import numpy as np
|
|||
import os
|
||||
|
||||
|
||||
def calculate_clarity_score(image_path):
|
||||
def calculate_clarity_score(image_path,target_size=(512, 512)):
|
||||
"""
|
||||
计算图片的清晰度评分
|
||||
"""
|
||||
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
||||
image = cv2.resize(image, target_size)
|
||||
if image is None:
|
||||
raise ValueError(f"无法读取图片:{image_path}")
|
||||
return cv2.Laplacian(image, cv2.CV_64F).var()
|
||||
|
@ -40,9 +41,7 @@ def process_images(input_folder, output_folder,clarity_score_threshold):
|
|||
# output_subfolder
|
||||
relative_path = os.path.relpath(root, input_folder)
|
||||
output_subfolder = os.path.join(output_folder, relative_path)
|
||||
# print(f'root: {root}, input_folder: {input_folder}, relative_path: {relative_path}, output_subfolder: {output_subfolder}')
|
||||
|
||||
|
||||
# print(f'root: {root}, input_folder: {input_folder}, relative_path: {relative_path}, output_subfolder: {output_subfolder}')
|
||||
if not os.path.exists(output_subfolder):
|
||||
os.makedirs(output_subfolder)
|
||||
output_path = os.path.join(output_subfolder, os.path.splitext(filename)[0] + ".png")
|
||||
|
@ -55,8 +54,8 @@ def process_images(input_folder, output_folder,clarity_score_threshold):
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
input_folder = 'PMN_WS/data_process/datasets/source' # 输入文件夹路径
|
||||
output_folder = 'PMN_WS/data_process/datasets/target' # 输出文件夹路径
|
||||
input_folder = 'data_process/datasets/source' # 输入文件夹路径
|
||||
output_folder = 'data_process/datasets/target' # 输出文件夹路径
|
||||
clarity_score_threshold =50
|
||||
process_images(input_folder, output_folder,clarity_score_threshold)
|
||||
|