python处理图片的一些列操作

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()

你可能感兴趣的