数字信号处理源码

# solution.py
import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
import uniformNoise


def calculate_red_histogram(img):

    # 将图像从BGR转换为RGB(OpenCV中图像默认为BGR格式)
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    # 获取红色分量
    red_channel = img_rgb[:, :, 0]

    # 计算红色分量直方图
    hist, bins = np.histogram(red_channel.flatten(), bins=256, range=[0, 256])

    return hist, bins


def plot_red_histogram(hist, bins):
    # 绘制直方图
    plt.plot(hist, color='red')
    plt.title('Red Channel Histogram')
    #plt.xlabel('Pixel Value')
    #plt.ylabel('Frequency')
    plt.show()


def detect_peaks(hist):
    # 使用find_peaks函数进行峰值检测
    peaks, _ = find_peaks(hist, height=0)
    # 可视化峰值标记
    plt.plot(peaks, red_hist[peaks], "x", color='blue', markersize=10)
    plt.show()

    return peaks


# import numpy as np
# import matplotlib.pyplot as plt

def smooth_histogram(hist, window_size):
    # 定义平均滤波器
    kernel = np.ones(window_size) / window_size
    # 应用卷积操作
    smoothed_hist = np.convolve(hist, kernel, mode='same')

    # 绘制原始直方图和平滑后的直方图
    plt.bar(range(len(red_hist)), red_hist, alpha=0.5, label='Original Histogram')
    plt.plot(smoothed_hist, label='Smoothed Histogram', color='red', linewidth=2)
    plt.legend()
    plt.show()
    return smoothed_hist


# 读取图像
true_face_img = cv2.imread('images/true_face.jpg')
false_face_img = cv2.imread('images/false_face.jpg')

# 计算红色分量直方图
red_hist, red_bins = calculate_red_histogram(true_face_img)
red_hist2, red_bins2 = calculate_red_histogram(false_face_img)

# 绘制红色分量直方图
plot_red_histogram(red_hist, red_bins)
plot_red_histogram(red_hist2, red_bins2)

# 进行峰值检测
peaks = detect_peaks(red_hist)
peaks2 = detect_peaks(red_hist2)

print(len(peaks))
print(len(peaks2))

# 对直方图进行光滑化
smoothed_hist = smooth_histogram(red_hist, 5)  # window_size 为平滑窗口的大小

# 绘制光滑后的红色分量直方图
plot_red_histogram(smoothed_hist, red_bins)

# 进行峰值检测
peaks3 = detect_peaks(smoothed_hist)
print(len(peaks3))

original_image = cv2.imread('images/false_face.jpg')
noisy_image = uniformNoise.add_uniform_noise(original_image, amplitude=100)  # amplitude为均匀分布的振幅,可以调整

# 计算添加噪声的直方图
noise_hist, noise_bins = calculate_red_histogram(noisy_image)

# 绘制噪声直方图
plot_red_histogram(noise_hist, noise_bins)

# 噪声图像峰值检测
noise_peaks = detect_peaks(noise_hist)
print(len(noise_peaks))

# uniformaNoise.py

import cv2
import numpy as np


def add_uniform_noise(image, amplitude=20):
    """
    在图像中添加均匀噪声
    :param image: 原始图像
    :param amplitude: 均匀分布的振幅
    :return: 添加噪声后的图像
    """
    noisy_image = image + np.random.uniform(low=-amplitude, high=amplitude, size=image.shape)
    noisy_image = np.clip(noisy_image, 0, 255)  # 确保图像值在合理范围内
    return noisy_image.astype(np.uint8)


# 读取图像
original_image = cv2.imread('images/false_face.jpg')

# 添加均匀噪声
noisy_image = add_uniform_noise(original_image, amplitude=20)  # amplitude为均匀分布的振幅,可以调整

# 显示原始图像和添加噪声后的图像
# cv2.imshow('Original Image', original_image)
# cv2.imshow('Noisy Image', noisy_image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

posted @ 2023-12-20 03:18  Coline1  阅读(14)  评论(0编辑  收藏  举报