How to realize the use of common picture processing functions in Python
This article analyzes "how to implement the use of common image processing functions in Python". The content is detailed and easy to understand. Friends interested in "how to realize the use of common image processing functions in Python" can read it slowly and deeply with the idea of Xiaobian. I hope it can help everyone after reading. Let's learn how to use common image processing functions in Python together with Xiaobian.
cvtColor function
This function takes two parameters.
1, src Original image to be transformed
2, code conversion code identification
Examples:
import cv2image=cv2.imread("ddd.jpg")image1=cv2.cvtColor(image,cv2.COLOR_BGR2BGRA)cv2.imshow("",image1)cv2.waitKey(0)if __name__ == '__main__': print()split() and merge()
Examples:
import cv2image=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 cv2image=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)
An Important Function to Realize Binarization
parameter description
src Input image
image Output image
thresh
maxval is assigned maxval when the pixel value exceeds threshold thresh
type When the pixel value is less than the threshold value thresh, assign it to type. The following 5 types of parameters can be filled in.
Examples:
import cv2image=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() custom threshold function binarizes import cv2image= cv 2.imread ("/home/dfy/Pictures/Camera_photo/sss.jpg")width,height,n=image.shapeimage2=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]=0cv2.imshow('',image2)cv2.waitKey(0)if __name__ == '__main__': print()
It's slow for images that are too high resolution.
Chromaticity Function applyColorMapimport cv2image=cv2.imread("/home/dfy/Pictures/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() on how to implement the use of common image processing functions in Python to share here, I hope the above content can make everyone improve. If you want to learn more, please pay more attention to the updates of Xiaobian. Thank you for your attention to the website!