OpenCV提取图像的位平面

提取位平面函数(仅仅支持灰度图像)

def extractBitPlace(img,layer):
    h,w=img.shape
    mat=np.ones((h,w),np.uint8)
    mat=mat*(2**(layer-1))
    return cv2.bitwise_and(img,mat)

提取得到的某层位平面结果还不可以直接显示出来,因为值太小,很难显示出区分度来,所以需要进行阈值处理,转化成0,255的二值图像。
完整代码:

import cv2
import numpy as np
"""
提取位平面
img 输入图像
layer 提取第几层(1-8)
"""
def extractBitPlace(img,layer):
    h,w=img.shape
    mat=np.ones((h,w),np.uint8)
    mat=mat*(2**(layer-1))
    return cv2.bitwise_and(img,mat)
"""
阈值处理
"""
def thresholdProcessing(img):
    img[img>0]=255
    return img
# 读取原始图像
lena = cv2.imread("D:\C\copy\lenacolor.png", 0)
img=thresholdProcessing(extractBitPlace(lena,8))
cv2.imshow("lena",lena)
cv2.imshow("img",img)
cv2.waitKey()
cv2.destroyAllWindows()

image

posted @ 2022-11-18 22:20  请去看诡秘之主  阅读(57)  评论(0编辑  收藏  举报