图像处理:
import cv2
import numpy as np
img = cv2.imread("ma.jpg")
"""
第二个参数,取值可为:
cv2.IMREAD_COLOR=1:默认参数,读入一副彩色图片,忽略alpha通道
cv2.IMREAD_GRAYSCALE=0:读入灰度图片
cv2.IMREAD_UNCHANGED=-1:读入完整图片,包括alpha通道(png有,jpg无)
"""
img1 = cv2.imread("ma.jpg",cv2.IMREAD_GRAYSCALE)
cv2.namedWindow("new window")
cv2.imshow("new window", img1)
cv2.imshow("new", img)
print(img.shape)
print(img.size)
print(img.dtype)
print(cv2.mean(img))
print(cv2.meanStdDev(img))
img2=img.copy()
cv2.imshow("fuzi", img2)
cv2.waitKey(5000)
cv2.destroyAllWindows()
创建窗口
cv2.namedWindow("operation", cv2.WINDOW_AUTOSIZE)
保存图像
cv2.imwrite("2.jpg",img)
像素操作
import cv2
img = cv2.imread("1.png")
print(img)
print(img[0,0])
print('第一列数据')
print(img[:,0])
print('第一行数据')
print(img[0,:])
import cv2
img = cv2.imread("1.png")
i=img[100,100]
(b,g,r)=i
print(i,b,g,r)
for x in range(0,101):
img[100+x,100]=(250,0,0)
cv2.imshow("new window", img)
cv2.waitKey(0)
图像缩放
import cv2
img = cv2.imread("1.png")
img1=cv2.resize(img,(0,0),fx=0.5,fy=0.5,interpolation=cv2.INTER_NEAREST)
cv2.imwrite("3.jpg",img1)
cv2.waitKey(0)
图片裁剪
import cv2
img = cv2.imread("500x400.jpg")
img1=img[100:300,125:375]
cv2.imwrite("linsi.jpg",img1)
cv2.waitKey(0)
绘图
写入文字
import cv2
import numpy as np
img = cv2.imread("500x400.jpg")
font=cv2.FONT_HERSHEY_SIMPLEX
font=cv2.FONT_HERSHEY_DUPLEX
img1=cv2.putText(img,'wo is liming',(80,90), font, 2,(255,255,255),3)
cv2.imwrite("linsi.jpg",img1)
cv2.waitKey(0)
绘制矩形
import cv2
image = cv2.imread("wuhuan.jpg",1)
cv2.rectangle(image,(50,100),(200,200),(255,0,0),-1)
cv2.imshow('image',image)
cv2.waitKey()
绘制多边形
import cv2
import numpy as np
img = cv2.imread("500x400.jpg")
points = np.array([[150,50],[140,140],[200,170],[250,250],[150,50]],np.int32)
points = points.reshape(-1,1,2)
img1=cv2.polylines(img,[points],True,(0,0,255),3)
cv2.imwrite("linsi.jpg",img1)
cv2.waitKey(0)
绘制椭圆
import cv2
img = cv2.imread("500x400.jpg")
img1=cv2.ellipse(img,(256,256),(100,50),0,30,180,(255,0,0),-1)
cv2.imwrite("linsi.jpg",img1)
cv2.waitKey(0)
绘制矩形
import cv2
img = cv2.imread("500x400.jpg")
img1=cv2.rectangle(img,(10,10),(30,40),(255,0,0),2)
cv2.imwrite("linsi.jpg",img1)
cv2.waitKey(0)
绘制线
import cv2
img = cv2.imread("500x400.jpg")
img1=cv2.line(img,(10,10),(200,200),(0,255,0),3)
cv2.imwrite("linsi.jpg",img1)
cv2.waitKey(0)
绘制点和圆圈
import cv2
img = cv2.imread("500x400.jpg")
img1=cv2.circle(img,(60,60),30,(0,0,255),-1)
img1=cv2.circle(img1,(200,200),50,(255,0,0),3)
cv2.imwrite("linsi.jpg",img1)
cv2.waitKey(0)
绘制图片
import cv2
image=cv2. imread('1.png')
height=int(image.shape[0]*0.2)
width =int(image.shape[1]* 0.2)
image_resize=cv2. resize(image,(width, height))
for i in range(height):
for j in range(width):
image[i+100,j+100]=image_resize[i,j]
cv2. imshow(' dst', image)
cv2.waitKey()
图像平移
import numpy as np
import cv2
img = cv2.imread("3.jpg")
cv2.imshow("yuan", img)
img_info=img.shape
height=img_info[0]
width=img_info[1]
mat_translation=np.float32([[1,0,20],[0,1,50]])
dst=cv2.warpAffine(img,mat_translation,(width+20,height+50))
cv2.imshow('dst',dst)
cv2.waitKey(0)
图像镜像翻转
import numpy as np
import cv2
img = cv2.imread("3.jpg")
cv2.imshow("yuan", img)
img1 = cv2.flip(img,1)
'''
参数2 必选参数。用于指定镜像翻转的类型,其中0表示绕×轴正直翻转,即垂直镜像翻转;1表示绕y轴翻转,即水平镜像翻转;-1表示绕×轴、y轴两个轴翻转,即对角镜像翻转。
参数3 可选参数。用于设置输出数组,即镜像翻转后的图像数据,默认为与输入图像数组大小和类型都相同的数组。
'''
cv2.imshow('dst',img1)
cv2.waitKey(0)
图像旋转
import cv2
img = cv2.imread('3.jpg', 1)
cv2.imshow('src', img)
imgInfo = img.shape
height= imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]
matRotate = cv2.getRotationMatrix2D((width*0.5, height*0.5), 45, 0.9)
'''
参数1 必选参数。用于设置旋转中心点,点坐标为OpenCV图像坐标系下的坐标。
参数2 必选参数。用于设置旋转的角度,单位为度。
参数3 必选参数。用于设置缩放系数,即对旋转的图像进行缩放。
'''
dst = cv2.warpAffine(img, matRotate, (width,height))
'''
参数2 变换矩阵:是一个2行3列的矩阵,由这个矩阵决定是何种变换
参数3 变换后输出图像的大小:(width,height)-->宽和高(自己规定)
'''
cv2.imshow('dst',dst)
cv2.waitKey(0)
图像叠加
import cv2
import numpy as np
import random
img = cv2.imread("500x400.jpg",1)
img1 = cv2.imread("tuzi500x400.jpg",1)
cv2.imshow('img',img)
cv2.imshow('img1',img1)
dst=cv2.add(img,img1)
'''
参数1 参数2 待叠加的两个图像,两个图像的大小,通道数必须一致
'''
cv2.imshow('dst',dst)
cv2.waitKey()
图像颜色反转
灰度图的图像颜色反转
import cv2
import numpy as np
img = cv2.imread("3.jpg",0)
height,width=img.shape
dst=np.zeros((height,width,1),np.uint8)
for i in range(height):
for j in range(width):
dst[i,j]=255-img[i,j]
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey()
bgr图像的图像颜色反转
import cv2
import numpy as np
img = cv2.imread("2013.jpg",1)
cha=img.shape
height,width,deep=cha
dst=np.zeros((height,width,3),np.uint8)
for i in range(height):
for j in range(width):
b,g,r=img[i,j]
dst[i,j]=(255-b,255-g,255-r)
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey()
图像毛玻璃模糊效果
import cv2
import numpy as np
import random
img = cv2.imread("2013.jpg",1)
cha=img.shape
height,width,deep=cha
cv2.imshow('img',img)
dst=np.zeros(cha,np.uint8)
randon_v=5
for m in range(height-randon_v):
for n in range(width-randon_v):
index=random.randint(1,randon_v)
(b,g,r)=img[m+index,n+index]
dst[m,n]=(b,g,r)
cv2.imshow('dst',dst)
cv2.waitKey()
边缘检测
import cv2
image = cv2.imread("wuhuan.jpg",1)
image_info =image.shape
height=image_info[0]
width=image_info[1]
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray',gray)
image_G=cv2.GaussianBlur(gray,(5,5),0)
dst=cv2.Canny(image_G,50,50)
cv2.imshow("dst",dst) cv2.waitKey()
视频
通过打开摄像头捕捉视频
import cv2
cap=cv2.VideoCapture(0)
if cap.isOpened():
print('已经打开了摄像头捕捉视频')
else:
print('打开摄像头失败')
通过打开视频文件读取视频
import cv2
cap=cv2.VideoCapture('1.mp4')
if cap.isOpened():
print('已经打开了视频文件')
else:
print('视频文件打开失败')
分解视频
import cv2
cap=cv2.VideoCapture('1.mp4')
if cap.isOpened():
print('已经打开了视频文件')
fps = cap.get(cv2.CAP_PROP_FPS)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
print('fps:', fps,'width:',width,'height:',height)
i=0
while 1:
if i==15:
print('保存了视频的前15帧图像,保存结束')
break
else:
i=i+1
ret, frame = cap.read()
file_name='.\\aa\\img'+str(i)+'.jpg'
cv2.imwrite(file_name, frame)
else:
print('视频文件打开失败')
生成视频
import cv2
image=cv2.imread('./aa/img5.jpg')
cv2.imshow("new window", image)
image_info=image.shape
height=image_info[0]
width=image_info[1]
size=(height,width)
print(size)
fps=30
fourcc=cv2.VideoWriter_fourcc(*"mp4v")
video = cv2.VideoWriter('ss.mp4', cv2.VideoWriter_fourcc(*"mp4v"), fps, (width,height))
"""
参数1 即将保存的文件路径
参数2 VideoWriter_fourcc为视频编解码器
fourcc意为四字符代码(Four-Character Codes),顾名思义,该编码由四个字符组成,下面是VideoWriter_fourcc对象一些常用的参数,注意:字符顺序不能弄混
cv2.VideoWriter_fourcc('I', '4', '2', '0'),该参数是YUV编码类型,文件名后缀为.avi
cv2.VideoWriter_fourcc('P', 'I', 'M', 'I'),该参数是MPEG-1编码类型,文件名后缀为.avi
cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'),该参数是MPEG-4编码类型,文件名后缀为.avi
cv2.VideoWriter_fourcc('T', 'H', 'E', 'O'),该参数是Ogg Vorbis,文件名后缀为.ogv
cv2.VideoWriter_fourcc('F', 'L', 'V', '1'),该参数是Flash视频,文件名后缀为.flv
cv2.VideoWriter_fourcc('m', 'p', '4', 'v') 文件名后缀为.mp4
参数3 为帧播放速率
参数4 (width,height)为视频帧大小
"""
for i in range(1,60):
file_name = './aa/img' + str(i) + '.jpg'
image=cv2.imread(file_name)
video.write(image)
cv2.waitKey()