知乎的水印是如何批量添加的?您想拥有这个本领吗?
我在opencv论坛发现了这个趣图添加logo的方法,也许您正需要这个代码,那我就诚心分享下吧。
如何删除结果图像中mainlogo.png周围的黑色边框?
import cv2
import numpy as np
import imutilsimg1 = cv2.imread('ralph.jpg')
overlay_img1 = np.ones(img1.shape, np.uint8)*255img2 = cv2.imread('mainlogo.png')
img2 = imutils.rotate_bound(img2, 10)
img2[np.where((img2==[0,0,0]).all(axis=2))] = [255,255,255] rows,cols,channels = img2.shapeoverlay_img1[0:rows, 0:cols ] = img2img2gray = cv2.cvtColor(overlay_img1,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 220, 255, cv2.THRESH_BINARY_INV)
mask_inv = cv2.bitwise_not(mask)temp1 = cv2.bitwise_and(img1,img1,mask = mask_inv)
temp2 = cv2.bitwise_and(overlay_img1, overlay_img1, mask = mask)
cv2.imshow('Temp2', temp2)
result = cv2.add(temp1,temp2)
cv2.imshow("Result",result)
cv2.imwrite("Result.jpg", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
ralph.jpg
mainlogo.png
Result:
可以在每个处理步骤之后添加imshow()来调试它吗?
(我几乎可以肯定,这些核心问题来自imutils.rotate()步骤)
解决方案:
https://mp.weixin.qq.com/s?__biz=MzIzMzYxNDIyOQ==&mid=2247484135&idx=1&sn=90524b02f0f3f9b22190131011e204e3&chksm=e883bc1ddff4350b557b6746ab733bb5aa5ce2e760b343bea15e4ccff57b7017604abdf541d1&token=1874966473&lang=zh_CN#rdmp.weixin.qq.com#!/usr/bin/env python35
#OpenCV 4.0.1, Raspberry pi3B/+, IDE 3.5.
#Date: 4th March, 2019import cv2
import numpy as npimg1 = cv2.imread('ralph.jpg')
overlay_img1 = np.ones(img1.shape,np.uint8)*255
img2 = cv2.imread('mainlogo.png')
rows,cols,channels = img2.shape
overlay_img1[0:rows, 0:cols ] = img2
img2gray = cv2.cvtColor(overlay_img1,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray,220,55,cv2.THRESH_BINARY_INV)
mask_inv = cv2.bitwise_not(mask)
temp1 = cv2.bitwise_and(img1,img1,mask = mask_inv)
temp2 = cv2.bitwise_and(overlay_img1,overlay_img1, mask = mask)
cv2.imshow('Temp2', temp2)
result = cv2.add(temp1,temp2)
cv2.imshow("Result",result)
cv2.imwrite("Result.jpg",result)
cv2.waitKey(0)
cv2.destroyAllWindows()