一、图像的传统特征介绍
1.HOG
方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子。它通过计算和统计图像局部区域的梯度方向直方图来构成特征。例如,区别圆形的大小或者不一样的形状,此时有用的是边缘信息的特征,而图像的梯度(x和y导数)的大小在边缘和拐角处(突然强度变化的区域)很大,因此可以作为区分的特征。具体提取参考:
2.LBP
局部二值模式(Local Binary Pattern, LBP)是一种用来描述图像局部纹理特征的算子,它具有旋转不变性和灰度不变性等显著的优点。
3.Haar
Haar特征分为三类:边缘特征、线性特征、中心特征和对角线特征,组合成特征模板。特征模板内有白色和黑色两种矩形,并定义该模板的特征值为白色矩形像素和减去黑色矩形像素和。Haar特征值反映了图像的灰度变化情况。
注:参考链接:https://blog.csdn.net/q123456789098/article/details/52748918
二、传统的图像特征提取与深度学习对比
- 难点:传统的图像特征提取需要自主选择提取哪些特征,并需要相关领域知识,像角点、边缘、纹理,只有使用不同的特征才能更好地描述不同的类别,而在训练的时候,面对很多特征需要对大量参数进行微调。
- 优点:相比深度学习需要大量数据及长时间的训练且应用场景有限,像铝表面、布匹的缺陷瑕疵检测,使用传统的特征可以短时间、高效的完成。
- 深度学习:采用端到端的自动学习,提取出需要的特征,提供了最具描述性、显著性的特征。
- 理解传统的计算机视觉实际上真的有助于你更好的使用深度学习。例如,计算机视觉中最常见的神经网络是卷积神经网络。但是什么是卷积?它实际上是一种广泛使用的图像处理技术(例如Sobel边缘检测)。了解卷积有助于了解神经网络的内在机制,在解决问题时,它可以帮助你设计和调整模型。
- 参考https://blog.csdn.net/weixin_41036461/article/details/80587405
三、深度学习常用的数据增强方法
- 随机裁剪
- 颜色抖动,具体有:随机调整饱和度、亮度、对比度,也可以叫做扭转颜色
- 随机角度旋转
- 加入高斯噪声
- 图像翻转,镜像
import os import math import random import numpy as np from PIL import Image, ImageEnhance from multiprocessing import Pool def random_crop(img, size, scale=[0.08, 1.0], ratio=[3. / 4., 4. / 3.]): aspect_ratio = math.sqrt(random.uniform(*ratio)) w = 1. * aspect_ratio h = 1. / aspect_ratio bound = min((float(img.size[0]) / img.size[1]) / (w**2), (float(img.size[1]) / img.size[0]) / (h**2)) scale_max = min(scale[1], bound) scale_min = min(scale[0], bound) target_area = img.size[0] * img.size[1] * random.uniform(scale_min, scale_max) target_size = math.sqrt(target_area) w = int(target_size * w) h = int(target_size * h) i = random.randint(0, img.size[0] - w) j = random.randint(0, img.size[1] - h) img = img.crop((i, j, i + w, j + h)) img = img.resize((size, size), Image.LANCZOS) return img def randomColor(image): """ 对图像进行颜色抖动 :param image: PIL的图像image :return: 有颜色色差的图像image """ random_factor = np.random.randint(0, 31) / 10. # 随机因子 color_image = ImageEnhance.Color(image).enhance(random_factor) # 调整图像的饱和度 random_factor = np.random.randint(10, 21) / 10. # 随机因子 brightness_image = ImageEnhance.Brightness( color_image).enhance(random_factor) # 调整图像的亮度 random_factor = np.random.randint(10, 21) / 10. # 随机因1子 contrast_image = ImageEnhance.Contrast( brightness_image).enhance(random_factor) # 调整图像对比度 random_factor = np.random.randint(0, 31) / 10. # 随机因子 return ImageEnhance.Sharpness(contrast_image).enhance(random_factor) def randomRotation(image, mode=Image.BICUBIC): """ 对图像进行随机任意角度(0~360度)旋转 :param mode 邻近插值,双线性插值,双三次B样条插值(default) :param image PIL的图像image :return: 旋转转之后的图像 """ random_angle = np.random.randint(1, 360) return image.rotate(random_angle, mode) def distort_color(img): def random_brightness(img, lower=0.5, upper=1.5): e = random.uniform(lower, upper) return ImageEnhance.Brightness(img).enhance(e) def random_contrast(img, lower=0.5, upper=1.5): e = random.uniform(lower, upper) return ImageEnhance.Contrast(img).enhance(e) def random_color(img, lower=0.5, upper=1.5): e = random.uniform(lower, upper) return ImageEnhance.Color(img).enhance(e) ops = [random_brightness, random_contrast, random_color] random.shuffle(ops) img = ops[0](img) img = ops[1](img) img = ops[2](img) return img def randomGaussian(image, mean=0.2, sigma=0.3): """ 对图像进行高斯噪声处理 :param image: :return: """ def gaussianNoisy(im, mean=0.2, sigma=0.3): """ 对图像做高斯噪音处理 :param im: 单通道图像 :param mean: 偏移量 :param sigma: 标准差 :return: """ for _i in range(len(im)): im[_i] += random.gauss(mean, sigma) return im # 将图像转化成数组 img = np.asarray(image) img.flags.writeable = True # 将数组改为读写模式 width, height = img.shape[:2] img_r = gaussianNoisy(img[:, :, 0].flatten(), mean, sigma) img_g = gaussianNoisy(img[:, :, 1].flatten(), mean, sigma) img_b = gaussianNoisy(img[:, :, 2].flatten(), mean, sigma) img[:, :, 0] = img_r.reshape([width, height]) img[:, :, 1] = img_g.reshape([width, height]) img[:, :, 2] = img_b.reshape([width, height]) return Image.fromarray(np.uint8(img)) def randomFlip(image): #图像翻转(类似于镜像,镜子中的自己) #FLIP_LEFT_RIGHT,左右翻转 #FLIP_TOP_BOTTOM,上下翻转 #ROTATE_90, ROTATE_180, or ROTATE_270.按照角度进行旋转,与randomRotate()功能类似 return image.transpose(Image.FLIP_LEFT_RIGHT)
来源:https://www.cnblogs.com/seeney/p/9733143.html