详解Python中常用的图片处理函数的使用
程序员文章站
2022-06-24 16:54:02
目录cvtcolor函数split()和merge()threshold()函数自定义threshold函数进行二值化色度函数applycolormapcvtcolor函数这个函数有两个参数1,src...
cvtcolor函数
这个函数有两个参数
1,src 要进行变换的原图像
2,code 转换代码标识
例子:
import cv2 image=cv2.imread("ddd.jpg") image1=cv2.cvtcolor(image,cv2.color_bgr2bgra) cv2.imshow("",image1) cv2.waitkey(0) if __name__ == '__main__': print()
split()和merge()
例子:
import cv2 image=cv2.imread("ddd.jpg") # image1=cv2.cvtcolor(image,cv2.color_bgr2bgra) r,g,b=cv2.split(image) cv2.imshow("r",r) cv2.imshow("g",g) cv2.imshow("b",b) cv2.waitkey(0) if __name__ == '__main__': print()
import cv2 image=cv2.imread("/home/dfy/pictures/camera_photo/camera_photo/sss.jpg") # image1=cv2.cvtcolor(image,cv2.color_bgr2bgra) r,g,b=cv2.split(image) cv2.imshow("r",r) cv2.imshow("g",g) cv2.imshow("b",b) image1=cv2.merge([b,g,r]) cv2.imshow("image",image1) cv2.waitkey(0) if __name__ == '__main__': print()
threshold()函数
ret,image= cv2.threshold(src,thresh,maxval,type)
实现二值化的重要函数
参数说明
src
输入图像
image
输出图像
thresh
阀值
maxval
当像素值超过阀值thresh 时赋值为maxval
type
当像素值小于阀值thresh的时赋值为type type可填下面5种类型的参数
例子:
import cv2 image=cv2.imread("/home/dfy/pictures/camera_photo/camera_photo/sss.jpg") # image1=cv2.cvtcolor(image,cv2.color_bgr2bgra) # r,g,b=cv2.split(image) # cv2.imshow("",r) # cv2.imshow("",g) # cv2.imshow("",b) # image1=cv2.merge([b,g,r]) # cv2.imshow("image",image1) ret,image1=cv2.threshold(image,127,255,cv2.thresh_binary) ret1,image2=cv2.threshold(image,127,255,cv2.thresh_binary_inv) # ret2,image3=cv2.threshold(image,127,255,cv2.thresh_triangle) ret3,image4=cv2.threshold(image,127,255,cv2.thresh_tozero) ret4,image5=cv2.threshold(image,127,255,cv2.thresh_tozero_inv) cv2.imshow("1",image1) cv2.imshow("2",image2) # cv2.imshow("3",image3) cv2.imshow("4",image4) cv2.imshow("5",image5) cv2.waitkey(0) if __name__ == '__main__': print()
自定义threshold函数进行二值化
import cv2 image=cv2.imread("/home/dfy/pictures/camera_photo/camera_photo/sss.jpg") width,height,n=image.shape image2=image.copy() for i in range(width): for j in range(height): for channel in range(3): if image2[i][j][channel]>127: image2[i][j][channel]=255 else: image2[i][j][channel]=0 cv2.imshow('',image2) cv2.waitkey(0) if __name__ == '__main__': print()
这个速度很慢对于分辨率太高的图
色度函数applycolormap
import cv2 image=cv2.imread("/home/dfy/pictures/camera_photo/camera_photo/sss.jpg") image_color_map=cv2.applycolormap(image,cv2.colormap_jet) cv2.imshow("im",image_color_map) cv2.waitkey(0) if __name__ == '__main__': print()
到此这篇关于详解python中常用的图片处理函数的使用的文章就介绍到这了,更多相关python图片处理函数内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: linux系统编程-1-基础
下一篇: Linux 系统编程基础