找回密码
 会员注册
查看: 44|回复: 0

pythonOpenCV—ColorMap

[复制链接]

7

主题

0

回帖

22

积分

新手上路

积分
22
发表于 2024-9-4 23:26:55 | 显示全部楼层 |阅读模式
文章目录cv2.applyColorMapcv2.putText小试牛刀自定义颜色参考学习来自OpenCV基础(21)使用OpenCV中的applyColorMap实现伪着色cv2.applyColorMapcv2.applyColorMap()是OpenCV中的一个函数,用于将灰度图像或单通道图像应用一个颜色映射,以产生伪彩色或彩色图像。这在可视化、增强图像的可读性或用于某些特定的图像分析任务时非常有用。函数的原型如下:cv2.applyColorMap(src,colormap[,dst])1src:输入图像,应为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):TwilightShifted风格的颜色映射。COLORMAP_TURBO(编号20):Turbo风格的颜色映射。COLORMAP_DEEPGREEN(编号21):深绿色调的颜色映射。dst:可选参数,输出图像。如果未提供,则创建一个新的图像。下面是一个简单的示例,展示如何使用cv2.applyColorMap():importcv2importnumpyasnp#读取一张灰度图像gray_img=cv2.imread('gray_image.png',cv2.IMREAD_GRAYSCALE)#应用JET颜色映射colored_img=cv2.applyColorMap(gray_img,cv2.COLORMAP_JET)#显示彩色图像cv2.imshow('ColoredImage',colored_img)cv2.waitKey(0)cv2.destroyAllWindows()12345678910111213在这个示例中,我们首先读取一张灰度图像,然后使用cv2.applyColorMap()函数将其转换为JET颜色映射的彩色图像,并显示出来。cv2.putTextcv2.putText是OpenCV库中的一个函数,用于在图像上添加文本。一、函数原型cv2.putText(img,text,org,fontFace,fontScale,color[,thickness[,lineType[,bottomLeftOrigin]]])1二、参数说明img:图像。text:要添加的文本字符串。org:文本字符串左下角的坐标,通常是一个元组,如(x,y)fontFace:字体类型。OpenCV提供了几种预定义的字体,或者你也可以使用Hershey字体。fontScale:字体大小的比例因子。color:文本的颜色。对于BGR,传递一个元组。thickness:可选参数,线条粗细。如果为负值(如-1),则填充文本。lineType:可选参数,线型。例如cv2.LINE_AA表示抗锯齿线型。bottomLeftOrigin:当标志为真时,图像数据的原点位于左下角。否则,它位于左上角。小试牛刀importcv2importnumpyasnpdefcolormap_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"}returnswitcher.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)foriinrange(0,col):forjinrange(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("PseudoColored",im_out)cv2.waitKey(0)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647输入图片输出结果自定义颜色importcv2importnumpyasnpdefapplyCustomColorMap(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)returnim_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("PseudoColoredImage",im_color)cv2.waitKey(0)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061输入输出
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-5 09:10 , Processed in 0.904079 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表