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

到此這篇關於詳解Python中常用的圖片處理函數的使用的文章就介紹到這瞭,更多相關Python圖片處理函數內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: