亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將灰色圖像轉換為藍色和紅色圖像

將灰色圖像轉換為藍色和紅色圖像

當年話下 2023-09-05 21:04:12
我想知道如何拍攝灰度圖像并將暗像素變為紅色,將白色像素變為藍色?所有其他看起來灰色的像素(不是完全黑色或白色)應該是從紅色到藍色的過渡。我嘗試了以下方法:獲取灰度圖像,將其轉換為 RGB,然后嘗試刪除綠色通道。但圖像看起來只是粉紅色:im = cv2.imread('grey_img.jpg') im[:,:,1] = 0那么如何將灰色圖像變成藍色到紅色的圖像呢?
查看完整描述

3 回答

?
SMILET

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])

結果:

https://img1.sycdn.imooc.com//64f7277c00016b7d02540257.jpg

您可以通過更改百分位數或目標空間點來調整亮度



查看完整回答
反對 回復 2023-09-05
?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

刪除色帶不會實現您所描述的效果,因為您正在嘗試對圖像進行著色,而不是對其進行脫色。決定如何處理每個像素的像素級函數是解決此問題的好方法。

https://img1.sycdn.imooc.com//64f7278e00014e2103990299.jpg

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()

https://img1.sycdn.imooc.com//64f7279f0001950e03960294.jpg

查看完整回答
反對 回復 2023-09-05
?
FFIVE

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


輸入:

https://img1.sycdn.imooc.com//64f727b200010c9402550257.jpg

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()

https://img1.sycdn.imooc.com//64f727c10001498202540256.jpg

查看完整回答
反對 回復 2023-09-05
  • 3 回答
  • 0 關注
  • 540 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號