美文网首页
Python+openCV:模拟数字水印的嵌入和提取

Python+openCV:模拟数字水印的嵌入和提取

作者: 洋阳酱 | 来源:发表于2020-09-30 21:17 被阅读0次

    最近在学习OpenCV,看的是微信读书里的《OpenCV轻松入门:面向Python》,根据书上的例题,做了一些小程序

    参考来源

    《OpenCV轻松入门:面向Python》例3.15

    加载原始图片

    我从网上找了一张上海的图片,加载成了黑白照片


    原始图
    oriImg = cv2.imread("shanghai.jpg", 0)  # 0表示将图片转为黑白
    r ,c = oriImg.shape
    cv2.namedWindow("origin",cv2.WINDOW_NORMAL)  # 设置窗口尺寸,避免图片太大,显示不全
    cv2.imshow("origin", oriImg)  # 显示图像
    cv2.imwrite('OriginImg.png',oriImg)  # 保存图像
    

    加载水印图片

    水印图

    水印照片的处理比原图复杂一点,主要是分为几步
    1)因为我找个这个图有点小,所以我把图片放大了一点

    watermark = cv2.imread("watermark.jpg", 0)
    r1 ,c1 = watermark.shape
    watermark = cv2.resize(watermark,(r1*6,c1*4))
    

    2)图片处理一下,把图片转成0和1两个值

    w1 = watermark[:,:]>200
    watermark[w1] = 0
    w2 = watermark[:,:]>200
    watermark[w2] = 1
    

    3)把水印图和原图做的一样大,方便后续处理

    watermarkImg = np.zeros((r,c), dtype=np.uint8)
    r1 ,c1 = watermark.shape
    watermarkImg[600:600+r1,2000:2000+c1] = watermark
    cv2.namedWindow("watermark",cv2.WINDOW_NORMAL)
    cv2.imshow("watermark", watermarkImg)
    cv2.imwrite('WatermarkImg.png',watermarkImg)
    
    黑白水印图

    水印嵌入

    t254 = np.ones((r,c), dtype=np.uint8)*254 
    imgH7 = cv2.bitwise_and(oriImg, t254)
    resultImg = cv2.bitwise_or(imgH7, watermarkImg)
    cv2.namedWindow("result",cv2.WINDOW_NORMAL)
    cv2.imshow("result",resultImg)
    cv2.imwrite('ResultImg.png',resultImg)
    
    加入水印

    水印解码

    t1 = np.ones((r,c), dtype=np.uint8)
    delImg = cv2.bitwise_and(resultImg, t1)
    w3 = delImg[:,:]>0
    delImg[w3] = 255
    cv2.namedWindow("delete",cv2.WINDOW_NORMAL)
    cv2.imshow("delete",delImg)
    cv2.imwrite('DeleteImg.png',delImg)
    
    提取水印

    完整代码

    import cv2
    import numpy as np
    
    # 加载原始图片
    oriImg = cv2.imread("shanghai.jpg", 0)
    r ,c = oriImg.shape
    cv2.namedWindow("origin",cv2.WINDOW_NORMAL)  # 设置窗口尺寸
    cv2.imshow("origin", oriImg)
    cv2.imwrite('OriginImg.png',oriImg)
    
    # 加载水印图片
    watermark = cv2.imread("watermark.jpg", 0)
    r1 ,c1 = watermark.shape
    watermark = cv2.resize(watermark,(r1*6,c1*4))
    w1 = watermark[:,:]>200
    watermark[w1] = 0
    w2 = watermark[:,:]>200
    watermark[w2] = 1
    watermarkImg = np.zeros((r,c), dtype=np.uint8)
    r1 ,c1 = watermark.shape
    watermarkImg[600:600+r1,2000:2000+c1] = watermark
    cv2.namedWindow("watermark",cv2.WINDOW_NORMAL)
    cv2.imshow("watermark", watermarkImg)
    cv2.imwrite('WatermarkImg.png',watermarkImg)
    
    # 水印嵌入
    t254 = np.ones((r,c), dtype=np.uint8)*254 
    imgH7 = cv2.bitwise_and(oriImg, t254)
    resultImg = cv2.bitwise_or(imgH7, watermarkImg)
    cv2.namedWindow("result",cv2.WINDOW_NORMAL)
    cv2.imshow("result",resultImg)
    cv2.imwrite('ResultImg.png',resultImg)
    
    # 水印解码
    t1 = np.ones((r,c), dtype=np.uint8)
    delImg = cv2.bitwise_and(resultImg, t1)
    w3 = delImg[:,:]>0
    delImg[w3] = 255
    cv2.namedWindow("delete",cv2.WINDOW_NORMAL)
    cv2.imshow("delete",delImg)
    cv2.imwrite('DeleteImg.png',delImg)
    
    cv2.waitKey()
    cv2.destroyAllWindows()
    

    相关文章

      网友评论

          本文标题:Python+openCV:模拟数字水印的嵌入和提取

          本文链接:https://www.haomeiwen.com/subject/msypuktx.html