首页 > 解决方案 > 从二进制 8 位图像访问 LSB 和 MSB

问题描述

我正在研究问题所在的加密:

对于图像的每个像素,将像素十进制值转换为二进制 8 位。然后将每个 8 位像素的 MSB 和 LSB 分开,并将其转换为十进制。

我已将图像转换为每个像素的 8 位二进制文​​件。现在我不知道如何分离 MSB 和 LSB。

这是我的代码:

#taking input image
input = cv2.imread('test.webp')
#print("before\n\n")
cv2.imshow('image before',input)


#Get input size converting to a fixed size

height, width = input.shape\[:2\]

#Desired "pixelated" size

w, h = (256, 256)

#Resize input to "pixelated" size

temp = cv2.resize(input, (w, h), interpolation=cv2.INTER_LINEAR)

print(temp)
#Initialize output image

#the converted pixed into 8bit image
output = cv2.resize(temp, (width, height), interpolation=cv2.INTER_NEAREST)
cv2.imwrite("pixelate.jpg", output)
#print(output)
#output
print('after\n\n')
cv2.imshow('image after',output)



cv2.waitKey(0)

cv2.destroyAllWindows()][1]

标签: python-3.xopencvimage-processingbinary8-bit

解决方案


首先,读取图像并打印其数据类型

img = cv2.imread('test.webp')
print(img.dtype)

如果数据类型不是uint8,则将图像缩放到0-255并将其数据类型更改为uint8

现在,屏蔽每个像素的第一个(LSB)和最后一个(MSB)位

import numpy as np
mask = np.full(img.shape, 0x7E, dtype=np.uint8)

res = np.bitwise_and(img, mask)

import matplotlib.pyplot as plt
plt.imshow(res)

推荐阅读