文章目录
- cv2.applyColorMap
- cv2.putText
- 小试牛刀
- 自定义颜色
参考学习来自 OpenCV基础(21)使用 OpenCV 中的applyColorMap实现伪着色
cv2.applyColorMap
cv2.applyColorMap() 是 OpenCV 中的一个函数,用于将灰度图像或单通道图像应用一个颜色映射,以产生伪彩色或彩色图像。这在可视化、增强图像的可读性或用于某些特定的图像分析任务时非常有用。
函数的原型如下:
cv2.applyColorMap(src, colormap[, dst])
-
src: 输入图像,应为 8 位单通道或 32 位浮点单通道图像。
-
colormap: 要应用的颜色映射类型。OpenCV 提供了多种预定义的颜色映射
COLORMAP_AUTUMN (编号 0):秋季风格的颜色映射。
COLORMAP_BONE (编号 1):骨骼风格的颜色映射。
COLORMAP_JET (编号 2):JET 风格的颜色映射,常用于科学可视化。
COLORMAP_WINTER (编号 3):冬季风格的颜色映射。
COLORMAP_RAINBOW (编号 4):彩虹风格的颜色映射。
COLORMAP_OCEAN (编号 5):海洋风格的颜色映射。
COLORMAP_SUMMER (编号 6):夏季风格的颜色映射。
COLORMAP_SPRING (编号 7):春季风格的颜色映射。
COLORMAP_COOL (编号 8):冷色调风格的颜色映射。
COLORMAP_HSV (编号 9):HSV 风格的颜色映射。
COLORMAP_PINK (编号 10):粉红色调的颜色映射。
COLORMAP_HOT (编号 11):热色调风格的颜色映射。
COLORMAP_PARULA (编号 12):Parula 风格的颜色映射。
COLORMAP_MAGMA (编号 13):Magma 风格的颜色映射。
COLORMAP_INFERNO (编号 14):Inferno 风格的颜色映射。
COLORMAP_PLASMA (编号 15):Plasma 风格的颜色映射。
COLORMAP_VIRIDIS (编号 16):Viridis 风格的颜色映射,设计用于在色彩视觉缺陷的情况下仍具有良好的可区分性。
COLORMAP_CIVIDIS (编号 17):Cividis 风格的颜色映射,类似于 Viridis,但颜色更加生动。
COLORMAP_TWILIGHT (编号 18):Twilight 风格的颜色映射。
COLORMAP_TWILIGHT_SHIFTED (编号 19):Twilight Shifted 风格的颜色映射。
COLORMAP_TURBO (编号 20):Turbo 风格的颜色映射。
COLORMAP_DEEPGREEN (编号 21):深绿色调的颜色映射。 -
dst: 可选参数,输出图像。如果未提供,则创建一个新的图像。
下面是一个简单的示例,展示如何使用 cv2.applyColorMap():
import cv2
import numpy as np # 读取一张灰度图像
gray_img = cv2.imread('gray_image.png', cv2.IMREAD_GRAYSCALE) # 应用 JET 颜色映射
colored_img = cv2.applyColorMap(gray_img, cv2.COLORMAP_JET) # 显示彩色图像
cv2.imshow('Colored Image', colored_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
在这个示例中,我们首先读取一张灰度图像,然后使用 cv2.applyColorMap() 函数将其转换为 JET 颜色映射的彩色图像,并显示出来。
cv2.putText
cv2.putText 是 OpenCV 库中的一个函数,用于在图像上添加文本。
一、函数原型
cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
二、参数说明
-
img:图像。
-
text:要添加的文本字符串。
-
org:文本字符串左下角的坐标,通常是一个元组,如 (x, y)
-
fontFace:字体类型。OpenCV 提供了几种预定义的字体,或者你也可以使用 Hershey 字体。
-
fontScale:字体大小的比例因子。
-
color:文本的颜色。对于 BGR,传递一个元组。
-
thickness:可选参数,线条粗细。如果为负值(如 -1),则填充文本。
-
lineType:可选参数,线型。例如 cv2.LINE_AA 表示抗锯齿线型。
-
bottomLeftOrigin:当标志为真时,图像数据的原点位于左下角。否则,它位于左上角。
小试牛刀
import cv2
import numpy as npdef colormap_name(id):switcher = {0: "COLORMAP_AUTUMN",1: "COLORMAP_BONE",2: "COLORMAP_JET",3: "COLORMAP_WINTER",4: "COLORMAP_RAINBOW",5: "COLORMAP_OCEAN",6: "COLORMAP_SUMMER",7: "COLORMAP_SPRING",8: "COLORMAP_COOL",9: "COLORMAP_HSV",10: "COLORMAP_PINK",11: "COLORMAP_HOT"}return switcher.get(id, "NONE")if __name__ == '__main__':row = 3col = 4im = cv2.imread("./David.jpg", cv2.IMREAD_GRAYSCALE)h, w = im.shapeim_out = np.zeros((row*h, col*w, 3), np.uint8)for i in range(0, col):for j in range(0, row):k = i + j * colim_color = cv2.applyColorMap(im, k)cv2.putText(im_color, colormap_name(k), (20, w-20), cv2.FONT_HERSHEY_DUPLEX, 0.75,(255, 255, 255), 2, cv2.LINE_AA)sub_i = i * wsub_j = j * him_out[sub_j: sub_j + h, sub_i: sub_i + w, :] = im_colorcv2.imwrite("PseudoColored.jpg", im_out)cv2.imshow("Pseudo Colored", im_out)cv2.waitKey(0)
输入图片
输出结果
自定义颜色
import cv2
import numpy as npdef applyCustomColorMap(im_gray):lut = np.zeros((256, 1, 3), dtype=np.uint8)lut[:, 0, 0] = [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 253, 251, 249, 247, 245, 242, 241, 238, 237, 235, 233, 231, 229, 227, 225,223, 221, 219, 217, 215, 213, 211, 209, 207, 205, 203, 201, 199, 197, 195, 193, 191, 189, 187, 185,183, 181, 179, 177, 175, 173, 171, 169, 167, 165, 163, 161, 159, 157, 155, 153, 151, 149, 147, 145,143, 141, 138, 136, 134, 132, 131, 129, 126, 125, 122, 121, 118, 116, 115, 113, 111, 109, 107, 105,102, 100, 98, 97, 94, 93, 91, 89, 87, 84, 83, 81, 79, 77, 75, 73, 70, 68, 66, 64, 63, 61, 59, 57,54, 52, 51, 49, 47, 44, 42, 40, 39, 37, 34, 33, 31, 29, 27, 25, 22, 20, 18, 17, 14, 13, 11, 9, 6, 4,2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]lut[:, 0, 1] = [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255, 255, 255, 255, 255, 255, 255, 255, 254, 252, 250, 248, 246, 244, 242, 240, 238, 236, 234, 232,230, 228, 226, 224, 222, 220, 218, 216, 214, 212, 210, 208, 206, 204, 202, 200, 198, 196, 194, 192,190, 188, 186, 184, 182, 180, 178, 176, 174, 171, 169, 167, 165, 163, 161, 159, 157, 155, 153, 151,149, 147, 145, 143, 141, 139, 137, 135, 133, 131, 129, 127, 125, 123, 121, 119, 117, 115, 113, 111,109, 107, 105, 103, 101, 99, 97, 95, 93, 91, 89, 87, 85, 83, 82, 80, 78, 76, 74, 72, 70, 68, 66, 64,62, 60, 58, 56, 54, 52, 50, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14,12, 10, 8, 6, 4, 2, 0]lut[:, 0, 2] = [195, 194, 193, 191, 190, 189, 188, 187, 186, 185, 184, 183, 182, 181, 179, 178, 177, 176, 175, 174,173, 172, 171, 170, 169, 167, 166, 165, 164, 163, 162, 161, 160, 159, 158, 157, 155, 154, 153, 152,151, 150, 149, 148, 147, 146, 145, 143, 142, 141, 140, 139, 138, 137, 136, 135, 134, 133, 131, 130,129, 128, 127, 126, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 126, 126, 126,126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126,126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 127,127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 126, 126, 126, 126, 126, 126, 126, 126, 126,126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126,126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126]im_color = cv2.LUT(im_gray, lut)return im_color;if __name__ == '__main__':im = cv2.imread("./David.jpg", cv2.IMREAD_GRAYSCALE)im = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR)im_color = applyCustomColorMap(im)cv2.imwrite('colormap_algae.jpg', im_color)cv2.imshow("Pseudo Colored Image", im_color)cv2.waitKey(0)
输入
输出