上传文件至 /
这个提交包含在:
17
ysml.py
普通文件
17
ysml.py
普通文件
@ -0,0 +1,17 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
def laplacian_sharpen(image_path):
|
||||
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
||||
laplacian = cv2.Laplacian(img, cv2.CV_64F)
|
||||
sharpened = cv2.convertScaleAbs(img - laplacian)
|
||||
return sharpened
|
||||
# 读取图像
|
||||
image = cv2.imread('ysmltest.png', cv2.IMREAD_GRAYSCALE)
|
||||
sharp_laplacian = laplacian_sharpen('ysmltest.png')
|
||||
#cv2.imshow('Original Image', image)
|
||||
#cv2.imshow('Filtered Image', sharp_laplacian)
|
||||
cv2.waitKey(0)
|
||||
cv2.destroyAllWindows()
|
||||
107
ysml01.py
普通文件
107
ysml01.py
普通文件
@ -0,0 +1,107 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import ysml as pill
|
||||
import os
|
||||
# 定义直方图均衡化函数
|
||||
def histogram_equalization(image):
|
||||
# 应用直方图均衡化
|
||||
eq = cv2.equalizeHist(image)
|
||||
return eq
|
||||
|
||||
|
||||
# 定义自适应直方图均衡化函数
|
||||
def adaptive_histogram_equalization(image):
|
||||
# 创建 CLAHE 对象
|
||||
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
||||
# 应用自适应直方图均衡化
|
||||
cl = clahe.apply(image)
|
||||
return cl
|
||||
|
||||
|
||||
# 定义 Gamma 校正函数
|
||||
def gamma_correction(image, gamma=1.0):
|
||||
inv_gamma = 1.0 / gamma
|
||||
table = np.array([((i / 255.0) ** inv_gamma) * 255 for i in range(256)]).astype("uint8")
|
||||
# 应用 Gamma 校正查找表
|
||||
adjusted = cv2.LUT(image, table)
|
||||
return adjusted
|
||||
|
||||
|
||||
# 定义对比度拉伸函数
|
||||
def contrast_stretching(image, low_percentile=2, high_percentile=98):
|
||||
# 计算灰度级的百分位数
|
||||
low_val = np.percentile(image, low_percentile)
|
||||
high_val = np.percentile(image, high_percentile)
|
||||
|
||||
# 应用对比度拉伸
|
||||
stretched = np.clip((image - low_val) * 255 / (high_val - low_val), 0, 255).astype(np.uint8)
|
||||
return stretched
|
||||
|
||||
|
||||
def save_processed_image_opencv(input_path, output_path, compression_params=None):
|
||||
if compression_params is None:
|
||||
compression_params = [cv2.IMWRITE_JPEG_QUALITY, 95]
|
||||
try:
|
||||
# 读取图片
|
||||
img = cv2.imread(input_path)
|
||||
if img is None:
|
||||
raise ValueError("无法读取图片文件")
|
||||
processed_img=pill.laplacian_sharpen(input_path)
|
||||
# 确保输出目录存在
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
# 保存图片
|
||||
cv2.imwrite(output_path, processed_img, compression_params)
|
||||
print(f"图片已保存到: {output_path}")
|
||||
except Exception as e:
|
||||
print(f"处理图片时出错: {e}")
|
||||
|
||||
|
||||
save_processed_image_opencv('ysmltest.png', 'output/processed_opencv.jpg')
|
||||
# 读取原始图像
|
||||
sharp_laplacian = pill.laplacian_sharpen('ysmltest.png')
|
||||
image_path = ('output/processed_opencv.jpg')
|
||||
img_gray = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
||||
# 对灰度图像进行处理
|
||||
enhanced_eq = histogram_equalization(img_gray)
|
||||
enhanced_clahe = adaptive_histogram_equalization(img_gray)
|
||||
enhanced_gamma = gamma_correction(img_gray, gamma=0.8)
|
||||
enhanced_stretched = contrast_stretching(img_gray)
|
||||
|
||||
# 将处理后的灰度图像转换为彩色图像
|
||||
enhanced_eq_color = cv2.cvtColor(enhanced_eq, cv2.COLOR_GRAY2RGB)
|
||||
enhanced_clahe_color = cv2.cvtColor(enhanced_clahe, cv2.COLOR_GRAY2RGB)
|
||||
enhanced_gamma_color = cv2.cvtColor(enhanced_gamma, cv2.COLOR_GRAY2RGB)
|
||||
enhanced_stretched_color = cv2.cvtColor(enhanced_stretched, cv2.COLOR_GRAY2RGB)
|
||||
|
||||
# 展示图像
|
||||
plt.figure(figsize=(12, 12))
|
||||
|
||||
#.subplot(3, 2, 1)
|
||||
#plt.imshow(cv2.cvtColor(img_gray, cv2.COLOR_GRAY2RGB))
|
||||
#plt.title('Original Grayscale Image')
|
||||
#plt.axis('off')
|
||||
|
||||
#plt.subplot(3, 2, 2)
|
||||
#plt.imshow(enhanced_eq_color)
|
||||
#plt.title('Histogram Equalization')
|
||||
#plt.axis('off')
|
||||
|
||||
#plt.subplot(3, 2, 3)
|
||||
#plt.imshow(enhanced_clahe_color)
|
||||
#plt.title('Adaptive Histogram Equalization')
|
||||
#plt.axis('off')
|
||||
|
||||
#plt.subplot(3, 2, 4)
|
||||
#plt.imshow(enhanced_gamma_color)
|
||||
#plt.title('Gamma Correction')
|
||||
#plt.axis('off')
|
||||
|
||||
#plt.subplot(3, 2, 5)
|
||||
plt.imshow(enhanced_stretched_color)
|
||||
plt.title('Contrast Stretching')
|
||||
plt.axis('off')
|
||||
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
|
||||
在新工单中引用
屏蔽一个用户