Python 如何使灰色图像的算法适应彩色图像?

Python 如何使灰色图像的算法适应彩色图像?,python,image-processing,Python,Image Processing,我一直在使用图像处理。这需要一个只有一个通道的图像(请参见assert len(image.shape)==2)如何调整代码以使其适用于彩色图像-因此图像有三个通道?最简单的方法是将图像分割为通道,然后在重新映射后,重新合并在一起 # make sure it's not gray assert len(image.shape) == 3 # grab the image resolution shape = image.shape[:2] # random_state, gaussian

我一直在使用图像处理。这需要一个只有一个通道的图像(请参见
assert len(image.shape)==2
)如何调整代码以使其适用于彩色图像-因此图像有三个通道?

最简单的方法是将图像分割为通道,然后在重新映射后,重新合并在一起

# make sure it's not gray
assert len(image.shape) == 3

# grab the image resolution
shape = image.shape[:2]

# random_state, gaussian and meshgrid
.
.
.

#calculate indices just as before
indices = np.reshape(x+dx, (-1, 1)), np.reshape(y+dy, (-1, 1))

# split the image into 3 channels
b, g, r = cv2.split(image)

# do the mapping on all of the channels separately
b = map_coordinates(b, indices, order=1).reshape(shape)
g = map_coordinates(g, indices, order=1).reshape(shape)
r = map_coordinates(r, indices, order=1).reshape(shape)

# return merged result
return cv2.merge((b,g,r))

这个问题有什么不好?这可能取决于选择的算法。你可以在每个颜色通道上分别应用算法,然后以某种方式合并,你可以在之前对图像进行灰度化,你可以在之前将颜色转换为不同的颜色空间或完全不同的东西。