目录
- 比特平面分层
比特平面分层
在一幅256级灰度图像中,图像的值是由8比特(1字节)组成的
def convert_bin(data, n):"""convert decimal to binary, return n th bit, 0 if bit value 0 else 1"""
#------------------numpy------------x = np.binary_repr(data, width=8)x = x[::-1]a = x[n]new_data = int(a)# -----------------self----------------
# x = bin(data)[2:]
# x = '0' * (8 - len(x)) + x
# x = x[::-1]
# a = x[n]
# new_data = int(a)return new_data
def bit_seperate(img, func, n):new_img = np.zeros(img.shape, dtype=np.uint8)for i in range(img.shape[0]):for j in range(img.shape[1]):new_img[i, j] = func(img[i, j], n)return new_img
# 比特平面分层
img = cv2.imread("./DIP_Figures/DIP3E_Original_Images_CH01/Fig0122(a)(fractal-iris).tif", 0)
fig = plt.figure(figsize=(15, 12))
for i in range(9, 0, -1):ax = fig.add_subplot(3, 3, (9-i)+1, xticks=[], yticks=[])if i == 9:ax.imshow(img, cmap='gray'), plt.title('Original')else:img_0 = bit_seperate(img, convert_bin, (i - 1))ax.imshow(img_0, cmap='gray')ax.set_title(f"{bin((i - 1))}")plt.tight_layout()
plt.show()