# 将多个灰度像素打包到一个整数中
def pack_pixels(pixels, threshold):
value = 0
for gray in pixels:
bit = 1 if gray >= threshold else 0 # 二值化
value = (value << 1) + bit # 多个二值化像素值拼接为一个字节值
return value
for r in range(height):
for b in range(int(width / PIXEL_PER_BYTE)):
colStart = b * PIXEL_PER_BYTE
pixels = frame[r, colStart: colStart + PIXEL_PER_BYTE]
byte = pack_pixels(pixels, threshold)
data.append(byte)
if DEBUG:
global frameCount
cv2.imwrite(os.path.join('debug', str(frameCount) + '.png'), frame)
cv2.imwrite(os.path.join('debug', str(frameCount) + '-bin.png'), binary)
frameCount += 1
end = datetime.now()
print('time cost:', end - start)
return bytes(data)