Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将灰色图像转换为蓝色和红色图像_Python_Opencv - Fatal编程技术网

Python 将灰色图像转换为蓝色和红色图像

Python 将灰色图像转换为蓝色和红色图像,python,opencv,Python,Opencv,我想知道如何拍摄灰度图像,将黑色像素变成红色,白色像素变成蓝色?所有其他看起来是灰色(不是完全黑色或白色)的像素应该是从红色到蓝色的过渡 我尝试了以下方法:拍摄灰度图像,将其转换为RGB,然后尝试删除绿色通道。但这张照片看起来只是略带粉红色: im = cv2.imread('grey_img.jpg') im[:,:,1] = 0 那么,如何将灰色图像转换为蓝色到红色的图像呢?删除色带将无法实现您所描述的效果,因为您正在尝试对图像着色,而不是将其取消着色。一个像素级函数来决定如何处理每个像素

我想知道如何拍摄灰度图像,将黑色像素变成红色,白色像素变成蓝色?所有其他看起来是灰色(不是完全黑色或白色)的像素应该是从红色到蓝色的过渡

我尝试了以下方法:拍摄灰度图像,将其转换为RGB,然后尝试删除绿色通道。但这张照片看起来只是略带粉红色:

im = cv2.imread('grey_img.jpg')
im[:,:,1] = 0

那么,如何将灰色图像转换为蓝色到红色的图像呢?

删除色带将无法实现您所描述的效果,因为您正在尝试对图像着色,而不是将其取消着色。一个像素级函数来决定如何处理每个像素是解决这个问题的好方法


这里有一种方法可以使用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

输入:



我学了一些数学知识,它们很优雅:

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])
结果:


你可以通过改变百分位数或目标空间点来调整亮度

我的意思是说灰度图像。不是纯白色和纯黑色的图像。我将把我的图像转换成灰度,我想我可以使用和你提到的相同的概念,但是我将用灰度像素的强度来量化它,而不是阈值。这可能会给我带来转变,而不仅仅是一个蓝色或红色的二进制。是的,策略是一样的。您只需使用灰度值分别确定蓝色和红色值像素的“亮度”和“黑暗度”。我更新了我的函数来为您执行此操作。此函数不关心原始图像是灰色还是彩色。
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()
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])