cv2中的PIL ImageChops.screen

时间:2018-02-21 12:14:26

标签: python image-processing python-imaging-library cv2

如何从cv2中的PIL中获取相同的代码

img3 = ImageChops.screen(im1, im2)

2 个答案:

答案 0 :(得分:1)

您可以使用ImageChops.screen使用的公式来实现它:

  

out = MAX-(((MAX-image1)*(MAX-image2)/ MAX)(source

代码:

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值相乘,因此必须提升到uint18 s,最后我将其强制转换回uint8 s,因为可以保证这些值是再次< 256

答案 1 :(得分:0)

  

屏幕将两个倒置的图像彼此叠加(source

您也可以这样做(无需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)