OpenCV(Open Source Computer Vision Library)是一個跨平臺計算機視覺庫,實現了圖像處理和計算機視覺方面的不少通用算法python
#python沙箱環境 virtualenv opencvenv source opencvenv/bin/activate pip install cryptography ndg-httpsclient #某些python版本須要的幾個兼容包 pip install numpy #opencv的依賴 #我這裏直接pip安裝pyopencv不成功 #換其餘方法,直接從yum包裏拷貝opencv的python wrapper sudo yum install opencv-python cp /usr/lib64/python2.7/site-packages/cv* opencv_test/cvenv/lib64/python2.7/site-packages/
# -*- coding: utf-8 -*- import cv2 #矩陣形式讀取圖像爲三維列表,每一維分別表明 行、列、像素,像素維度列表中依次爲BGR三個顏色通道 img = cv2.imread("test.jpg") print img.shape #利用numpy的shape方法顯示圖像矩陣的三維尺寸信息,例如:(533, 800, 3) img2 = img.copy() #複製圖像 cv2.imwrite('save.jpg', img2) #保存
# -*- coding: utf-8 -*- import cv2 src = cv2.imread('src.jpg') # 默認的彩色圖(IMREAD_COLOR)方式讀入原始圖像 mask = cv2.imread('mask.jpg', cv2.IMREAD_GRAYSCALE) # 灰度圖(IMREAD_GRAYSCALE)方式讀入水印蒙版圖像 # 參數:目標修復圖像; 蒙版圖(定位修復區域); 選取鄰域半徑; 修復算法(包括INPAINT_TELEA/INPAINT_NS, 前者算法效果較好) dst = cv2.inpaint(src, mask, 3, cv2.INPAINT_TELEA) cv2.imwrite('result.jpg', dst)
效果演示
src圖:
mask圖:
算法
INPAINT_TELEA算法處理效果:
INPAINT_NS算法處理效果:
bash
細緻比較的話,INPAINT_TELEA算法處理,水印的隱去效果會稍好點app
參考自ps去水印原理,經過一張白底的反色水印圖來中和原圖水印python2.7
# -*- coding: utf-8 -*- import cv2 import numpy src = cv2.imread('src.jpg') mask = cv2.imread('mask.png') save = numpy.zeros(src.shape, numpy.uint8) #建立一張空圖像用於保存 for row in range(src.shape[0]): for col in range(src.shape[1]): for channel in range(src.shape[2]): if mask[row, col, channel] == 0: val = 0 else: reverse_val = 255 - src[row, col, channel] val = 255 - reverse_val * 256 / mask[row, col, channel] if val < 0: val = 0 save[row, col, channel] = val cv2.imwrite('result.jpg', save)
src圖:
反色水印圖:
輸出效果圖:
ui