如何合成低照度图像 用于神经元网络学习

目前使用retinex-net进行训练模型,希望对某些特殊场景图像进行深度学习,想自己生成低照度图像训练集。 想问下有没能用python代码进行批量生成低照度图像。寻求帮助另有感谢费

嗯,期望这个实例可以帮助到你,【低照度图像增强之卷积神经网络RetinexNet】,链接:https://blog.csdn.net/weixin_38285131/article/details/88287131/

生成低照度图像是一个有挑战性的任务,因为需要模拟真实世界中的低照度条件。一种常用的方法是通过对真实图像进行处理来生成低照度图像,例如降低亮度、增加噪声等。在Python中,你可以使用PIL库来处理图像,并实现批量生成低照度图像的功能。

下面是一个简单的示例代码,用于对一张图像生成低照度图像:

from PIL import Image
import numpy as np

def generate_low_light_image(image_path, brightness_factor, noise_factor):
    # 打开图像
    image = Image.open(image_path)

    # 将图像转换为numpy数组
    img_array = np.array(image)

    # 降低亮度
    low_light_img = img_array * brightness_factor

    # 添加噪声
    noise = np.random.normal(scale=noise_factor, size=img_array.shape)
    low_light_img += noise

    # 将图像值限制在0到255之间
    low_light_img = np.clip(low_light_img, 0, 255)

    # 转换回PIL图像对象
    low_light_image = Image.fromarray(np.uint8(low_light_img))

    return low_light_image

# 示例用法
input_image_path = 'path/to/your/input/image.jpg'
brightness_factor = 0.5  # 降低亮度的因子,值越小越暗
noise_factor = 20  # 噪声因子,值越大噪声越明显

low_light_image = generate_low_light_image(input_image_path, brightness_factor, noise_factor)
low_light_image.save('path/to/your/output/low_light_image.jpg')

上述代码中,你可以通过调整brightness_factornoise_factor参数来控制低照度图像的亮度和噪声水平。为了生成批量的低照度图像,你可以将上述代码放在循环中,遍历输入图像文件列表,然后保存生成的低照度图像到相应的输出文件夹中。
请注意,这只是一个简单的示例,生成真实世界的低照度图像是一个复杂的任务,需要根据具体场景和需求来调整生成低照度图像的方法和参数。

援引GPT回答:
可以使用Python代码批量生成低照度图像。以下是一个示例代码,使用OpenCV库和numpy库来生成低照度图像:

import cv2
import numpy as np

def generate_low_light_images(input_folder, output_folder, brightness_factor):
    # 读取输入文件夹中的所有图像
    image_files = glob.glob(input_folder + '/*.jpg')  # 根据实际文件格式进行更改

    for file in image_files:
        # 读取图像
        image = cv2.imread(file)
        
        # 将图像转换为浮点数
        image = image.astype(np.float32) / 255.0
        
        # 调整图像亮度
        low_light_image = image * brightness_factor
        
        # 将图像转换回整数类型(0-255)
        low_light_image = (low_light_image * 255).astype(np.uint8)
        
        # 保存生成的低照度图像
        output_file = os.path.join(output_folder, os.path.basename(file))
        cv2.imwrite(output_file, low_light_image)

# 设置输入文件夹、输出文件夹和亮度因子
input_folder = 'path/to/input/folder'
output_folder = 'path/to/output/folder'
brightness_factor = 0.5  # 调整此值以控制低照度程度

# 生成低照度图像
generate_low_light_images(input_folder, output_folder, brightness_factor)

在上述代码中,你需要将input_folder替换为包含原始图像的文件夹的路径,将output_folder替换为保存生成的低照度图像的文件夹的路径,并调整brightness_factor以控制低照度程度。

请注意,这只是一个简单的示例代码,根据你的特殊场景可能需要进行更多的图像处理和调整。

【以下回答由 GPT 生成】

我可以帮助你生成低照度图像。下面是一个具体的解决方案:

步骤1: 导入依赖 首先,你需要安装并导入一些Python库。你可以使用以下代码:

import cv2
import numpy as np

步骤2: 读取原始图像 接下来,你需要读取原始图像。你可以使用以下代码:

# 读取原始图像
img = cv2.imread('your_image_path.jpg')  # 替换为你的图像路径

步骤3: 调整图像的亮度 然后,你可以通过调整图像的亮度来生成低照度图像。你可以使用以下代码将图像的亮度降低到特定的程度:

# 将图像的亮度降低到特定的程度(例如,50)
darkened_img = cv2.convertScaleAbs(img, alpha=0.5)  # alpha的值即为降低的程度,0.5表示降低为原来的一半

步骤4: 保存生成的低照度图像 最后,你可以保存生成的低照度图像。你可以使用以下代码:

# 保存生成的低照度图像
cv2.imwrite('darkened_image.jpg', darkened_img)  # 替换为你希望保存的路径和文件名

这样,你就可以生成低照度的图像。

请注意,这只是一个简单的示例,只是通过减小图像的亮度来生成低照度图像。如果你希望更复杂的效果,可能需要使用其他技术或算法。

另外,如果你希望将这个过程应用到多个图像,你可以使用循环来批量生成低照度图像。

如果你对于Retinex-Net的特定用法有更多需求,你可以提供更详细的信息,我可以给出更具体的解决方案。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

生成低照度图像可以通过调整图像的亮度和对比度来实现。以下是使用Python代码批量生成低照度图像的示例:

import cv2
import numpy as np

def generate_low_light_images(input_folder, output_folder, brightness_factor, contrast_factor):
    # 读取输入文件夹中的所有图像文件
    image_files = glob.glob(input_folder + '/*.jpg')  # 根据实际情况修改文件格式
    for image_file in image_files:
        # 读取图像
        image = cv2.imread(image_file)
        
        # 调整亮度和对比度
        adjusted_image = cv2.convertScaleAbs(image, alpha=brightness_factor, beta=contrast_factor)
        
        # 保存生成的低照度图像
        output_file = output_folder + '/' + os.path.basename(image_file)
        cv2.imwrite(output_file, adjusted_image)

# 设置输入和输出文件夹路径
input_folder = 'path/to/input/folder'
output_folder = 'path/to/output/folder'

# 设置亮度和对比度因子
brightness_factor = 0.5  # 调整亮度的因子,范围为0-1,值越小越暗
contrast_factor = 0.8  # 调整对比度的因子,范围为0-1,值越小对比度越低

# 生成低照度图像
generate_low_light_images(input_folder, output_folder, brightness_factor, contrast_factor)

在上述代码中,你需要将input_folderoutput_folder替换为实际的输入和输出文件夹路径。brightness_factorcontrast_factor是用于调整亮度和对比度的因子,你可以根据需要进行调整。

请注意,这只是一种简单的方法来生成低照度图像,效果可能不如专业的算法(如Retinex-Net)好。如果你希望生成更真实的低照度图像,可以考虑使用其他先进的算法或数据集。



```c
import cv2
import numpy as np
 
def generate_low_light_images(input_folder, output_folder, brightness_factor):
    # 读取输入文件夹中的所有图像
    image_files = glob.glob(input_folder + '/*.jpg')  # 根据实际文件格式进行更改
 
    for file in image_files:
        # 读取图像
        image = cv2.imread(file)
        
        # 将图像转换为浮点数
        image = image.astype(np.float32) / 255.0
        
        # 调整图像亮度
        low_light_image = image * brightness_factor
        
        # 将图像转换回整数类型(0-255)
        low_light_image = (low_light_image * 255).astype(np.uint8)
        
        # 保存生成的低照度图像
        output_file = os.path.join(output_folder, os.path.basename(file))
        cv2.imwrite(output_file, low_light_image)
 
# 设置输入文件夹、输出文件夹和亮度因子
input_folder = 'path/to/input/folder'
output_folder = 'path/to/output/folder'
brightness_factor = 0.5  # 调整此值以控制低照度程度
 
# 生成低照度图像
generate_low_light_images(input_folder, output_folder, brightness_factor)

```

逻辑回归是一种二分类算法,用于预测二分类问题。逻辑回归的优点是计算速度快,便于理解和实现。本文将介绍使用 Python 构建逻辑回归模型,进行5折交叉验证,每验证一次绘制一条曲线,并最终绘制一条平均 ROC 曲线。

数据集

本文使用的数据集是 UCI 的 Pima Indians Diabetes 数据集,该数据集包含了768个样本,8个特征和1个二元分类。我们将使用该数据集来构建逻辑回归模型。

首先,我们需要导入sklearnpandas库,并读取数据集:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_curve, auc
import numpy as np
import matplotlib.pyplot as plt

# 加载数据集
data = pd.read_csv('diabetes.csv')

# 划分数据集
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)

我们将数据集划分为训练集和测试集,测试集占25%。

构建逻辑回归模型

我们将使用LogisticRegression类来构建逻辑回归模型。首先,我们需要实例化该类,并将训练集数据拟合到模型中:

# 构建逻辑回归模型
logreg = LogisticRegression()

# 将训练集拟合到模型中
logreg.fit(X_train, y_train)

5折交叉验证绘制 ROC 曲线

接下来,我们使用cross_val_predict函数来进行5折交叉验证,并使用roc_curve函数来计算 ROC 曲线的参数。

from sklearn.model_selection import cross_val_predict

# 进行5折交叉验证
y_score = cross_val_predict(logreg, X_train, y_train, cv=5, method='predict_proba')

# 计算 ROC 曲线的参数
fpr, tpr, thresholds = roc_curve(y_train, y_score[:, 1])
roc_auc = auc(fpr, tpr)

# 绘制 ROC 曲线
plt.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)

# 绘制对角线
plt.plot([0, 1], [0, 1], 'k--')

# 设置图形参数
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc='lower right')
plt.show()

我们使用cross_val_predict函数进行5折交叉验证,并将预测的概率值存储在y_score中。然后,我们使用roc_curve函数来计算 ROC 曲线的参数,包括真阳性率和假阳性率。最后,我们使用plt.plot函数将 ROC 曲线绘制出来。

接下来,我们使用for循环进行5次交叉验证,并绘制5条 ROC 曲线:

from sklearn.model_selection import StratifiedKFold

# 进行5折交叉验证
cv = StratifiedKFold(n_splits=5)
mean_tpr = 0.0
mean_fpr = np.linspace(0, 1, 100)
colors = ['blue', 'red', 'green', 'purple', 'black']

# 绘制多条 ROC 曲线
for i, (train, test) in enumerate(cv.split(X_train, y_train)):
    probas_ = logreg.fit(X_train[train], y_train[train]).predict_proba(X_train[test])
    fpr, tpr, thresholds = roc_curve(y_train[test], probas_[:, 1])
    mean_tpr += np.interp(mean_fpr, fpr, tpr)
    plt.plot(fpr, tpr, lw=1, color=colors[i],
             label='ROC fold %d (area = %0.2f)' % (i, auc(fpr, tpr)))

# 绘制随机猜测的对角线
plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='black',
         label='Random guess', alpha=.8)

# 计算和绘制平均 ROC 曲线
mean_tpr /= cv.get_n_splits(X_train, y_train)
mean_tpr[-1] = 1.0
mean_auc = auc(mean_fpr, mean_tpr)
plt.plot(mean_fpr, mean_tpr, color='navy',
         label='Mean ROC (area = %0.2f)' % mean_auc, lw=2)

# 设置图形参数
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('False Positive Rate', fontsize=16)
plt.ylabel('True Positive Rate', fontsize=16)
plt.title('Receiver Operating Characteristic (ROC) Curve', fontsize=18)
plt.legend(loc="lower right", fontsize=14)
plt.show()

for循环中,我们使用StratifiedKFold函数进行5折交叉验证,并在每次交叉验证中计算 ROC 曲线的参数。我们使用np.interp函数将所有曲线插值到相同的假阳性率上,然后将真阳性率相加,计算平均值。最后,我们绘制平均 ROC 曲线。

结论

在本文中,我们使用 Python 构建了一个逻辑回归模型,并进行了5折交叉验证。我们绘制了5条 ROC 曲线,并计算了平均 ROC 曲线。通过绘制 ROC 曲线,我们可以评估模型的性能,并选择最佳的阈值来进行分类。

https://blog.csdn.net/PresleyR/article/details/116200387

生成低照度图像的方法有很多种,以下是一些常用方法:

  1. 基于Retinex算法的方法:使用Retinex算法对原始图像进行增强,使其在低照度环境下的亮度得到提升,从而生成低照度图像。

  2. 基于深度学习的方法:使用已有的低照度图像训练神经网络,利用生成对抗网络(GAN)等深度学习技术生成新的低照度图像。

  3. 基于物理模型的方法:通过对光线传播、反射和衰减等物理过程进行建模,生成低照度图像。

对于初学者来说,建议使用Retinex算法进行低照度图像的生成。以下是Python代码实现:

import cv2
import numpy as np

def simplest_cb(img, percent=1):
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    height, width = gray.shape[0:2]
    pixels = []
    for x in range(width):
        for y in range(height):
            pixels.append(gray[y][x])
    pixels.sort()
    perc = int(np.ceil(percent / 100.0 * len(pixels)))
    cmin = pixels[perc]
    cmax = pixels[-perc]
    for x in range(width):
        for y in range(height):
            if gray[y][x] < cmin:
                gray[y][x] = 0
            elif gray[y][x] > cmax:
                gray[y][x] = 255
            else:
                gray[y][x] = int(round((gray[y][x] - cmin) * 255.0 / (cmax - cmin)))
    return cv2.cvtColor(cv2.merge((gray, gray, gray)), cv2.COLOR_RGB2BGR)

def retinex(img, scale):
    img = np.float32(img) + 1.0
    max_scale = int(np.log(np.max(img)) / np.log(scale))
    gaussian_list = []
    for i in range(max_scale):
        gaussian = cv2.GaussianBlur(img, (0, 0), scale ** i)
        gaussian_list.append(gaussian)
    detail = np.zeros_like(gaussian_list[0])
    for i in range(len(gaussian_list) - 1):
        detail = cv2.subtract(gaussian_list[i], cv2.resize(gaussian_list[i + 1], (gaussian_list[i].shape[1], gaussian_list[i].shape[0])))
    return cv2.normalize(detail, None, 0, 255, cv2.NORM_MINMAX)

def low_light(img_path, save_path):
    img = cv2.imread(img_path)
    img_retinex = retinex(img, 1.3)
    img_low = simplest_cb(img_retinex, 1)
    cv2.imwrite(save_path, img_low)

代码中使用了Retinex算法对原始图像进行增强,并使用简单的对比度拉伸方法生成低照度图像。可以通过调整参数对生成的低照度图像进行不同程度的增强。

希望能对您有所帮助!

py 实现 合成低照度图像 用于神经元网络学习


图像处理神经网络python_图像处理与神经网络方法_weixin_39653622的博客-CSDN博客 我目前正在进行一个项目,以确定一个人的情绪/情绪。作为第一步,我们正在研究图像识别、检测和跟踪python代码。我尝试了各种不同的方法来解决这个问题,结果发现了。在1)Haar级联法(快速但没有识别和读取表达式的范围)。在2)神经网络(擅长图像识别,如微笑/愤怒等细节)。在我对神经网络感到困惑。在我们可以先使用haar-cascade轻松(非常快速)检测出人脸,然后使用canny边缘检测或裁剪来裁..._图像处理和神经网络 https://blog.csdn.net/weixin_39653622/article/details/109934221