YOLO11结合传统图像增强算法 | 让你的模型无惧黑暗 | 包含端到端推理代码 | 低照度图像增强
原图 | YOLO11目标检测 | 传统图像增强 + YOLO11目标检测 |
目录
一 低照度图像增强
二 YOLO11
三 YOLO11结合传统图像增强算法
① 代码
② 效果图
一 低照度图像增强
【低照度图像增强系列】使用SSR/MSR/MSRCR/MSRCP/automatedMSRCR算法对低照度图像进行增强处理的实践 - 点击此处即可跳转
二 YOLO11
【YOLOv11】ultralytics最新作品yolov11 AND 模型的训练、推理、验证、导出 以及 使用 - 点击此处即可跳转
安装 YOLO11:
# Clone the ultralytics repository
git clone https://github.com/ultralytics/ultralytics.git
# 创建conda 环境yolov11
conda create -n yolov11 python=3.9
conda activate yolov11
# Navigate to the cloned directory
cd ultralytics
# Install the package in editable mode for development
pip install -e . -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install onnx -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install onnxslim -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install onnxruntime -i https://pypi.tuna.tsinghua.edu.cn/simple
# opencv
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install opencv-contrib-python -i https://pypi.tuna.tsinghua.edu.cn/simple
三 YOLO11结合传统图像增强算法
YOLO11结合传统图像增强算法,让你的模型无惧黑暗。
① 代码
from ultralytics import YOLO
import cv2
import numpy as np
def singleScaleRetinex(img, sigma):
# cv2.GaussianBlur( SRC,ksize,sigmaX [,DST [,sigmaY [,borderType ] ] ] ) → DST
# ksize为高斯核大小。 width、height 可以不同,但必须是正数和奇数。它们可以为零,此时会根据给定的sigma大小自动计算一个尺寸。
# 参数sigmaX是必填参数,x方向的标准差,决定了卷积核x方向高斯分布的形状。这个sigmaX值越小,则核中心值越大,周围值下降的越快(表现为高瘦);
# 相反,这个值越大,则核中心值越小,周围值下降的越慢(表现为矮胖)。
retinex = np.log10(img) - np.log10(cv2.GaussianBlur(img, (0, 0), sigma))
return retinex
def multiScaleRetinex(img, sigma_list):
retinex = np.zeros_like(img)
for sigma in sigma_list:
retinex += singleScaleRetinex(img, sigma)
retinex = retinex / len(sigma_list)
return retinex
def colorRestoration(img, alpha, beta):
img_sum = np.sum(img, axis=2, keepdims=True)
color_restoration = beta * (np.log10(alpha * img) - np.log10(img_sum))
return color_restoration
def simplestColorBalance(img, low_clip, high_clip):
total = img.shape[0] * img.shape[1]
for i in range(img.shape[2]):
# # np.unique对数据去重后从小到大排序
unique, counts = np.unique(img[:, :, i], return_counts=True)
current = 0
for u, c in zip(unique, counts):
if float(current) / total < low_clip:
low_val = u
if float(current) / total < high_clip:
high_val = u
current += c
img[:, :, i] = np.maximum(np.minimum(img[:, :, i], high_val), low_val)
return img
def MSRCP(img, sigma_list, low_clip, high_clip):
img = np.float64(img) + 1.0
intensity = np.sum(img, axis=2) / img.shape[2]
retinex = multiScaleRetinex(intensity, sigma_list)
intensity = np.expand_dims(intensity, 2)
retinex = np.expand_dims(retinex, 2)
intensity1 = simplestColorBalance(retinex, low_clip, high_clip)
intensity1 = (intensity1 - np.min(intensity1)) / (np.max(intensity1) - np.min(intensity1)) * 255.0 + 1.0
img_msrcp = np.zeros_like(img)
for y in range(img_msrcp.shape[0]):
for x in range(img_msrcp.shape[1]):
B = np.max(img[y, x])
A = np.minimum(256.0 / B, intensity1[y, x, 0] / intensity[y, x, 0])
img_msrcp[y, x, 0] = A * img[y, x, 0]
img_msrcp[y, x, 1] = A * img[y, x, 1]
img_msrcp[y, x, 2] = A * img[y, x, 2]
img_msrcp = np.uint8(img_msrcp - 1.0)
return img_msrcp
def deal_img_Result(imgpath):
# 加载图像
im = cv2.imread(imgpath)
MSRCP_Result = MSRCP(im, [15, 80, 100], 0.01, 0.99)
cv2.imwrite("tmp.jpg", MSRCP_Result)
pass
def imgEnhanceYolo(model_path, img_path):
deal_img_Result(img_path)
model = YOLO(model_path) # load a custom model
# Predict with the model
results = model("tmp.jpg", conf=0.3)
for result in results:
# boxes = result.boxes # Boxes object for bounding box outputs
# masks = result.masks # Masks object for segmentation masks outputs
# keypoints = result.keypoints # Keypoints object for pose outputs
# probs = result.probs # Probs object for classification outputs
# obb = result.obb # Oriented boxes object for OBB outputs
result.show(line_width=1, font_size=1, color_mode='class') # display to screen
result.save(filename="enhance_result.jpg") # save to disk
pass
def Onlyyolo(model_path, img_path):
# Load a model
model = YOLO(model_path) # load a custom model
# Predict with the model
results = model.predict(img_path, conf=0.3)
# Process results list
for result in results:
result.show(line_width=1, font_size=1, color_mode='class') # display to screen
result.save(filename="result.jpg") # save to disk
pass
if __name__ == '__main__':
Onlyyolo("yolo11s.pt", "data/difficult/test1.jpg")
imgEnhanceYolo("yolo11s.pt", "data/difficult/test1.jpg")
pass
② 效果图
单独YOLO检测结果:
将原始图像经过传统图像增强算法处理后,YOLO11检测结果如下:
至此,本文分享的内容就结束啦。
如果对YOLO11结合传统图像增强算法的实践感兴趣,欢迎留言,后续会持续更新。遇见便是缘,感恩遇见!