问题描述
所以我尝试了不同的方式来处理当前图像。我有以下图像和图像的后续蒙版:
最近有人向我指出了一种方法,例如图像的对比度增强。我已经寻找了可能的方法来做到这一点,例如 hsv 拆分和应用蒙版,但没有得到我正在寻找的结果。有没有办法增加图像的对比度,使图像中具有显着性的区域更亮,而低显着性的区域则不那么亮。例如下图,我想尝试获得相同的结果。我查看了以下 Automatic contrast and brightness adjustment of a color photo of a sheet of paper with OpenCV,但在任何方面都不太走运。
解决方法
这是在 Python/OpenCV 中使用强度修改显着图的硬光合成。您可以调整 rescale_intensity 中的参数以根据需要进行调整。
图片:
显着性:
import cv2
import numpy as np
import skimage.exposure
# read image 1
img12 = cv2.imread('img12.png')
hh,ww = img12.shape[:2]
# read saliency mask as grayscale and resize to same size as img1
mask = cv2.imread('hard_light_mask.png')
mask = cv2.cvtColor(mask,cv2.COLOR_BGR2GRAY)
mask = cv2.resize(mask,(ww,hh))
mask = cv2.merge([mask,mask,mask])
# adjust mask contrast and brightness
mask = skimage.exposure.rescale_intensity(mask,in_range=(0,255),out_range=(92,192)).astype(np.uint8)
print(mask.dtype)
# threshold mask at mid gray and convert to 3 channels
thresh = cv2.threshold(mask,128,255,cv2.THRESH_BINARY)[1]
# do hard light composite of img12 and mask
# see CSS specs at https://www.w3.org/TR/compositing-1/#blendinghardlight
img12f = img12.astype(np.uint8)/255
maskf = mask.astype(np.uint8)/255
threshf = thresh.astype(np.uint8)/255
threshf_inv = 1 - threshf
low = 2.0 * img12f * maskf
high = 1 - 2.0 * (1-img12f) * (1-maskf)
result = ( 255 * (low * threshf_inv + high * threshf) ).clip(0,255).astype(np.uint8)
# save results
cv2.imwrite('img12_reduced_hardlight.png',result)
# show results
cv2.imshow('img12',img12)
cv2.imshow('mask',mask)
cv2.imshow('thresh',thresh)
cv2.imshow('result',result)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果: