入门第一课
知识点1:opencv helloword 打开示例图片
import cv2 as cv
import numpy as np
image=cv.imread("C:/Users/islix/Pictures/girl.jpg")
cv.imshow("input",image)
cv.waitKey(0)
cv.destroyAllWindows()
显示调出单张图片
知识点2:图像色彩空间转换
位深度,表示通道,rgb三个通道,3*8
opencv没有png的透明通道
def color_space_demo():
image = cv.imread("C:/Users/islix/Pictures/girl.jpg")
gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
cv.imshow("gray", gray)
cv.imshow("hsv", hsv)
cv.waitKey(0)
cv.destroyAllWindows()
效果如下:
hsv里h取值范围为0~180,sv取值0~255。
知识点3:显示形状和通道数
cv.IMREAD_GRAYSCALE→显示灰度图片
def mat_demo():
image = cv.imread("C:/Users/islix/Pictures/girl.jpg")
print(image.shape)#H,W,C
cv.imshow("image", image)
cv.waitKey(0)
cv.destroyAllWindows()
知识点4:创建空白图像
def mat_demo():
image = cv.imread("C:/Users/islix/Pictures/girl.jpg")
print(image.shape)#H,W,C
blank=np.zeros_like(image)
cv.imshow("blank",blank)
cv.imshow("image", image)
cv.waitKey(0)
cv.destroyAllWindows()
知识点4:拷贝一部分图像
def mat_demo():
image = cv.imread("C:/Users/islix/Pictures/girl.jpg")
print(image.shape)#H,W,C
roi=image[100:200,100:200,:]
blank=np.zeros_like(image)
blank[100:200,100:200,:]=image[100:200,100:200,:]
cv.imshow("blank",blank)
cv.imshow("image", image)
cv.waitKey(0)
cv.destroyAllWindows()
roi是单独的图片 单独进行显示。
你可以把它拷到图的任意位置。
知识点5像素读写
此代码可将图像变为rbg取反色图像的状态
def pixel_demo():
image = cv.imread("C:/Users/islix/Pictures/girl.jpg")
cv.imshow("input",image)
h,w,c=image.shape
for row in range(h):
for col in range(w):
b,g,r = image[row,col]
image[row,col] = (255-b,255-g,255-r)
cv.imshow("result", image)
cv.waitKey(0)
cv.destroyAllWindows()
知识点6像素相加
def math_demo():
image = cv.imread("C:/Users/islix/Pictures/girl.jpg")
# cv.imshow("input",image)
h,w,c=image.shape
blank= np.zeros_like(image)
blank[:,:]=(50,50,50)
cv.imshow("blank", blank)
result=cv.add(image,blank)
cv.imshow("result", result)
# cv.imwrite("C:/Users/islix/Pictures/girl_result.png",image)
cv.waitKey(0)
cv.destroyAllWindows()