Python cv2中的PIL ImageCups.screen
如何在cv2中使用来自PIL的相同代码Python cv2中的PIL ImageCups.screen,python,opencv,image-processing,python-imaging-library,cv2,Python,Opencv,Image Processing,Python Imaging Library,Cv2,如何在cv2中使用来自PIL的相同代码 img3 = ImageChops.screen(im1, im2) 您可以使用ImageChops.screen使用的公式来实现它: out=MAX-((MAX-image1)*(MAX-image2)/MAX)() 守则: import cv2 import numpy as np im1 = cv2.imread('im1.png').astype(np.uint16) im2 = cv2.imread('im2.png').astype(np.
img3 = ImageChops.screen(im1, im2)
您可以使用
ImageChops.screen
使用的公式来实现它:
out=MAX-((MAX-image1)*(MAX-image2)/MAX)()
守则:
import cv2
import numpy as np
im1 = cv2.imread('im1.png').astype(np.uint16)
im2 = cv2.imread('im2.png').astype(np.uint16)
im3 = (255 - ((255 - im1) * (255 - im2) / 255)).astype(np.uint8)
cv2.imwrite('im3.png', im3)
升级到uint16
s是必要的,因为两个uint18
值相乘,最后我将其重新转换为uint8
s,因为这些值保证再次是<256
屏幕将两个倒置的图像叠加在一起()
您也可以这样做(无需numpy
):
import cv2
# read the input images, they can be color (RGB) images too
im1 = cv2.imread('im1.jpg')
im2 = cv2.imread('im2.jpg')
# images must be of same size, if not resize one of the images
if im1.shape != im2.shape:
im2 = cv2.resize(im2, im1.shape[:2][::-1], interpolation = cv2.INTER_AREA)
# invert and normalize first image
im1 = cv2.normalize(cv2.bitwise_not(im1), None, 0, 1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
# invert and normalize second image
im2 = cv2.normalize(cv2.bitwise_not(im2), None, 0, 1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
# superimpose two images, re-normalize and invert
im = cv2.bitwise_not(cv2.normalize(cv2.multiply(im1, im2), None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U))
# write the output image
cv2.imwrite('im.jpg', im)