600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 图像处理之纹理特征提取

图像处理之纹理特征提取

时间:2020-05-04 07:01:04

相关推荐

图像处理之纹理特征提取

旋转不变性:图像旋转时,所选特征不随图像的旋转而发生变化

LBP参考:

LBP纹理特征提取

灰度不变性-旋转不变性

import numpy as npfrom PIL import Imageimport math def LBP(src):''':param src:灰度图像:rtype:灰度图像'''src = np.array(src)height = src.shape[0]width = src.shape[1]dst = src.copy()lbp_value = np.zeros((1,8), dtype=np.uint8)neighbours = np.zeros((1,8), dtype=np.uint8)# height as row, width as columnfor x in range(1, width-1):for y in range(1, height-1):neighbours[0, 0] = src[y - 1, x - 1]neighbours[0, 1] = src[y - 1, x]neighbours[0, 2] = src[y - 1, x + 1]neighbours[0, 3] = src[y, x - 1]neighbours[0, 4] = src[y, x + 1]neighbours[0, 5] = src[y + 1, x - 1]neighbours[0, 6] = src[y + 1, x]neighbours[0, 7] = src[y + 1, x + 1]center = src[y, x]for i in range(8):if neighbours[0, i] > center:lbp_value[0, i] = 1else:lbp_value[0, i] = 0lbp = lbp_value[0, 0] * 1 + lbp_value[0, 1] * 2 + lbp_value[0, 2] * 4 + lbp_value[0, 3] * 8 \+ lbp_value[0, 4] * 16 + lbp_value[0, 5] * 32 + lbp_value[0, 6] * 64 + lbp_value[0, 0] * 128dst[y, x] = lbpdst = Image.fromarray(dst)return dstdef circular_LBP(src, radius, n_points):src = np.array(src)height = src.shape[0]width = src.shape[1]dst = src.copy()src.astype(dtype=np.float32)dst.astype(dtype=np.float32)neighbours = np.zeros((1, n_points), dtype=np.uint8)lbp_value = np.zeros((1, n_points), dtype=np.uint8)for x in range(radius, width - radius - 1):for y in range(radius, height - radius - 1):lbp = 0.# 先计算共n_points个点对应的像素值,使用双线性插值法for n in range(n_points):theta = float(2 * np.pi * n) / n_pointsx_n = x + radius * np.cos(theta)y_n = y - radius * np.sin(theta)# 向下取整x1 = int(math.floor(x_n))y1 = int(math.floor(y_n))# 向上取整x2 = int(math.ceil(x_n))y2 = int(math.ceil(y_n))# 将坐标映射到0-1之间tx = np.abs(x - x1)ty = np.abs(y - y1)# 根据0-1之间的x,y的权重计算公式计算权重w1 = (1 - tx) * (1 - ty)w2 = tx * (1 - ty)w3 = (1 - tx) * tyw4 = tx * ty# 根据双线性插值公式计算第k个采样点的灰度值neighbour = src[y1, x1] * w1 + src[y2, x1] * w2 + src[y1, x2] * w3 + src[y2, x2] * w4neighbours[0, n] = neighbourcenter = src[y, x]for n in range(n_points):if neighbours[0, n] > center:lbp_value[0, n] = 1else:lbp_value[0, n] = 0for n in range(n_points):lbp += lbp_value[0, n] * 2**n# 转换到0-255的灰度空间,比如n_points=16位时结果会超出这个范围,对该结果归一化dst[y, x] = int(lbp / (2**n_points-1) * 255)dst = Image.fromarray(dst)return dstdef value_rotation(num):value_list = np.zeros((8), np.uint8)temp = int(num)value_list[0] = tempfor i in range(7):temp = ((temp << 1) | (temp / 128)) % 256value_list[i+1] = temp#print value_listreturn np.min(value_list)def rotation_invariant_LBP(src):src = np.array(src)height = src.shape[0]width = src.shape[1]dst = src.copy()lbp_value = np.zeros((1, 8), dtype=np.uint8)neighbours = np.zeros((1, 8), dtype=np.uint8)for x in range(1, width - 1):for y in range(1, height - 1):neighbours[0, 0] = src[y - 1, x - 1]neighbours[0, 1] = src[y - 1, x]neighbours[0, 2] = src[y - 1, x + 1]neighbours[0, 3] = src[y, x - 1]neighbours[0, 4] = src[y, x + 1]neighbours[0, 5] = src[y + 1, x - 1]neighbours[0, 6] = src[y + 1, x]neighbours[0, 7] = src[y + 1, x + 1]center = src[y, x]for i in range(8):if neighbours[0, i] > center:lbp_value[0, i] = 1else:lbp_value[0, i] = 0lbp = lbp_value[0, 0] * 1 + lbp_value[0, 1] * 2 + lbp_value[0, 2] * 4 + lbp_value[0, 3] * 8 \+ lbp_value[0, 4] * 16 + lbp_value[0, 5] * 32 + lbp_value[0, 6] * 64 + lbp_value[0, 0] * 128# 旋转不变值dst[y, x] = value_rotation(lbp)dst = Image.fromarray(dst)return dstif __name__ == '__main__':I = Image.open('./test/130014.jpg').convert('L')I.show()I_lbp = LBP(I)I_lbp.show()I_lbp_circle = circular_LBP(I, 2, 16)I_lbp_circle.show()I_lbp_rotate_invariant = rotation_invariant_LBP(I)I_lbp_rotate_invariant.show()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。