3 回答

TA貢獻1796條經驗 獲得超4個贊
我接觸到了一些數學方面的東西,它們很優雅:
img = cv2.imread('images/lena.png', cv2.IMREAD_GRAYSCALE)
# find some percentiles for grayscale range of src image
percentiles = np.percentile(img, [0, 25, 75, 100])
# define the same count of values to further interpolation
targets = np.geomspace(10, 255, 4)
# use interpolation from percentiles to targets for blue and red
b = np.interp(img, percentiles, targets).astype(np.uint8)
g = np.zeros_like(img)
r = np.interp(img, percentiles, targets[::-1]).astype(np.uint8)
# merge channels to BGR image
result = cv2.merge([b, g, r])
結果:
您可以通過更改百分位數或目標空間點來調整亮度

TA貢獻1887條經驗 獲得超5個贊
刪除色帶不會實現您所描述的效果,因為您正在嘗試對圖像進行著色,而不是對其進行脫色。決定如何處理每個像素的像素級函數是解決此問題的好方法。
from PIL import Image
def pixop_redblue(pixel):
pixel, alpha = pixel[:3], pixel[3:]
grey = sum(pixel) // len(pixel)
redvalue = 255 - grey # "darkness"
bluevalue = grey # "brightness"
return (redvalue, 0, bluevalue) + alpha
img = Image.open('trees.jpg')
img2 = img.copy()
img2.putdata([pixop_redblue(pixel) for pixel in img.getdata()])
img2.show()

TA貢獻1797條經驗 獲得超6個贊
以下是使用 Python/OpenCV 將漸變顏色應用于灰度圖像的一種方法。
- Load the grayscale image
- Convert it to 3 equal channels (only if image is 1 channel grayscale already)
- Create a 1 pixel red image
- Create a 1 pixel blue image
- Concatenate the two
- Resize linearly to 256 pixels as a Lookup Table (LUT)
- Apply the LUT
- Save the result
輸入:
import cv2
import numpy as np
# load image as grayscale
img = cv2.imread('lena_gray.png', cv2.IMREAD_GRAYSCALE)
# convert to 3 equal channels (only if img is already 1 channel grayscale)
img = cv2.merge((img, img, img))
# create 1 pixel red image
red = np.zeros((1, 1, 3), np.uint8)
red[:] = (0,0,255)
# create 1 pixel blue image
blue = np.zeros((1, 1, 3), np.uint8)
blue[:] = (255,0,0)
# append the two images
lut = np.concatenate((red, blue), axis=0)
# resize lut to 256 values
lut = cv2.resize(lut, (1,256), interpolation=cv2.INTER_CUBIC)
# apply lut
result = cv2.LUT(img, lut)
# save result
cv2.imwrite('lena_red_blue_lut_mapped.png', result)
# display result
cv2.imshow('RESULT', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
添加回答
舉報