gpt4 book ai didi

python - 如何使用 OpenCV 去除灰度图像的七个低位

转载 作者:行者123 更新时间:2023-12-02 16:14:05 26 4
gpt4 key购买 nike

我编写了一个脚本来删除 7 个最低有效位,只保留 8 位灰度图像的每个像素的 MBS。为此,我使用 0b10000000 屏蔽每个像素,但没有获得预期的输出。

import cv2
import numpy as np

imageSource = 'input.jpg'
original_img = cv2.imread(imageSource,cv2.COLOR_BGR2GRAY)
cv2.imshow( "original", original_img )
result = original_img & 0b10000000
cv2.imshow( "out", result )
cv2.imwrite('out.jpg',result)
cv2.waitKey(0)
cv2.destroyAllWindows()

原图: /image/r597Y.png

我的代码的输出: enter image description here

期望的结果: enter image description here

最佳答案

所以你基本上想要所有的值:

  • 大于 127 (128 = 0b1000000 到 255 = 0b11111111) 为 255,
  • 小于或等于 127 (0 = 0b00000000 到 127 = 0b01111111) 为 0。

您可以避免二进制 AND &,而只需使用 threshold 函数:

_, result = cv2.threshold(result, 127, 255, cv2.THRESH_BINARY)

关于python - 如何使用 OpenCV 去除灰度图像的七个低位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46800380/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com